I think that it doesn’t matter what operating system you use — as long as you know your OS of choice well! This is a Linux command cheat sheet covering a wide range of topics. I cannot guarantee that the information is fully up-to-date or even correct. Use at own risk . It is intended primarily as a reference for myself in the future. I have learned most of the material covered below a couple of years ago in the LinuxFoundationX’s Introduction to Linux course offered through edx.org.

Table of contents

The most basic operations

Power off / reboot: shutdown -h now , halt , poweroff , reboot , shutdown -r now

, , , , Most basic navigation: pwd , cd , cd ~ , cd .. , cd - , ls , ls -a , ls -li , tree

, , , , , , , , Most basic file management: mv , cp , rm -f , rm -i , rm -r , rmdir <empty_dir>

, , , , , Date and time: date , cal , cal -y

Create files and directories:

$ touch <file> $ touch -t 1703270159 <file> $ mkdir <path/dir>

Create a hard / soft (symbolic) link: ln / ln -s

/ Create a temporary file or directory:

$ TEMP = $( mktemp /tmp/tempfile.XXXXXXXX ) $ TEMPDIR = $( mktemp -d /tmp/tempdir.XXXXXXXX )

I/O redirection

Assume that do_something reads from stdin and writes to stdout and stderr .

Get the input from a file using < .

. Send the output to a new file using > .

. Append to an existing file using >> .

Example:

$ do_something < input-file $ do_something > output-file $ do_something 2> error-file $ do_something > all-output-file 2>&1 $ do_something > & all-output-file

Pipe the output of one command or program into another as its input:

$ command1 | command2 | command3

Search

Locate applications: which , whereis

, Update database, and search for a character string in the database with updatedb and locate . E.g., to list all files and directories with both “zip” and “bin” in their name: $ updatedb $ locate zip | grep bin

and . E.g., to list all files and directories with both “zip” and “bin” in their name: Locate files recursively from a given directory: find E.g., searching only for regular files named “test1” in /usr : $ find /usr -type f -name test1 E.g., search based on file size or time stamp: $ find / -size +10M $ find / -ctime 3 find uses wildcards such as ? , * , [set] , [!set] (where “set” is a set of letters). Run commands on the found files with the -exec option. E.g., to find and remove all files that end with .swp in current directory: $ find -name "*.swp" -exec rm {} ’ ; ’

Use grep to search for a pattern in a file and print all matching lines. Examples: $ grep [ pattern] <filename> $ grep -C 3 [ pattern] <filename>

Viewing text files

cat for short files, no scroll-back.

for short files, no scroll-back. tac to look at a file backwards.

to look at a file backwards. Concatenate multiple files and display the output: $ cat <file1> <file2> $ tac <file1> <file2> <file3>

less for larger files; use / and ? for forward and backward search.

for larger files; use and for forward and backward search. Print the first n or the last n lines of a file: $ head -n $ tail -n

or the last lines of a file: Monitor new output in a growing file: $ tail -f

Create and fill text files

Create a file and fill it with content: $ echo line one > myfile $ echo line two >> myfile $ echo line three >> myfile or $ cat << EOF > myfile > line one > line two > line three > EOF

From existing text files: $ cat file1 file2 > newfile $ cat file >> existingfile

More text utilities

Sort the lines in file alphabetically: $ sort <filename>

Remove consecutive duplicate lines from file: $ uniq <filename>

Split a file into 1000-line segments: $ split <infile> <prefix>

Count lines, words, and characters in a file: $ wc <filename>

Print or join files by column (field): awk , paste , join , cut

, , , Miscellaneous text utilities: sed , tr , tee , strings

Comparing and patching files

Show the file type of a file: $ file <filename>

Compare two files: $ diff <filename1> <filename2>

Compare two files to a common file: $ diff3 <filename1> <commonfile> <filename2>

Produce a patch file: $ diff -Nur oldfile newfile > patchfile

Apply a patch file: $ patch -p0 < patchfile or $ patch file patchfile

Postscript and PDF files

Convert bar.txt to foo.ps : $ enscript -p foo.ps bar.txt

to : View the details of a PDF file: $ pdfinfo <filename>.pdf

Converting between PostScript and PDF: $ ps2pdf <filename>.pdf $ pdf2ps <filename>.ps $ epstopdf <filename>.eps <filename>.pdf

pdftk is the Swiss Army knife of PDF tools. Usage examples: Merge 1.pdf and 2.pdf , and save as 12.pdf : $ pdftk 1.pdf 2.pdf cat output 12.pdf Save pages 1 and 2 of 1.pdf to new.pdf : $ pdftk A = 1.pdf cat A1-2 output new.pdf Rotate all pages of in.pdf 90 deg. clockwise, and save as out.pdf : $ pdftk in .pdf cat 1-endeast output out.pdf Encrypt a PDF file: $ pdftk public.pdf output private.pdf user_pw PROMPT

is the Swiss Army knife of PDF tools. Usage examples:

Viewing linux documentation

man to search, format, and displays the manual pages. Examples: man -f displays a one-line manual pages descriptions: $ man -f printf printf ( 1 ) - format and print data printf ( 3 ) - formatted output conversion or equivalently, $ whatis printf printf ( 1 ) - format and print data printf ( 3 ) - formatted output conversion

displays a one-line manual pages descriptions: The section number can be supplied: $ man 3 printf

man -k shows all man pages that discuss a specified subject: $ man -k ruby erb ( 1 ) - Ruby Templating erb2.3 ( 1 ) - Ruby Templating gem ( 1 ) - frontend to RubyGems, the Ruby package manager ( ... ) GNU Info System: $ info $ info <topic name> Every command has a --help or -h option.

File ownership and permissions

Change user ownership: $ chown <owner> <filename>

Change group ownership: $ chgrp <group> <filename>

Change the permissions on a file: $ chmod <permissions> <file>

User accounts and groups

Identify currently logged-on users: $ who $ who -a $ whoami

Add / Remove a user: useradd / userdel

/ Set the initial password for a new user: $ passwd <username>

Display information about a user: $ id <user> $ groups <user>

Add / Remove a group: groupadd / groupdel

/ Add a user to a group: $ groupmod -G <group> <user>

Grant root privileges to user temporarily: $ su $ sudo < command >

Show last time each user has logged into the system: $ last

Environment variables and aliases

View the values of currently set environment variables: $ set $ env $ export

Show the value of a specific variable: $ echo $VARIABLE

Export a new variable value: $ export VARIABLE = value

E.g., to prefix a private bin directory to your path: $ export PATH = $HOME /bin: $PATH

List currently defined aliases: $ alias

Create an alias, e.g.: $ alias vi = 'vim'

Filesystems

Attach a filesystem: $ mount <device node> <mount point> $ mount /dev/sda5 /home

Display information about mounted filesystems: $ df -Th $ mount $ fdisk -l

NFS (Network Filesystem) On the Server: Start the NFS: $ sudo service nfs start Modify /etc/exports . Example entry: /projects * .example.com ( rw ) ` After modifying the /etc/exports file run: $ exportfs -av On the client: Mount the remote filesystem: $ mount servername:/projects /mnt/nfs/projects Or modify /etc/fstab . Example entry: servername:/projects /mnt/nfs/projects nfs default 0 0 `

The proc Filesystem Some important files in /proc are: /proc/cpuinfo , /proc/interrupts , /proc/meminfo , /proc/mounts , /proc/partitions , /proc/version . /proc also has subdirectories, such as /proc/<Process-ID-#> and /proc/sys .

Filesystem

Processes

All processes / all processes and all threads / all processes for all users:

$ ps -ef $ ps -eLf $ ps aux

Process tree:

$ pstree

Proc list with updates in real time:

$ top $ htop

(press A to sort when using top )

Terminate a process:

$ kill -SIGKILL <pid> $ kill -9 <pid>

View the background processes in the current terminal:

$ jobs -l

Suspend a foreground process: CTRL-z

Cancel a foreground process: CTRL-c

Move process to the background / foreground:

$ bg $ fg

Schedule future non-interactive proc, e.g.:

$ at 11 am may 20 at> echo Hello! > hello.txt at> <CTRL-D> $ $ at now + 3 minutes at> mkdir dirfrom3minutesago at> <CTRL-D> $

Schedule periodic background work:

$ crontab -e

Suspend execution (suffix = s , m , h , d ):

$ sleep <number><suffix>

Backing up data

Synchronize directory trees with rsync , which copies only the differences between directories. Examples:

$ rsync -avP --delete dir1/ dir2 $ rsync -avPe ssh --delete dir / user@host:/path/to/dir

Test the rsync command using the --dry-run option.

Compressed data and archives

Compressing data using gzip , bzip2 and xz There is an efficiency vs. speed trade-off. Ranked by space-efficiency: xz > bzip2 > gzip .

Replace each file in the current directory with its compressed version: $ gzip * $ bzip2 * $ xz *

Compress the file foo into foo.xz using the default compression level (-6), and remove foo if compression succeeds: $ xz foo

into using the default compression level (-6), and remove if compression succeeds: De-compress: $ gunzip foo $ bunzip2 * .bz2

Decompress bar.xz into bar and don’t remove bar.xz even if decompression is successful: $ xz -dk bar.xz Handling Files Using zip E.g., archive the login directory ( ~ ) and all files and directories under it as backup.zip : $ zip -r backup.zip ~

) and all files and directories under it as : Extracts all files in the file backup.zip : $ unzip backup.zip Archiving and Compressing Data Using tar Extract all the files in mydir.tar into the mydir directory: $ tar xvf mydir.tar

into the directory: Create the archive and compress with gzip / bz2 / xz : $ tar zcvf mydir.tar.gz mydir $ tar jcvf mydir.tar.bz2 mydir $ tar Jcvf mydir.tar.xz mydir

/ / : Extract all the files in mydir.tar.* into the mydir directory: $ tar xvf mydir.tar.gz Working with compressed data For gzip ‘ed files: zcat , zless , zgrep , zdiff .

‘ed files: , , , . For bzip2 ‘ed files: bzcat , bzless .

‘ed files: , . For xz ‘ed files: xzcat , xzless .

Network operations

Using Domain Name System (DNS) and Name Resolution Tools View IP and domain information about your system: $ hostname $ cat /etc/hosts $ cat /etc/resolv.conf View IP and domain information about linux.com: $ host linux.com $ nslookup linux.com $ dig linux.com $ dig +trace linux.com $ dig @8.8.8.8 linux.com ( 8.8.8.8 is Google’s recursive DNS server)

Network Interfaces and Configuration List all currently active network interfaces: $ ifconfig Show IP address of active network device: $ ip addr show Show routing info of active network device: $ ip route show Check the status of the remote host: $ ping < hostname >

Routing Tables / Routes Show current IP routing table: $ route -n Add/delete static route: $ route add -net address $ route del -net address Print the route taken by the packet to reach the network host at <domain> : $ traceroute <domain> Display all active connections and routing tables: $ netstat -r

Dump network traffic for analysis: $ tcpdump $ sudo tcpdump host google.com

Interaction with webpages Download a webpage: $ wget <url> Read or save the source code and other info of a URL: $ curl <url> $ curl -o saved.html http://www.mysite.com



Transferring files over the network

FTP (File Transfer Protocol) An example of connecting to the server and downloading a file: $ ftp -p some.server.com ftp> ls ftp> get somefile.txt ftp> quit $

SSH (Secure Shell) Log into remotesystem with username : $ ssh <username@remotesystem> Run my_command on a remote system via SSH: $ ssh <user@remotesystem> my_command Copy a local file to a remote system (similarly vice versa): $ scp <localfile> <user@remotesystem>:/home/user/ $ scp <user@remotesystem>:/home/user/somefile /local/dir/

