Moving around
| Command | What it does |
pwd | Show which folder you are in. |
ls | List files. Use ls -la to see hidden files and details. |
cd folder | Go into a folder. cd .. goes up one, cd ~ goes home. |
clear | Clear the screen. |
Files and folders
| Command | What it does |
mkdir -p a/b | Make a folder (and any folders above it). |
touch file.txt | Make an empty file. |
cp a.txt b.txt | Copy a file. Use cp -r to copy a folder. |
mv a.txt b.txt | Move or rename a file. |
rm file.txt | Delete a file. There is no recycle bin. |
rm -r folder | Delete a folder and everything in it. Be very careful. |
Reading and searching
| Command | What it does |
cat file.txt | Print a whole file. |
less file.txt | Read a long file page by page. Press q to quit. |
head -n 20 file.txt | Show the first 20 lines. tail shows the last lines. |
tail -f app.log | Watch a log file as new lines arrive. |
grep -rn 'text' . | Find text inside files in this folder and below. |
find . -name '*.py' | Find files by name. |
wc -l file.txt | Count lines. |
Permissions
| Command | What it does |
ls -l | Show permissions as rwx for owner, group and others. |
chmod +x script.sh | Make a file runnable. |
chmod 644 file.txt | Owner can read and write, everyone else can read. |
chown user:group file | Change who owns a file. |
sudo command | Run a command as the administrator. |
Processes and system
| Command | What it does |
ps aux | List running programs. |
top | Live view of CPU and memory use. Press q to quit. |
kill 1234 | Stop the process with id 1234. kill -9 forces it. |
df -h | Show free disk space. |
du -sh folder | Show how big a folder is. |
free -h | Show memory use. |
Network and downloads
| Command | What it does |
ping example.com | Check if a computer is reachable. |
curl https://example.com | Fetch a web page or call an API. |
ssh user@host | Log in to another computer securely. |
scp file user@host:/path | Copy a file to another computer. |
tar -czf a.tar.gz folder | Pack a folder into one compressed file. |
tar -xzf a.tar.gz | Unpack it. |
Handy tricks
| Command | What it does |
command1 | command2 | Send the output of one command into another. |
command > out.txt | Save output to a file. >> adds to the end. |
man ls | Open the manual for a command. |
history | Show the commands you typed before. |
Stay safe
Commands such as rm -r and anything with sudo can destroy data. Read the command twice before you press Enter, and never run commands you do not understand.
Frequently asked questions
What does chmod 755 mean?
The owner can read, write and run. Everyone else can read and run. Each digit adds 4 for read, 2 for write and 1 for run.
What is the difference between > and >>?
> replaces the file with the output. >> adds the output to the end of the file.
How do I stop a program that is stuck?
Press Ctrl+C in the terminal. If that fails, find its id with ps aux and use kill.
Test yourself: practice questions on this topic. More cheat sheets:
SQL Cheat Sheet · Git Cheat Sheet · Python Cheat Sheet
· Big-O cheat sheet