Free Bash cheat sheet

Bash cheat sheet

The terminal commands you use every day on one page: navigate, manage files, search text, pipe, redirect, and set permissions.

Move aroundFiles and foldersLook at filesSearchPipes and redirectionPermissions and processes

← All cheat sheets

Bash Cheat Sheet

Quick reference from FreeCodingCourses.com

Move around

pwd
print the folder you are in.
ls -la
list all files, long format, including hidden ones.
cd path/to/dir
change directory. cd .. goes up, cd ~ goes home.
clear
clear the screen (Ctrl+L does the same).

Files and folders

mkdir project
make a directory. -p makes parents too.
touch file.txt
create an empty file.
cp src.txt dest.txt
copy. cp -r for a folder.
mv old.txt new.txt
move or rename.
rm file.txt
delete a file. rm -r for a folder. No undo, be careful.

Look at files

cat file.txt
print the whole file.
less file.txt
scroll a long file. q to quit.
head -n 20 file.txt tail -n 20 file.txt
first / last 20 lines.
tail -f log.txt
follow a file as it grows (live logs).

Search

grep 'error' log.txt
find lines containing text.
grep -ri 'todo' .
recursive, case-insensitive, from here down.
find . -name '*.js'
find files by name pattern.
history | grep git
search your past commands.

Pipes and redirection

ls | grep .txt
pipe: send one command's output into the next.
echo hi > file.txt
write output to a file (overwrites).
echo more >> file.txt
>> appends instead of overwriting.
command 2> errors.txt
redirect error output (stderr).

Permissions and processes

chmod +x script.sh
make a file executable.
./script.sh
run a script in the current folder.
sudo command
run as administrator. Use with care.
ps aux | grep node
find a running process; kill <pid> stops it.

Unlock the full Bash cheat sheet

You're seeing 2 of 6 sections. Drop your email to open the rest plus the gotchas, and save the whole thing as a printable PDF.

No spam, unsubscribe anytime. See our privacy policy.

Bash questions, answered

Is Bash the same as the terminal or the shell?

They are related. The terminal is the window; the shell is the program inside it that reads your commands. Bash is one common shell. macOS now defaults to a similar shell called zsh, but the everyday commands here work the same in both.

Why do I need the command line if I have a mouse?

For a lot of developer work the terminal is simply faster and scriptable: installing packages, running builds, using Git, connecting to servers. Many tools have no graphical interface at all. You do not need to memorize everything, but the basics here come up daily.

What does sudo do, and is it safe?

sudo runs a command with administrator privileges, which you need for things like installing system software. It is safe when you understand the command you are running. Be cautious pasting sudo commands from the internet, since they can change or damage your system.

Where to go next