Cheat sheet: Essential Linux commands

The commands a working administrator uses daily, grouped by job: files, text, permissions, processes, networking and services.

Find your way around

Command What it does
pwd Show the current folder
ls -lah List files with sizes, permissions and hidden files
cd /var/log Change folder (cd ~ home, cd - previous folder)
tree -L 2 Show a folder as a tree, two levels deep
find / -name "*.conf" 2>/dev/null Find files by name
locate name Fast find from a prebuilt index

Work with files

Command What it does
cp -a src dst Copy, keeping permissions and structure
mv old new Move or rename
rm -r folder Delete a folder and what is inside it (there is no recycle bin)
mkdir -p a/b/c Make nested folders
touch file Create an empty file or update its time
ln -s target link Make a symbolic link
tar -czf out.tgz folder Make a compressed archive (-xzf to extract)
df -h Free space on each disk
du -sh * Size of each item in the current folder

Read and search text

Command What it does
cat file Print a file
less file Page through a file (q quits, /word searches)
head -n 20 file and tail -n 20 file First or last lines
tail -f /var/log/syslog Follow a log as it grows
grep -rin "error" /var/log Search text, recursively, ignoring case
wc -l file Count lines
sort file | uniq -c | sort -rn Count and rank repeated lines
cut -d: -f1 /etc/passwd Take one column from delimited text
sed 's/old/new/g' file Replace text in a stream

Permissions and users

Permissions are three groups (owner, group, others) of read (4), write (2) and execute (1).

Command What it does
chmod 640 file Owner read and write, group read, others nothing
chmod +x script.sh Make a script executable
chown user:group file Change owner and group
id Who am I and which groups am I in
sudo command Run one command as administrator
useradd -m -s /bin/bash name Create a user with a home folder
passwd name Set a password
usermod -aG sudo name Add a user to a group (the -a matters: without it you replace the groups)

Processes

Command What it does
ps aux List all processes
top or htop Live view of CPU and memory use
kill PID Ask a process to stop (kill -9 PID forces it)
pgrep -a name Find a process by name
free -h Memory use
uptime How long it has been up, and the load

Services (systemd)

Command What it does
systemctl status nginx Is it running, and its latest log lines
systemctl restart nginx Restart it
systemctl enable --now nginx Start now and at every boot
journalctl -u nginx -n 50 The last 50 log lines for a service
journalctl -p err -b Errors since the last boot

Networking

Command What it does
ip a Show addresses on each interface
ip r Show the routing table
ping -c 4 host Test reachability
ss -tulpn Which programs listen on which ports
dig example.com DNS lookup
curl -I https://example.com Fetch just the web headers
traceroute host Show each hop on the way
ssh user@host Remote shell
scp file user@host:/path Copy a file over SSH

Packages

Debian and Ubuntu Red Hat and Fedora
sudo apt update sudo dnf check-update
sudo apt install pkg sudo dnf install pkg
sudo apt upgrade sudo dnf upgrade
apt search word dnf search word

Habits that prevent outages

  • Read the command twice before pressing Enter, especially anything with rm, chmod -R or > (which overwrites).
  • Make a copy before you edit a config: cp file file.bak.
  • Test a change on one machine before all of them, and write down what you changed.