Bash Cheat Sheet
A cheat sheet for bash commands.
Command History
!! # Run the last commandtouch foo.sh
chmod +x !$ # !$ is the last argument of the last command i.e. foo.sh
Navigating Directories
pwd # Print current directory pathls # List directoriesls -a|--all # List directories including hiddenls -l # List directories in long formls -l -h|--human-readable # List directories in long form with human readable sizesls -t # List directories by modification time, newest firststat foo.txt # List size, created and modified timestamps for a filestat foo # List size, created and modified timestamps for a directory
tree # List directory and file tree
tree -a # List directory and file tree including hidden
tree -d # List directory treecd foo # Go to foo sub-directorycd # Go to home directorycd ~ # Go to home directorycd - # Go to last directorypushd foo # Go to foo sub-directory and add previous directory to stackpopd # Go back to directory in stack saved by `pushd`
Creating Directories
mkdir foo # Create a directorymkdir foo bar # Create multiple directoriesmkdir -p|--parents foo/bar # Create nested directorymkdir -p|--parents {foo,bar}/baz # Create multiple nested directories
mktemp -d|--directory # Create a temporary directory
Moving Directories
cp -R|--recursive foo bar # Copy directorymv foo bar # Move directoryrsync -z|--compress -v|--verbose /foo /bar # Copy directory, overwrites destinationrsync -a|--archive -z|--compress -v|--verbose /foo /bar # Copy directory, without overwriting destinationrsync -avz /foo username@hostname:/bar # Copy local directory to remote directoryrsync -avz username@hostname:/foo /bar # Copy remote directory to local directory
Deleting Directories
rmdir foo # Delete non-empty directoryrm -r|--recursive foo # Delete directory including contentsrm -r|--recursive -f|--force foo # Delete directory including contents, ignore nonexistent files and never prompt
Creating Files
touch foo.txt # Create file or update existing files modified timestamptouch foo.txt bar.txt # Create multiple filestouch {foo,bar}.txt # Create multiple filestouch test{1..3} # Create test1, test2 and test3 filestouch test{a..c} # Create testa, testb and testc files
mktemp # Create a temporary file
Standard Output, Standard Error and Standard Input
echo "foo" > bar.txt # Overwrite file with contentecho "foo" >> bar.txt # Append to file with contentls exists 1> stdout.txt # Redirect the standard output to a filels noexist 2> stderror.txt # Redirect the standard error output to a filels 2>&1 out.txt # Redirect standard output and error to a filels > /dev/null # Discard standard output and errorread foo # Read from standard input and write to the variable foo
Moving Files
cp foo.txt bar.txt # Copy filemv foo.txt bar.txt # Move filersync -z|--compress -v|--verbose /foo.txt /bar # Copy file quickly if not changedrsync z|--compress -v|--verbose /foo.txt /bar.txt # Copy and rename file quickly if not changed
Deleting Files
rm foo.txt # Delete filerm -f|--force foo.txt # Delete file, ignore nonexistent files and never prompt
Reading Files
cat foo.txt # Print all contentsless foo.txt # Print some contents at a time (g - go to top of file, SHIFT+g, go to bottom of file, /foo to search for 'foo')head foo.txt # Print top 10 lines of filetail foo.txt # Print bottom 10 lines of fileopen foo.txt # Open file in the default editorwc foo.txt # List number of lines words and characters in the file
File Permissions
# | Permission | rwx | Binary |
---|---|---|---|
7 | read, write and execute | rwx | 111 |
6 | read and write | rw- | 110 |
5 | read and execute | r-x | 101 |
4 | read only | r-- | 100 |
3 | write and execute | -wx | 011 |
2 | write only | -w- | 010 |
1 | execute only | --x | 001 |
0 | none | --- | 000 |
For a directory, execute means you can enter a directory.
User | Group | Others | Description |
---|---|---|---|
6 | 4 | 4 | User can read and write, everyone else can read (Default file permissions) |
7 | 5 | 5 | User can read, write and execute, everyone else can read and execute (Default directory permissions) |
- u - User
- g - Group
- o - Others
- a - All of the above
ls -l /foo.sh # List file permissionschmod +100 foo.sh # Add 1 to the user permissionchmod -100 foo.sh # Subtract 1 from the user permissionchmod u+x foo.sh # Give the user execute permissionchmod g+x foo.sh # Give the group execute permissionchmod u-x,g-x foo.sh # Take away the user and group execute permissionchmod u+x,g+x,o+x foo.sh # Give everybody execute permissionchmod a+x foo.sh # Give everybody execute permissionchmod +x foo.sh # Give everybody execute permission
Finding Files
Find binary files for a command.
type wget # Find the binarywhich wget # Find the binarywhereis wget # Find the binary, source, and manual page files
locate
uses an index and is fast.
updatedb # Update the indexlocate foo.txt # Find a filelocate --ignore-case # Find a file and ignore caselocate f*.txt # Find a text file starting with 'f'
find
doesn't use an index and is slow.
find /path -name foo.txt # Find a filefind /path -iname foo.txt # Find a file with case insensitive searchfind /path -name "*.txt" # Find all text filesfind /path -name foo.txt -delete # Find a file and delete itfind /path -name "*.png" -exec pngquant {} # Find all .png files and execute pngquant on itfind /path -type f -name foo.txt # Find a filefind /path -type d -name foo # Find a directoryfind /path -type l -name foo.txt # Find a symbolic linkfind /path -type f -mtime +30 # Find files that haven't been modified in 30 daysfind /path -type f -mtime +30 -delete # Delete files that haven't been modified in 30 days
Find in Files
grep 'foo' /bar.txt # Search for 'foo' in file 'bar.txt'grep 'foo' /bar -r|--recursive # Search for 'foo' in directory 'bar'grep 'foo' /bar -R|--dereference-recursive # Search for 'foo' in directory 'bar' and follow symbolic linksgrep 'foo' /bar -l|--files-with-matches # Show only files that matchgrep 'foo' /bar -L|--files-without-match # Show only files that don't matchgrep 'Foo' /bar -i|--ignore-case # Case insensitive searchgrep 'foo' /bar -x|--line-regexp # Match the entire linegrep 'foo' /bar -C|--context 1 # Add N line of context above and below each search resultgrep 'foo' /bar -v|--invert-match # Show only lines that don't matchgrep 'foo' /bar -c|--count # Count the number lines that matchgrep 'foo' /bar -n|--line-number # Add line numbersgrep 'foo' /bar --colour # Add colour to outputgrep 'foo\|bar' /baz -R # Search for 'foo' or 'bar' in directory 'baz'grep --extended-regexp|-E 'foo|bar' /baz -R # Use regular expressionsegrep 'foo|bar' /baz -R # Use regular expressions
Replace in Files
sed 's/fox/bear/g' foo.txt # Replace fox with bear in foo.txt and output to consolesed 's/fox/bear/gi' foo.txt # Replace fox (case insensitive) with bear in foo.txt and output to consolesed 's/red fox/blue bear/g' foo.txt # Replace red with blue and fox with bear in foo.txt and output to consolesed 's/fox/bear/g' foo.txt > bar.txt # Replace fox with bear in foo.txt and save in bar.txtsed 's/fox/bear/g' foo.txt -i|--in-place # Replace fox with bear and overwrite foo.txt
Symbolic Links
ln -s|--symbolic foo bar # Create a link 'bar' to the 'foo' folderln -s|--symbolic -f|--force foo bar # Overwrite an existing symbolic link 'bar'ls -l # Show where symbolic links are pointing
Compressing Files
zip
Compresses one or more files into *.zip files.
zip foo.zip /bar.txt # Compress bar.txt into foo.zipzip foo.zip /bar.txt /baz.txt # Compress bar.txt and baz.txt into foo.zipzip foo.zip /{bar,baz}.txt # Compress bar.txt and baz.txt into foo.zipzip -r|--recurse-paths foo.zip /bar # Compress directory bar into foo.zip
gzip
Compresses a single file into *.gz files.
gzip /bar.txt foo.gz # Compress bar.txt into foo.gz and then delete bar.txtgzip -k|--keep /bar.txt foo.gz # Compress bar.txt into foo.gz
tar -c
Compresses (optionally) and combines one or more files into a single *.tar, *.tar.gz, *.tpz or *.tgz file.
tar -c|--create -z|--gzip -f|--file=foo.tgz /bar.txt /baz.txt # Compress bar.txt and baz.txt into foo.tgztar -c|--create -z|--gzip -f|--file=foo.tgz /{bar,baz}.txt # Compress bar.txt and baz.txt into foo.tgztar -c|--create -z|--gzip -f|--file=foo.tgz /bar # Compress directory bar into foo.tgz
Decompressing Files
unzip
unzip foo.zip # Unzip foo.zip into current directory
gunzip
gunzip foo.gz # Unzip foo.gz into current directory and delete foo.gz
gunzip -k|--keep foo.gz # Unzip foo.gz into current directory
tar -x
tar -x|--extract -z|--gzip -f|--file=foo.tar.gz # Un-compress foo.tar.gz into current directorytar -x|--extract -f|--file=foo.tar # Un-combine foo.tar into current directory
Disk Usage
df # List disks, size, used and available spacedf -h|--human-readable # List disks, size, used and available space in a human readable formatdu # List current directory, subdirectories and file sizesdu /foo/bar # List specified directory, subdirectories and file sizesdu -h|--human-readable # List current directory, subdirectories and file sizes in a human readable formatdu -d|--max-depth # List current directory, subdirectories and file sizes within the max depthdu -d 0 # List current directory size
Memory Usage
free # Show memory usagefree -h|--human # Show human readable memory usagefree -h|--human --si # Show human readable memory usage in power of 1000 instead of 1024free -s|--seconds 5 # Show memory usage and update continuously every five seconds
Packages
apt update # Refreshes repository indexapt search wget # Search for a packageapt show wget # List information about the wget packageapt install wget # Install the wget packageapt remove wget # Removes the wget packageapt upgrade # Upgrades all upgradable packages
Shutdown and Reboot
shutdown # Shutdown in 1 minuteshutdown now "Cya later" # Immediately shut downshutdown +5 "Cya later" # Shutdown in 5 minutesshutdown --reboot # Reboot in 1 minuteshutdown -r now "Cya later" # Immediately rebootshutdown -r +5 "Cya later" # Reboot in 5 minutesshutdown -c # Cancel a shutdown or rebootreboot # Reboot nowreboot -f # Force a reboot
Identifying Processes
top # List all processes interactivelyhtop # List all processes interactivelyps all # List all processes
pidof foo # Return the PID of all foo processes
CTRL+Z # Suspend a process running in the foregroundbg # Resume a suspended process and run in the backgroundfg # Bring the last background process to the foregroundfg 1 # Bring the background process with the PID to the foregroundsleep 30 & # Sleep for 30 seconds and move the process into the backgroundjobs # List all background jobsjobs -p # List all background jobs with their PIDlsof # List all open files and the process using themlsof -itcp:4000 # Return the process listening on port 4000
Process Priority
Process priorities go from -20 (highest) to 19 (lowest).
nice -n -20 foo # Change process priority by namerenice 20 PID # Change process priority by PIDps -o ni PID # Return the process priority of PID
Killing Processes
CTRL+C # Kill a process running in the foregroundkill PID # Shut down process by PID gracefully. Sends TERM signal.kill -9 PID # Force shut down of process by PID. Sends SIGKILL signal.pkill foo # Shut down process by name gracefully. Sends TERM signal.pkill -9 foo # force shut down process by name. Sends SIGKILL signal.killall foo # Kill all process with the specified name gracefully.
Date & Time
date # Print the date and timedate --iso-8601 # Print the ISO8601 datedate --iso-8601=ns # Print the ISO8601 date and timetime tree # Time how long the tree command takes to execute
Scheduled Tasks
* * * * *
Minute, Hour, Day of month, Month, Day of the week
crontab -l # List cron tabcrontab -e # Edit cron tab in Vimcrontab /path/crontab # Load cron tab from a filecrontab -l > /path/crontab # Save cron tab to a file
* * * * * foo # Run foo every minute
*/15 * * * * foo # Run foo every 15 minutes0 * * * * foo # Run foo every hour15 6 * * * foo # Run foo daily at 6:15 AM44 4 * * 5 foo # Run foo every Friday at 4:44 AM0 0 1 * * foo # Run foo at midnight on the first of the month0 0 1 1 * foo # Run foo at midnight on the first of the year
at -l # List scheduled tasks
at -c 1 # Show task with ID 1
at -r 1 # Remove task with ID 1
at now + 2 minutes # Create a task in Vim to execute in 2 minutes
at 12:34 PM next month # Create a task in Vim to execute at 12:34 PM next month
at tomorrow # Create a task in Vim to execute tomorrow
HTTP Requests
curl https://example.com # Return response bodycurl -i|--include https://example.com # Include status code and HTTP headerscurl -L|--location https://example.com # Follow redirectscurl -o|--remote-name foo.txt https://example.com # Output to a text filecurl -H|--header "User-Agent: Foo" https://example.com # Add a HTTP headercurl -X|--request POST -H "Content-Type: application/json" -d|--data '{"foo":"bar"}' https://example.com # POST JSONcurl -X POST -H --data-urlencode foo="bar" http://example.com # POST URL Form Encodedwget https://example.com/file.txt . # Download a file to the current directorywget -O|--output-document foo.txt https://example.com/file.txt # Output to a file with the specified name
Network Troubleshooting
ping example.com # Send multiple ping requests using the ICMP protocolping -c 10 -i 5 example.com # Make 10 attempts, 5 seconds apartip addr # List IP addresses on the systemip route show # Show IP addresses to routernetstat -i|--interfaces # List all network interfaces and in/out usagenetstat -l|--listening # List all open portstraceroute example.com # List all servers the network traffic goes throughmtr -w|--report-wide example.com # Continually list all servers the network traffic goes throughmtr -r|--report -w|--report-wide -c|--report-cycles 100 example.com # Output a report that lists network traffic 100 times
nmap 0.0.0.0 # Scan for the 1000 most common open ports on localhost
nmap 0.0.0.0 -p1-65535 # Scan for open ports on localhost between 1 and 65535
nmap 192.168.4.3 # Scan for the 1000 most common open ports on a remote IP address
nmap -sP 192.168.1.1/24 # Discover all machines on the network by ping'ing them
DNS
host example.com # Show the IPv4 and IPv6 addressesdig example.com # Show complete DNS informationcat /etc/resolv.conf # resolv.conf lists nameservers
Hardware
lsusb # List USB devices
lspci # List PCI hardware
lshw # List all hardware
Terminal Multiplexers
Start multiple terminal sessions. Active sessions persist reboots. tmux
is more modern than screen
.
tmux # Start a new session (CTRL-b + d to detach)
tmux ls # List all sessions
tmux attach -t 0 # Reattach to a sessionscreen # Start a new session (CTRL-a + d to detach)screen -ls # List all sessionsscreen -R 31166 # Reattach to a sessionexit # Exit a session
Secure Shell Protocol (SSH)
ssh hostname # Connect to hostname using your current user name over the default SSH port 22ssh -i foo.pem hostname # Connect to hostname using the identity filessh user@hostname # Connect to hostname using the user over the default SSH port 22ssh user@hostname -p 8765 # Connect to hostname using the user over a custom portssh ssh://user@hostname:8765 # Connect to hostname using the user over a custom port
Set default user and port in ~/.ssh/config
, so you can just enter the name next time:
$ cat ~/.ssh/config
Host name
User foo
Hostname 127.0.0.1
Port 8765
$ ssh name
Secure Copy
scp foo.txt ubuntu@hostname:/home/ubuntu # Copy foo.txt into the specified remote directory
Bash Profile
- bash -
.bashrc
- zsh -
.zshrc
# Always run ls after cdfunction cd {builtin cd "$@" && ls}# Prompt user before overwriting any filesalias cp='cp --interactive'alias mv='mv --interactive'alias rm='rm --interactive'# Always show disk usage in a human readable formatalias df='df -h'alias du='du -h'
Bash Script
Variables
#!/bin/bashfoo=123 # Initialize variable foo with 123declare -i foo=123 # Initialize an integer foo with 123declare -r foo=123 # Initialize readonly variable foo with 123echo $foo # Print variable fooecho ${foo}_'bar' # Print variable foo followed by _barecho ${foo:-'default'} # Print variable foo if it exists otherwise print defaultexport foo # Make foo available to child processesunset foo # Make foo unavailable to child processes
Environment Variables
#!/bin/bashenv # List all environment variablesecho $PATH # Print PATH environment variable
Functions
#!/bin/bashgreet() {local world = "World"echo "$1 $world"return "$1 $world"}
greet "Hello"greeting=$(greet "Hello")
Exit Codes
#!/bin/bashexit 0 # Exit the script successfullyexit 1 # Exit the script unsuccessfullyecho $? # Print the last exit code
Conditional Statements
Boolean Operators
$foo
- Is true!$foo
- Is false
Numeric Operators
-eq
- Equals-ne
- Not equals-gt
- Greater than-ge
- Greater than or equal to-lt
- Less than-le
- Less than or equal to-e
foo.txt - Check file exists-z
foo - Check if variable exists
String Operators
=
- Equals==
- Equals-z
- Is null-n
- Is not null<
- Is less than in ASCII alphabetical order>
- Is greater than in ASCII alphabetical order
If Statements
#!/bin/bashif [[$foo = 'bar']]; thenecho 'one'elif [[$foo = 'bar']] || [[$foo = 'baz']]; thenecho 'two'elif [[$foo = 'ban']] && [[$USER = 'bat']]; thenecho 'three'elseecho 'four'fi
Inline If Statements
#!/bin/bash[[ $USER = 'rehan' ]] && echo 'yes' || echo 'no'
While Loops
#!/bin/bashdeclare -i counter
counter=10while [$counter -gt 2]; doecho The counter is $countercounter=counter-1
done
For Loops
#!/bin/bashfor i in {0..10..2}doecho "Index: $i"donefor filename in file1 file2 file3
doecho "Content: " >> $filenamedonefor filename in *;doecho "Content: " >> $filenamedone
Case Statements
#!/bin/bashecho "What's the weather like tomorrow?"read weather
case $weather in
sunny | warm ) echo "Nice weather: " $weather;;
cloudy | cool ) echo "Not bad weather: " $weather;;
rainy | cold ) echo "Terrible weather: " $weather;;
* ) echo "Don't understand";;esac