Linux Cheat Sheet

A Cheat Sheet is a concise reference that provides guidance for executing specific tasks/commands.

Below you will find a Linux Cheat Sheet I prepared at the end of a training (also available in PDF format). If you’re a Linux Administrator it’s always useful to create your own cheat sheet.

Basic commands
cd <directory> Change to <directory>
ls, ls -F, ls -l List directory content
mkdir Create new directory
pwd Print current directory
mv Move/rename files/folders
rm Delete file
rm -Rf <dir> Delete <dir> and everything under
more, less, cat Look at files content
command > file Overwrite file with output of command
command >> file Append output of command to file
cmd1 | cmd2 Use output of command 1 as input of command 2
command < file Use file as input of command
chmod Change permissions
chown Change ownership
ln -s fileName linkName Create a symbolic link to a file
Process management
ps, ps -aux, ps -ef Display running processes
top Display all processes
Ctrl-C Stop current process
jobs Show launched processes
Ctrl-Z Suspend active process
bg Send process to background
fg Send process to foreground
bg %1 Send process number 1 to background
fg %2 Send process number 2 to foreground
kill Kill a process
kill -9 Force process to be killed
Archiving & compressing files
tar cvf bak.tar /etc /home Put /etc and /home in bak.tar
tar xvf bak.tar Extract bak.tar content
gzip bak.tar Compress bak.tar
gzip -9 bak.tar -9: use highest compression level
gunzip bak.tar.gz Decompress bak.tar.gz
Finding text in files
grep hello *.txt Find lines containing “hello” in .txt files
grep -v hello *.txt Find all lines except ones containing “hello”
grep -i HeLLo *.txt Ignore case
grep -R hello /home/* Find lines containing “hello” in all files under /home
grep -lR hello /home/* List only name of files containing “hello”
grep ‘[Ss]mug’ files Search for ‘Smug’ or ‘smug’ using regular expression
Email utilities
mail user@domain Send email to user
mutt Powerful text based email client
Managing users
adduser, useradd Add a new user
deluser, userdel Delete a user
passwd Change user password
Various network tools
nslookup, dig Query DNS servers
ping Test if server is alive
traceroute Display route trace to
nmap Show open ports for
Monitoring network traffic
netstat -an Shows all connections to the server
netstat -r Show routing table
netstat -s Display network statistics
ethereal Packet sniffer
User information & communication
who, users List of users logged into a machine
lastlog Most recent login of all users
write user1 Send a message to user1
Monitoring utilities
top Dynamic realtime view of a running system
iostat -dkx 2 I/O statistics for devices, partitions & NFS
vmstat 2 Processes, memory, swap, I/O, traps, cpu
dstat Generates system resources statistics
ps -aux / ps -ef Processes information
Some networking commands
ifconfig Display IP settings
ifconfig eth0 down Disable interface eth0
ifconfig eth0 up Enable interface eth0
ifconfig eth0:0 netmask Configure subinterface eth0:0
route -n Display current routing table
route add default gw Add default gateway
route del default gw Delete default gateway
route add -net netmask gw Send datagrams to gateway if their destination IP belongs to the network
route add -host gw If the destination address is send the IP datagram to gateway
ipcalc -n Compute network address for
ipcalc -b Compute broadcast address for
Searching for files
find /home -name “*.txt” 2>/dev/null Find all “.txt” files under /home
find /home -name “*.txt” -exec wc -l {} \; 2>/dev/null Find all “.txt” files under /home and execute wc -l on each file found
locate maps Find all files with containing the string “maps”
updatedb Update locate search DB, must be executed before locate in case file(s) has been moved/deleted/added
Package management – rpm & yum for Redhat / dpkg & apt-get for Debian
rpm -i package / dpkg -i package Installing package
rpm -e package / dpkg -r package Erasing package
rpm -qa / dpkg -l List all installed packages
yum install package / apt-get install package Installing package with all dependencies
yum remove package /apt-get remove package Removing package with all dependencies
yum search <string> / apt-cache search <string> Search for package names containing <string>
SSH – Remote login
ssh username@host Login to host with username
SSH Tunneling: Access email securely on
ssh -N -f -L 2110/localhost/110 Forward traffic from port 2110 on the local machine to port 110 on the remote machine
ssh -N -f -L 2025/localhost/25 Forward traffic from port 2025 on the local machine to port 25 on the remote machine
Pop setup : server = localhost, port = 2110
Smtp setup: server = localhost, port = 2025
Use this setup with your local email client in order to use encrypted ssh tunnel to send/receive email using
IN ONE COMMAND: ssh -N -f -L 2110/localhost/110 -L 2025/localhost/25
SSH automatic login with private/public keys
Under machine A execute: “ssh-keygen -t rsa”, leave password blank creates a private key ”id_rsa” and a public key ””
scp $HOME/.ssh/ user@remotemachine:/tmp/ Move to remote machine B
ssh user@remotemachinecat /tmp/ >> $HOME/.ssh/authorized_keys Connect to remote machine B and append to $HOME/.ssh/authorized_keys, “.ssh” permissions are at most 700, “authorized_keys” permissions are at most 600
Now you can connect to machine B without any password from machine A, try: ssh user@remotemachine
Surviving ssh disconnections
nohup <command> & Command will continue executing even if ssh connection is closed
screen Useful tool to create multiple “sub-sessions” which survives disconnections
Remote file transfer
scp Copy using ssl
scp file1 user@remotehost:/tmp/ Copy file1 under /tmp on remotehost
scp user@remotehost:/tmp/hello.txt /home/myuser Copy /tmp/hello.txt from remotehost to /home/myuser
scp -r /home/myuser/* user@remotehost:/back/ Copy recursively everything under /home/myuser/ to /back/ on remote host
rsync Delta-transfer algorithm, transfer only the differences
rsync -avz host:/home/bar /data/tmp Transfer /home/bar directory from host to /data/tmp/bar
rsync -avz host:/home/bar/ /data/tmp Transfer /home/bar files and dirs on host under /data/tmp
rsync -ave ssh /data/tmp/tempo user@machine:/tmp Use ssh to do the rsync, no need for an rsync daemon on server
rsync –delete -ave ssh /data/tmp/tempo user@machine:/tmp Removes deleted file on destination dir ectory, default is to keep files on destination directory even if deleted on source directory
awk -F: ‘{ print $1 ” ” $3 }’ /etc/passwd Prints user and userid from /etc/passwd
awk -F: ‘ BEGIN {users=0; grp=0}{ users++; if ($4 > 1000) grp++ } END { print “total users: ” users “, users in groups greater than 1000 : ” grp }’ /etc/passwd Computes number of total users and number of users in groups greater than 1000
ls /tmp | awk ‘{print “mv /tmp/”$1” /tmp/NEW”$1}’ | /bin/bash Append the string “NEW” to every file or folder under /tmp
Changing file content
sed “s/pattern1/pattern2/” inputfile > outputfile Replaces pattern1 with pattern2 once per line, result is saved in outputfile
sed “s/pattern1/pattern2/g” inputfile > outputfile Replaces pattern1 with pattern2 for every pattern1 per line
sed -i “s/pattern1/pattern2/” file Changes are made directly in file
sed “/pattern1/d” inputfile > outputfile Deletes lines containing pattern1
sed “y/string1/string2/” inputfile > outputfile Replaces characters in string1 with those in string2
./configure –help | less Check compiling options
./configure && make && make install Compiling from source
which command Finds command location
strings file1 Print the strings of printable characters in file1
lynx Command line browser like Firefox !
wget Downloads home page of
wget -r -l 0 Downloads the hole website, if there is no restrictions !
wget -r -A.pdf –no-parent http://url-to-webpage-with-pdfs/ Downloads all pdf files from a certain site
wget –post-data ‘example_name=testing’ Send a post request to some site
curl –data-urlencode “name=I am Daniel” Send a post request to and perform url encoding
curl -u name:password HTTP Authentication using name / password
sudo command execute command as root or another user
Prepared by Najib Abi Fadel

6 thoughts on “Linux Cheat Sheet

  1. Robertsor

    I’ve been browsing online more than 2 hours today, yet I never found any interesting article like yours. It is pretty worth enough for me. In my opinion, if all web owners and bloggers made good content as you did, the net will be much more useful than ever before.

  2. bottes ugg erin

    It`s really useful! Looking through the Internet you can mostly observe watered down information, something like bla bla bla, but not here to my deep surprise. It makes me happy..!

Leave a Reply

Your email address will not be published.