bash Cheat sheet
Navigation
Change directory
cd
Go to home directory
cd ~
Go to last directory
cd ..
Print working directory
pwd
List files and directories
ls
List files and directories in long format
ls -l
List files and directories in long format with human readable sizes
ls -lh
List files and directories in long format and short by modification time, newest first
ls -lt
List all files, including hidden ones
ls -a
Standard Output
Print a string to the console
echo 'Hello, World!'
Enable interpretation of backslash escapes (e.g., '\n' for a new line)
echo -e 'Hello\nWorld'
Formatted printing with placeholders
printf 'Hello, %s!' 'User'
Basics
displays all environment variables
env
displays the shell you're using
echo $SHELL
File and Directory Operations
Create an empty file
touch <filename>
Create a directory
mkdir <directory>
Create nested directory
mkdir -p foo/bar
Copy a file or directory
cp <source> <destination>
Copy a directory
cp -R <source-folder> <destination>
Move or rename a file or directory
mv <source> <destination>
Remove a file
rm <file>
Remove a directory and its contents
rm -r <directory>
creates symbolic link to file
ln -s <filename> <link>
System
returns your username
whoami
shows the current date and time
date
shows kernel information
uname -a
shows disk usage
df
shows the disk usage of the files and directories in filename (du -s give only a total)
du <filename>
displays your currently active processes
top
pings host and outputs results
ping <host>
gets whois information for domain
whois <domain>
report time consumed by command execution
time <command>
File Content
replace pattern in file with replacement value to std output the character after s (/) is the delimeter
sed 's/<pattern>/<replacement>/g' <filename>
replace pattern in file with replacement value in place
sed -i 's/<pattern>/<replacement>/g' <filename>
replace pattern from input stream with replacement value
echo "this" | sed 's/is/at/g'
File Viewing and Editing
Display file content
cat <file>
Open a file for editing in the Nano text editor
nano <file>
Open a file for editing in the Vim text editor
vim <file>
Display the first lines of a file
head <file>
Display the last lines of a file
tail <file>
File Permissions
Change file permissions
chmod <permissions> <file>
Change file owner and group
chown <user>:<group> <file>
Change file group
chgrp <group> <file>
Pipelines and Redirection
Use the output of command1 as input for command2 (pipeline)
command1 | command2
Redirect output to a file (overwrite)
command > file
Redirect output to a file (append)
command >> file
Use a file as input for a command
command < file
Variables
Assign a value to a variable
variable_name=value
Display the value of a variable
echo $variable_name
Display the value of a variable
echo "Hi $variable_name"
Shell execution
Shell execution
echo "I am in $(pwd)"
Control Structures
Conditional statement
if condition; then
echo "Hi";
fi
Loop over a list of items
for item in list; do
echo "Hi";
done
Loop over a list of items
for i in {1..5}; do
echo "index: $i";
done
Loop over a list of items
for ((i = 0 ; i < 100 ; i++)); do
echo "index: $i";
done
Loop while a condition is true
while condition; do
echo "Hi";
done
Functions
Define a function
function_name() {
echo "John"
}
Call a function with arguments
echo "You are $(function_name)"
Call a function with arguments
function_name arguments
Miscellaneous
Show command history
history
Display manual for a command
man <command>