Killing the internet one fake blog post at a time.

Bash CheatSheet

Written in

by

A cheatsheet for Bash:

Basic Commands

  • ls – List directory contents
  • cd – Change the current working directory
  • mkdir – Create a new directory
  • touch – Create a new file or update the timestamp of an existing file
  • cat – Print the contents of a file to the screen
  • echo – Print a message to the screen
  • pwd – Print the current working directory
  • rm – Remove a file or directory
  • cp – Copy a file or directory to a new location
  • mv – Move or rename a file or directory
  • chmod – Change the permissions of a file or directory

Navigation

  • cd /path/to/directory – Change directory to the specified path
  • cd ~ – Change directory to the home directory
  • cd .. – Change directory to the parent directory
  • cd - – Change directory to the previous directory

File Management

  • ls -a – List all files, including hidden files
  • ls -l – List files with detailed information
  • ls -lh – List files with detailed information, including file size in human-readable format
  • rm -r – Remove a directory and its contents
  • cp -r – Copy a directory and its contents to a new location
  • mv file.txt newfile.txt – Rename a file
  • mv /path/to/file.txt . – Move a file to the current directory

Input/Output

  • > – Redirect output to a file (overwrite)
  • >> – Redirect output to a file (append)
  • < – Redirect input from a file
  • | – Pipe output to another command

File Permissions

  • chmod +x file.sh – Grant execute permission to a file
  • chmod u+x file.sh – Grant execute permission to the owner of a file
  • chmod o-r file.txt – Remove read permission from others for a file

Other Commands

  • man – Display the manual page for a command
  • history – Show a list of previously executed commands
  • grep – Search for a pattern in a file or output
  • find – Find files or directories that match a certain criteria
  • wget – Download a file from the internet
  • tar – Compress or extract files from an archive

Wildcards

  • * – Matches any sequence of characters
  • ? – Matches any single character
  • [] – Matches any single character within the specified range

Environment Variables

  • echo $PATH – Display the value of the PATH environment variable
  • export VAR=value – Set an environment variable
  • unset VAR – Unset an environment variable

Processes

  • ps – List running processes
  • kill PID – Send a signal to a process to terminate it
  • pkill name – Send a signal to all processes with a specific name
  • top – Display real-time information about running processes

SSH

  • ssh user@host – Connect to a remote host via SSH
  • scp file user@host:/path – Copy a file to a remote host via SSH

Text Editing

  • nano file.txt – Open a file for editing in the Nano text editor
  • vim file.txt – Open a file for editing in the Vim text editor
  • sed 's/old/new/g' file.txt – Replace all occurrences of a string in a file

Networking

  • ping host – Send a ping to a host to test network connectivity
  • ifconfig – Display network interface configuration
  • netstat – Display network connections and routing tables
  • route – Display or modify the system’s routing table

Job Control

  • jobs – Display a list of current jobs
  • bg – Move a job to the background
  • fg – Bring a job to the foreground
  • ctrl + c – Interrupt a running command
  • ctrl + z – Suspend a running command

This is just a small selection of the many commands available in Bash. With these tools at your disposal, you can navigate the file system, manage processes, edit files, and much more. As you become more familiar with Bash, you’ll discover new commands and techniques that can help you work more efficiently and productively.

Tags