Cheat sheet

Linux Commands Cheat Sheet for Beginners and Administrators

Updated: · Skillaria.Top

Basic Linux commands fall into groups: navigation (pwd, ls, cd), file work (cp, mv, rm, mkdir), permissions (chmod, chown), packages (apt), processes (ps, top, kill), search and text processing (find, grep, sed, awk), networking (ip, ss, curl) and services (systemctl, journalctl). Below are tables with commands and examples that cover most daily work in the terminal.

This Linux command cheat sheet is organized by tasks: find a file, install a package, kill a frozen process, pull a line out of a log, check a port. The commands are tested on Ubuntu and Debian; almost everything also works on other distributions, where the main difference is the package manager.

Help and hints

CommandWhat it does
man lsfull manual for a command; quit with q, search with /word
ls --helpshort summary of the flags
type cdwhat it is: a built-in command, an alias or a file
which python3path to the executable file
historycommand history; !123 repeats command number 123
Ctrl+Rsearch the history
Ctrl+C / Ctrl+Dinterrupt a command / close the shell
CommandWhat it does
pwdcurrent directory
ls -laall files, including hidden ones, with permissions and sizes
ls -lhsizes in human-readable form (K, M, G)
ls -ltsort by modification time
cd /var/loggo to an absolute path
cd .. / cd ~ / cd -one level up / home / previous directory
tree -L 2directory tree, two levels deep (package tree)

Working with files and directories

CommandWhat it does
touch file.txtcreate an empty file or update its modification time
mkdir -p a/b/ccreate directories together with their parents
cp file.txt backup.txtcopy a file
cp -r src/ dst/copy a directory recursively
mv old.txt new.txtrename or move
rm file.txtdelete a file (no trash bin)
rm -r dir/delete a directory with its contents; -f — without prompts, be careful
rmdir dirdelete an empty directory
ln -s /opt/app/current appsymbolic link
stat file.txtdetails: size, permissions, inode, dates
file image.bindetermine the file type from its content

Viewing contents

CommandWhat it does
cat fileprint the whole file
less filepage-by-page view, search with /, quit with q
head -n 20 filefirst 20 lines
tail -n 50 filelast 50 lines
tail -f /var/log/syslogfollow a file in real time
wc -l filenumber of lines
diff a.conf b.confdifferences between files

Permissions and owners

CommandWhat it does
chmod +x script.shallow execution
chmod 644 filerw-r--r--: the owner writes, others read
chmod 755 dirrwxr-xr-x: typical for directories and scripts
chmod 600 ~/.ssh/id_ed25519owner only: for private keys
sudo chown www-data:www-data filechange owner and group
sudo chown -R alice /srv/appchange owner recursively
idyour UID, GID and groups
sudo -iroot shell
sudo usermod -aG docker aliceadd a user to a group

Packages (apt, Debian and Ubuntu)

CommandWhat it does
sudo apt updaterefresh the package lists from the repositories
sudo apt upgradeupgrade installed packages
sudo apt install nginxinstall a package
sudo apt remove nginxremove, keeping configuration files; purge — together with configs
sudo apt autoremoveremove unneeded dependencies
apt search htopsearch for a package
apt show nginxdescription and version
dpkg -l | grep nginxcheck whether a package is installed
dpkg -L nginxwhich files the package installed

On RHEL, Fedora and Rocky Linux the equivalents are dnf install, dnf remove, dnf upgrade and rpm -qa.

Environment and profile

CommandWhat it does
echo $PATHwhere the shell looks for programs
envall environment variables
export APP_ENV=prodset a variable for the current session and child processes
alias ll='ls -la'a shortcut for a command
source ~/.bashrcreload the profile without logging out
crontab -e / crontab -ledit / show cron jobs

Processes

CommandWhat it does
ps auxall processes with user, CPU and memory
ps aux | grep nginxfind a process
pgrep -a nginxPID and command line by name
top / htopload in real time
kill 1234ask a process to terminate (SIGTERM)
kill -9 1234force-kill (SIGKILL) — last resort
pkill -f "python app.py"terminate by a match in the command line
command &, jobs, fgrun in the background and manage jobs
nohup ./job.sh &the process survives closing the terminal
free -hmemory and swap
uptimeuptime and average load
strace -p 1234system calls of a process

Services and logs (systemd)

CommandWhat it does
systemctl status nginxservice state and the last lines of its log
sudo systemctl start|stop|restart nginxstart, stop, restart
sudo systemctl reload nginxre-read the configuration without stopping
sudo systemctl enable --now nginxenable autostart and start now
systemctl --failedfailed units
journalctl -u nginx -fservice log in real time
journalctl -u nginx --since "1 hour ago"log for the last hour
journalctl -p err -berrors since the last boot

Finding files

CommandWhat it does
find /var/log -name "*.log"find by name
find . -type f -mtime -1files changed in the last day
find / -size +500M 2>/dev/nulllarge files, access errors hidden
find . -name "*.tmp" -deletefind and delete
du -sh *size of each item in a directory
df -hfree space on file systems

Searching and processing text: grep, sed, awk, jq

CommandWhat it does
grep "error" app.loglines containing the word
grep -i -n "error" app.logcase-insensitive, with line numbers
grep -r "listen" /etc/nginx/recursively through a directory
grep -v "DEBUG" app.loglines where the word is absent
grep -c "500" access.lognumber of matches
grep -E "warn|error" app.logextended regular expressions
sed 's/old/new/g' filereplace and print the result
sed -i 's/8080/9090/' app.confreplace directly in the file
sed -n '10,20p' fileprint lines 10–20
awk '{print $1}' access.logthe first field of each line
awk -F: '{print $1}' /etc/passwduser names (separator :)
awk '$9 == 500' access.loglines where the ninth field equals 500
sort | uniq -c | sort -rncount repeats and sort by frequency
cut -d, -f2 data.csvthe second column of a CSV
jq '.items[].name' data.jsonextract fields from JSON

Pipeline example: the ten IP addresses that sent the most requests to nginx.

awk '{print $1}' /var/log/nginx/access.log | sort | uniq -c | sort -rn | head -10

Redirection and pipelines

ConstructWhat it does
cmd > out.txtwrite the output to a file (overwrite)
cmd >> out.txtappend to the end of the file
cmd 2> err.txtredirect errors
cmd > all.log 2>&1output and errors into one file
cmd1 | cmd2pass the output of one command to the input of another
cmd | tee out.txtshow on screen and save
cmd1 && cmd2run the second only if the first succeeds
cmd1 || cmd2run the second if the first fails

Archives

CommandWhat it does
tar -czf backup.tar.gz dir/pack and compress with gzip
tar -xzf backup.tar.gzextract
tar -tzf backup.tar.gzlist the contents
zip -r site.zip site/ / unzip site.zipzip archives

Networking

CommandWhat it does
ip ainterfaces and IP addresses
ip rrouting table
ping -c 4 8.8.8.8host availability
ss -tulpnopen ports and the processes using them
dig example.com / nslookup example.comDNS query
curl -I https://example.comresponse headers only
curl -s https://api.example.com | jqAPI request with pretty-printed JSON
wget URLdownload a file
traceroute example.comthe path packets take to a host
ssh user@hostconnect to a server
ssh-keygen -t ed25519create an SSH key pair
scp file user@host:/tmp/copy a file to a server
rsync -avz dir/ user@host:/srv/dir/synchronize a directory
sudo ufw statusfirewall rules

System and disks

CommandWhat it does
uname -akernel and architecture
cat /etc/os-releasedistribution and version
hostnamectlhost name and system information
lsblkdisks and partitions
sudo mount /dev/sdb1 /mntmount a partition
who / wwho is logged in right now
sudo reboot / sudo shutdown -h nowreboot / power off

Summary

A couple of dozen commands cover everyday work: ls, cd, cp, mv, rm, chmod, sudo, apt, ps, kill, systemctl, journalctl, grep, find, ss, curl and ssh. Keep this Linux command cheat sheet at hand and check it until the commands become second nature. Every group of commands is covered step by step in our Linux course.

FAQ

How do I exit man or less?

Press q. To search inside, type a slash and the word; n jumps to the next match.

How do I exit the vim editor?

Press Esc, then type :q! and Enter to quit without saving, or :wq to save and quit. Beginners may find nano easier for editing files.

Why is rm -rf dangerous?

The command deletes without confirmation and without a trash bin, and a mistake in the path (for example, an extra space before the asterisk) can wipe the wrong directory. Check the path with ls before running it.

How do I repeat the last command with sudo?

Type sudo !! — the shell substitutes the previous command in full.

How do I stop a frozen command in the terminal?

Ctrl+C sends an interrupt signal. If the command does not respond, find its PID with ps or pgrep in another terminal and end it with kill.