Bash Cheatsheet
Bash is the default shell on most Unix systems. This cheatsheet covers navigation, file ops, pipes, and scripting basics.
Bash is the command-line shell found on most Linux and macOS systems. This cheatsheet covers everyday commands and scripting basics.
Navigation
Move around the filesystem.
pwd # print current directory
ls -la # list all, long format
cd /path/to/dir # change directory
cd .. # up one level
cd ~ # home directory
File Operations
Create, copy, move, delete.
touch file.txt # create empty file
mkdir -p a/b/c # create nested dirs
cp src dest # copy
mv old new # move / rename
rm file.txt # delete file
rm -rf dir # delete dir recursively
Viewing Files
Read and search content.
cat file.txt # print file
less file.txt # scroll file
head -n 20 file # first 20 lines
tail -f log.txt # follow a log
grep "error" file # search text
Pipes & Redirection
Chain commands together.
ls | grep ".js" # pipe output
command > out.txt # overwrite file
command >> out.txt # append to file
command 2> err.txt # redirect errors
cat file | wc -l # count lines
Variables
Store and use values.
name="Ada"
echo "Hello $name"
export PATH="$PATH:/new/bin"
count=$(ls | wc -l)
Conditionals
Branch on conditions.
if [ -f file.txt ]; then
echo "exists"
elif [ -d dir ]; then
echo "is dir"
else
echo "missing"
fi
Loops
Iterate over items.
for f in *.txt; do
echo "$f"
done
while read line; do
echo "$line"
done < file.txt
Permissions & Processes
Manage access and running tasks.
chmod +x script.sh # make executable
chmod 644 file # rw-r--r--
ps aux | grep node # find process
kill -9 <pid> # force kill
Bash is a powerful tool for automation and system tasks. Master these commands, then write scripts to automate repetitive workflows.
For full documentation, see https://www.gnu.org/software/bash/manual/