GitHub - RehanSaeed - Bash-Cheat-Sheet - A Cheat Sheet For Bash Commands.
GitHub - RehanSaeed - Bash-Cheat-Sheet - A Cheat Sheet For Bash Commands.
com/RehanSaeed/Bash-Cheat-Sheet
MIT license
Star Notifications
1 of 11 2/12/24, 2:36
GitHub - RehanSaeed/Bash-Cheat-Sheet: A cheat sheet... https://github.com/RehanSaeed/Bash-Cheat-Sheet
Command History
touch foo.sh
chmod +x !$ # !$ is the last argument of the last command i.e. foo.sh
Navigating Directories
Creating Directories
Moving Directories
Deleting Directories
2 of 11 2/12/24, 2:36
GitHub - RehanSaeed/Bash-Cheat-Sheet: A cheat sheet... https://github.com/RehanSaeed/Bash-Cheat-Sheet
Creating Files
read foo # Read from standard input and write to the variable foo
Moving Files
rsync -z|--compress -v|--verbose /foo.txt /bar # Copy file quickly if not changed
rsync z|--compress -v|--verbose /foo.txt /bar.txt # Copy and rename file quickly if not changed
Deleting Files
Reading Files
File Permissions
3 of 11 2/12/24, 2:36
GitHub - RehanSaeed/Bash-Cheat-Sheet: A cheat sheet... https://github.com/RehanSaeed/Bash-Cheat-Sheet
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
Finding Files
Find binary files for a command.
Find in Files
Replace in Files
4 of 11 2/12/24, 2:36
GitHub - RehanSaeed/Bash-Cheat-Sheet: A cheat sheet... https://github.com/RehanSaeed/Bash-Cheat-Sheet
sed 's/fox/bear/g' foo.txt # Replace fox with bear in foo.txt and output to console
sed 's/fox/bear/gi' foo.txt # Replace fox (case insensitive) with bear in foo.txt and output to console
sed 's/red fox/blue bear/g' foo.txt # Replace red with blue and fox with bear in foo.txt and output to console
sed 's/fox/bear/g' foo.txt > bar.txt # Replace fox with bear in foo.txt and save in bar.txt
sed 's/fox/bear/g' foo.txt -i|--in-place # Replace fox with bear and overwrite foo.txt
Symbolic Links
Compressing Files
zip
Compresses one or more files into *.zip files.
gzip
Compresses a single file into *.gz files.
gzip /bar.txt foo.gz # Compress bar.txt into foo.gz and then delete bar.txt
gzip -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.tgz
tar -c|--create -z|--gzip -f|--file=foo.tgz /{bar,baz}.txt # Compress bar.txt and baz.txt into foo.tgz
tar -c|--create -z|--gzip -f|--file=foo.tgz /bar # Compress directory bar into foo.tgz
Decompressing Files
unzip
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
Disk Usage
5 of 11 2/12/24, 2:36
GitHub - RehanSaeed/Bash-Cheat-Sheet: A cheat sheet... https://github.com/RehanSaeed/Bash-Cheat-Sheet
du -h|--human-readable # List current directory, subdirectories and file sizes in a human readable format
du -d|--max-depth # List current directory, subdirectories and file sizes within the max depth
du -d 0 # List current directory size
Memory Usage
Packages
Identifying Processes
sleep 30 & # Sleep for 30 seconds and move the process into the background
jobs # List all background jobs
jobs -p # List all background jobs with their PID
lsof # List all open files and the process using them
lsof -itcp:4000 # Return the process listening on port 4000
Process Priority
Process priorities go from -20 (highest) to 19 (lowest).
Killing Processes
6 of 11 2/12/24, 2:36
GitHub - RehanSaeed/Bash-Cheat-Sheet: A cheat sheet... https://github.com/RehanSaeed/Bash-Cheat-Sheet
kill 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.
time tree # Time how long the tree command takes to execute
Scheduled Tasks
* * * * *
Minute, Hour, Day of month, Month, Day of the week
HTTP Requests
Network Troubleshooting
ping example.com # Send multiple ping requests using the ICMP protocol
ping -c 10 -i 5 example.com # Make 10 attempts, 5 seconds apart
traceroute example.com # List all servers the network traffic goes through
mtr -w|--report-wide example.com # Continually list all servers the network traffic goes through
mtr -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
7 of 11 2/12/24, 2:36
GitHub - RehanSaeed/Bash-Cheat-Sheet: A cheat sheet... https://github.com/RehanSaeed/Bash-Cheat-Sheet
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
Hardware
Terminal Multiplexers
Start multiple terminal sessions. Active sessions persist reboots. tmux is more modern than screen .
ssh hostname # Connect to hostname using your current user name over the default SSH port 22
ssh -i foo.pem hostname # Connect to hostname using the identity file
ssh user@hostname # Connect to hostname using the user over the default SSH port 22
ssh user@hostname -p 8765 # Connect to hostname using the user over a custom port
ssh 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
8 of 11 2/12/24, 2:36
GitHub - RehanSaeed/Bash-Cheat-Sheet: A cheat sheet... https://github.com/RehanSaeed/Bash-Cheat-Sheet
Bash Script
Variables
#!/bin/bash
Environment Variables
#!/bin/bash
Functions
#!/bin/bash
greet() {
local world = "World"
echo "$1 $world"
return "$1 $world"
}
greet "Hello"
greeting=$(greet "Hello")
Exit Codes
#!/bin/bash
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
9 of 11 2/12/24, 2:36
GitHub - RehanSaeed/Bash-Cheat-Sheet: A cheat sheet... https://github.com/RehanSaeed/Bash-Cheat-Sheet
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/bash
Inline If Statements
#!/bin/bash
While Loops
#!/bin/bash
declare -i counter
counter=10
while [$counter -gt 2]; do
echo The counter is $counter
counter=counter-1
done
For Loops
#!/bin/bash
for i in {0..10..2}
do
echo "Index: $i"
done
for filename in *;
do
echo "Content: " >> $filename
done
Case Statements
#!/bin/bash
case $weather in
sunny | warm ) echo "Nice weather: " $weather
;;
cloudy | cool ) echo "Not bad weather: " $weather
;;
rainy | cold ) echo "Terrible weather: " $weather
10 of 11 2/12/24, 2:36
GitHub - RehanSaeed/Bash-Cheat-Sheet: A cheat sheet... https://github.com/RehanSaeed/Bash-Cheat-Sheet
Releases
No releases published
Packages
No packages published
Contributors 2
khairahscorner Airah
11 of 11 2/12/24, 2:36