Nexus Learn

Linux Commands Cheat Sheet

The terminal commands you will use most on Linux and macOS, explained in plain words. Type a command and press Enter. Use the Tab key to finish names.

Moving around

CommandWhat it does
pwdShow which folder you are in.
lsList files. Use ls -la to see hidden files and details.
cd folderGo into a folder. cd .. goes up one, cd ~ goes home.
clearClear the screen.

Files and folders

CommandWhat it does
mkdir -p a/bMake a folder (and any folders above it).
touch file.txtMake an empty file.
cp a.txt b.txtCopy a file. Use cp -r to copy a folder.
mv a.txt b.txtMove or rename a file.
rm file.txtDelete a file. There is no recycle bin.
rm -r folderDelete a folder and everything in it. Be very careful.

Reading and searching

CommandWhat it does
cat file.txtPrint a whole file.
less file.txtRead a long file page by page. Press q to quit.
head -n 20 file.txtShow the first 20 lines. tail shows the last lines.
tail -f app.logWatch 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.txtCount lines.

Permissions

CommandWhat it does
ls -lShow permissions as rwx for owner, group and others.
chmod +x script.shMake a file runnable.
chmod 644 file.txtOwner can read and write, everyone else can read.
chown user:group fileChange who owns a file.
sudo commandRun a command as the administrator.

Processes and system

CommandWhat it does
ps auxList running programs.
topLive view of CPU and memory use. Press q to quit.
kill 1234Stop the process with id 1234. kill -9 forces it.
df -hShow free disk space.
du -sh folderShow how big a folder is.
free -hShow memory use.

Network and downloads

CommandWhat it does
ping example.comCheck if a computer is reachable.
curl https://example.comFetch a web page or call an API.
ssh user@hostLog in to another computer securely.
scp file user@host:/pathCopy a file to another computer.
tar -czf a.tar.gz folderPack a folder into one compressed file.
tar -xzf a.tar.gzUnpack it.

Handy tricks

CommandWhat it does
command1 | command2Send the output of one command into another.
command > out.txtSave output to a file. >> adds to the end.
man lsOpen the manual for a command.
historyShow 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