Linux cheat sheet
All 157 commands of the IVski Linux Playground on one page — what each does and an example, grouped by topic. Print it or save it as a PDF.
Getting Started
Navigation
Files & Directories
cat— concatenate files and print on the standard outputcat -n myfile.txtcp— copy files and directoriescp -i file.txt dest/ln— make links between filesln -s TARGET NAMEmkdir— make directoriesmkdir -p ~/site/v1 ~/site/v2mv— move (rename) filesmv -i file.txt dest/readlink— print resolved symbolic links or canonical file namesreadlink ~/system-logsrm— remove files or directoriesrm -I *.txtrmdir— remove empty directoriesrmdir emptydirectory/
Reading Files
diff— compare files line by linediff -i a.txt b.txtgrep— print lines that match patternsgrep -l 'word' *.txthead— output the first part of fileshead -1 data.csvless— display the contents of a file in a terminalless -N /etc/passwdtail— output the last part of filestail -n +2 data.csvwc— print newline, word, and byte counts for each filewc -l *.txt
Permissions
apt-get— APT package handling utility -- command-line interfacesudo apt-get updatechgrp—chgrp -R www-data /var/www/chmod— change file mode bitschmod -R 755 directory/chown— change file owner and groupchown -R student:student directory/getfacl— get file access control listsgetfacl /srv/reports/q3.txtgroupadd— create a new groupsudo groupadd devsgroups— print the groups a user is ingroupssetfacl— set file access control listssetfacl -d -m u:ana:rw /srv/reportsstat— display file or file system statusstat file.txtsu— run a command with substitute user and group IDsu -touch— change file timestampstouch draft.txtumask— Display or set file mode mask.umask -S
Text Processing
awk— pattern scanning and processing languageawk -F, '{print $2}' data.csvcommand— Execute a simple command or display information about commands.command > file.txtcut— remove sections from each line of filescut -c5- file.txtjq— Command-line JSON processorjq -r '.servers[].name' ~/json/servers.jsonsed— stream editor for filtering and transforming textsed -n '5p' file.txtsort— sort lines of text filessort -r file.txttr— translate or delete charactersecho 'hello' | tr 'a-z' 'A-Z'uniq— report or omit repeated linessort file.txt | uniq
Processes
bg— Move jobs to the background.bg %1disown— Remove jobs from current shell.disown %1dmesg— print or control the kernel ring buffersudo dmesg | grep -i "killed process"fg— Move job to the foreground.fgjobs— Display status of jobs.jobskill— Send a signal to a job.kill -lkillall— kill processes by namekillall nginxnice— run a program with modified scheduling prioritynice -n 19 tar -czf ~/docs.tar.gz ~/documentsnohup— run a command immune to hangups, with output to a non-ttynohup command &pgrep— look up, signal, or wait for processes based on name and other attributespgrep nginxpkill— look up, signal, or wait for processes based on name and other attributespkill -u studentps— report a snapshot of the current processes.ps -efpstree— display a tree of processespstreerenice— alter priority of running processesrenice -n 10 -p 3412sleep— delay for a specified amount of timesleep 5top— display Linux processestop -b -n 1 | head -20watch— execute a program periodically, showing output fullscreenwatch -n 5 free -h
Users & Groups
gpasswd— administer /etc/group and /etc/gshadowsudo gpasswd -a user groupgroupdel— delete a groupsudo groupdel developersid— print real and effective user and group IDsidlast— display login, logout and reboot informationlastlastlog—lastlogpasswd— change user passwordpasswduseradd— create a new user or update default new user informationsudo useradd -m -s /bin/bash -c "Full Name" usernameuserdel— delete a user account and related filessudo userdel -r usernameusermod— modify a user accountsudo usermod -L usernamew— Show who is logged on and what they are doing.wwho— show who is logged onwho
Networking
curl— transfer a URLcurl -L https://short.urldig— DNS lookup utilitydig google.comhostname— show or set the system's host namehostname -Iifconfig— configure a network interfaceifconfigip— show / manipulate routing, network devices, interfaces and tunnelsip addrnetstat— Print network connections, routing tables, interface statistics, masquerade connections, and multicast membershipsnetstat -tlnpnslookup— query Internet name servers interactivelynslookup google.comping— send ICMP ECHO_REQUEST to network hostsping -i 2 hostpython3— an interpreted, interactive, object-oriented programming languagecurl -s https://jsonplaceholder.typicode.com/posts/1 | python3 -m json.toolss— another utility to investigate socketsss -tlnpwget— The non-interactive network downloader.wget -i urls.txt
Archives & Backups
gunzip— compress or expand filesgunzip file.txt.gzgzip— compress or expand filesgzip -k file.txtrsync— a fast, versatile, local/remote file-copying toolrsync -av ~/site/ ~/site-copy/tar— an archiving utilitytar -xvf archive.tarunzip— list, test and extract compressed files in a ZIP archiveunzip -l archive.zipzip— package and compress (archive) fileszip -9 archive.zip files/
Disk & Memory
blkid— locate/print block device attributessudo blkid -s UUID -o value /dev/sdb1df— report file system space usagedf -hdu— estimate file space usagedu -hfallocate— preallocate or deallocate space to a filesudo fallocate -l 2G /swapfilefdisk— manipulate disk partition tablesudo fdisk -l /dev/sdbfindmnt— find a filesystemsudo findmnt --verifyfree— Display amount of free and used memory in the systemfree -mlsblk— list block deviceslsblkmkfs.ext4— create an ext2/ext3/ext4 file systemsudo mkfs.ext4 -L data /dev/sdb1mkswap— set up a Linux swap areasudo mkswap /swapfilemount— mount a filesystemsudo mount -ancdu— NCurses Disk Usagencdu /varreboot— Power off, reboot, or halt the machinesudo rebootswapon— enable/disable devices and files for paging and swappingswapon --showumount— unmount filesystemssudo umount /srv/datavmstat— Report virtual memory statisticsvmstat 1 5
Shell & Environment
alias— Define or display aliases.alias ll='ls -la'env— run a program in a modified environmentenvexport— Set export attribute for shell variables.export EDITOR=nanohistory— Display or manipulate the history list.history -cprintenv— print all or part of environmentprintenvsource— Execute commands from a file in the current shell.source ~/.bashrctype— Display information about command type.type lsunalias— Remove each NAME from the list of defined aliases.unalias llwhereis— locate the binary, source, and manual page files for a commandwhereis lswhich— locate a commandwhich ls
Shell Scripting
bash— GNU Bourne-Again SHellbash myscript.shbreak— Exit for, while, or until loops.breakcontinue— Resume for, while, or until loops.continueecho— Write arguments to the standard output.echo "files in /etc: $(wc -l < "$TMP")"exit— Exit the shell.exit 1local— Define local variables.local src="$1"mktemp— create a temporary file or directorymktemp -dset— Set or unset values of shell options and positional parameters.set -esh— command interpreter (shell)sh myscript.shshift— Shift positional parameters.shifttrap— Trap signals and other events.trap -p
Advanced Commands
crontab— maintain crontab files for individual users (Vixie Cron)crontab -edd— convert and copy a fileddfind— search for files in a directory hierarchyfind . -type fnano— Nano's ANOther text editor, inspired by Piconano -l file.txtscp— OpenSSH secure file copyscp -r directory/ user@host:/ssh— OpenSSH remote login clientssh -p 2222 username@hostnamessh-copy-id— use locally available keys to authorise logins on a remote machinessh-copy-id username@hostnamessh-keygen— OpenSSH authentication key utilityssh-keygen -t ed25519 -C "your@email.com"vim— Vi IMproved, a programmer's text editorvim filename.txtxargs— build and execute command lines from standard inputxargs -n 1 command
Package Management
Git & Version Control
git— the stupid content trackergit add -A
Services & systemd
journalctl— Print log entries from the systemd journaljournalctl -rsystemctl— Control the systemd system and service managersystemctl status sshdsystemd-analyze— Analyze and debug system managersystemd-analyze calendar 'Mon..Fri 09:00'
Server Setup
hostnamectl— Control the system hostnamehostnamectllogrotate— rotates, compresses, and mails system logssudo logrotate -d /etc/logrotate.d/myappprintf— Formats and prints ARGUMENTS under control of the FORMAT.printf 'Unattended-Upgrade::Automatic-Reboot "true";\nUnattended-Upgrade::Automatic-Reboot-Time "02:00";\n' | sudo tee /etc/apt/apt.conf.d/52auto-reboottimedatectl— Control the system time and datetimedatectlunattended-upgrade— automatic installation of security (and other) upgradessudo unattended-upgrade -v
Web Server (nginx)
Server Security
certbot— Automatically configure HTTPS using Let's Encryptsudo certbot --nginx -d aroma.example -d www.aroma.examplefail2ban-client— configure and control the serversudo fail2ban-client statussshd— OpenSSH daemonsudo sshd -tufw— program for managing a netfilter firewallsudo ufw status
Databases (MariaDB)
mariadb— the MariaDB command-line tool (mysql is now a symlink to mariadb)mariadb -u shop -p shopmariadb-dump— a database backup program (mariadb-dump is now a symlink to mariadb-dump)sudo mariadb-dump shop > ~/shop.sql