Skip to content

Basics

This cheat sheet-style guide provides a quick reference to commands and practices commonly used when working Command Line (CLI). This cheat sheet mostly uses bash or zsh.

explainshell demystifies the complex CLI commands. cheat.sh and tldr.sh are also amazing resources for references.

  • man ‑ By default the system documentation/manuals are documented in the man pages
Terminal window
man <COMMAND>
  • help ‑ Lists all the internal commands of shell
Terminal window
help
  • ~/.bashrc ‑ configure the shell, define command aliases and set command shell options
  • ~/.bash_profile ‑ initialization commands that set environment variables, a shell’s prompt
  • ~/.bash_logout ‑ cleanup operations and other commands that you want the shell to execute whenever a user logs out of a shell
  • ~/.bash_aliases ‑ if storing many aliases commands
Terminal window
set -x # Enable RAW i/o
set -v # Enable RAW i/o
set -e # abort on error(non‑zero exit code)
set -u # detect unset variables
variable=${VARIABLE_NAME:-default} # default value if variable empty
$(( (1+1)/5 )) # Arithmetic operation
${1..10} # Sequence
${var%suffix} # trim a suffix named 'suffix'
${var#prefix} # trim a prefix named 'prefix'
${foo,bar} # expand into multiple values
diff /etc/hosts <(ssh somehost cat /etc/hosts) # parenthesis to execute a sub‑process
{..} # Include code in between this block, as a good practice
cat <<EOF
touch somefile
echo foo > somefile
EOF
#!/bin/bash
set -euo pipefail
IFS=$'\n\t'

File Descriptors ‑ every file has an associated number called FD. Default ones are:

  • 0 ‑ STDIN
  • 1 ‑ STDOUT
  • 2 ‑ STDERR

Types of redirection

  • > ‑ STDOUT redirection to file, overwrites at destination
Terminal window
ls -lrt > test.out
  • >> ‑ STDOUT redirection to file, appends at destination
Terminal window
ls -lrt >> test.out
  • < ‑ STDIN redirection from a file
Terminal window
wc < input_file.out
  • File Descriptors based redirection
Terminal window
ls -lrt 2>&1
  • | ‑ redirects STDOUT of one command into the STDIN of a second command
Terminal window
ls -lrt | wc
  • Ctrl + a ‑ Go to beginning
  • Ctrl + e ‑ Go to end
  • Alt + b ‑ Go back one word
  • Alt + f ‑ Go forward one word
  • Ctrl + w ‑ Delete a word backward
  • Alt + d ‑ Delete next word
  • Ctrl + u ‑ Delete to beginning of line OR delete line
  • Ctrl + k ‑ Delete to end
  • Ctrl + r ‑ Search backwards (recursive)
  • Ctrl + s ‑ Search forwards
  • Ctrl + l ‑ Clears the screen
  • Alt + t ‑ Transpose the last 2 words/characters
  • & ‑ background process.
  • Ctrl+z ‑ pauses the process temporarily, and places it in SUSPENDED mode.
  • fg ‑ used to bring a background / paused process back to the foreground and continue its execution.
  • bg ‑ resume any SUSPENDED jobs and run them in background
  • jobs ‑ check the job status of all the jobs
  • kill ‑ used for killing a process

Status: SUSPENDED, CONTINUED, RUNNING, STOPPED

Terminal window
sleep 100 & # Run the command as a background process
jobs # view all the jobs
jobs -l # view all the jobs along with their job ID & process PID
fg %<JOB_ID> # Resume the job in foreground which has the JOB_ID
bg %<JOB_ID> # Resume the job in the background which has the JOB_ID
# while a job is running:
Ctrl+z # Pauses executing of the current running job
Ctrl+c # terminates the current executing process
Terminal window
# --color = enabling coloured output
# -l = long listing
# -h = human readable Listing of data
# -t = sort by time
# -R = Recursive listing of directories
# Must use aliases – `.bash_aliases`
alias ls='ls -A -F -B --human --color'
alias ll='ls -l'
alias la='ls -A'
alias lh='ls -h'
ls -lrt
Terminal window
# -N = Line number enabled
# +F = Real Time Monitoring
sudo dmesg | less +F
cat test.txt | less
Terminal window
# -n = number of lines to display from the start
# -c = number of bytes to be displayed
head file.txt
head -n 10 file.txt
head -c 150 file.txt
Terminal window
# -f = real time updates as more content is appended to the end of file.
# -n = number of lines of tail to be displayed
tail file.txt
tail -f file.log
Terminal window
# -s = softlink/symlink
ln sourceFileForHardLink hardLinkFileNameWithPath
ln -s sourceFileForHardLink softLinkFileNameWithPath
  • User‑Group‑Others (--- --- ---)
  • Read‑Write‑Execute (rwx)
Terminal window
# -R = Recursive
# -V = Verbose
chmod +x file.txt
chmod 600 file.txt
chmod u-w file.txt
chmod u+x file.txt
Terminal window
# -R = Recursive
# -V = Verbose
chown user:group directory
Terminal window
# -h = human readable
du -h
Terminal window
# -h = human readable
# -i = inode listing
# --total = total summary
# --output = specify the output strategy
df -h /proc
df -h --output=source,avail,pcent,target
  • Data structures that keep track of all the files in the filesystem
  • all unique inode in a given filesystem
  • stores metadata (type, size, group, user, permission, access, change & modification time)

ps – lists all the processes which are running.

Section titled “ps – lists all the processes which are running.”
Terminal window
# -A or -e = list all the processes running for all users
# -T = processes running with the current terminal(tty)
# -f = full listing for the process
# -u = filter process for a specific user
# -L = List threads for a process PID
# -G = List process associated with a group
# -C = search process by name as PID is not known
# -o = specifies the format for the columns
ps -ef
ps aux
ps -L <PID>

nohup – run process in the background, even when session is logged out, “no hangup”: prevents from receiving SIGHUP signals

Section titled “nohup – run process in the background, even when session is logged out, “no hangup”: prevents from receiving SIGHUP signals”
Terminal window
nohup ./example.sh > output.log 2>&1 &
nohup python example.py &

disown – delete/remove jobs so that it doesn’t send SIGHUP signal

Section titled “disown – delete/remove jobs so that it doesn’t send SIGHUP signal”
Terminal window
# -a = removes all the jobs
# -h = marks job so that it DOESN’T send SIGHUP when shell receives a SIGHUP signal
# -r = deletes running job
disown -r
disown -h %2
disown -a
Terminal window
# -l = lists all the SIG for kill
# 1 = HUP – reload a process
# 9 = KILL – Force kill a process
# 15 = TERM – Graceful kill of process
kill PID_NUMBER
kill -9 PID
Terminal window
pandoc README.md --from markdown --to docx -o tmp.docx

sort – alphabetical (lowercase < uppercase) / increasing sorting by default

Section titled “sort – alphabetical (lowercase < uppercase) / increasing sorting by default”
Terminal window
# -r = reverse order
# -n = recognise numerical results
# -k = sort based on specific column
# -u = remove duplicate entries
# -c = check if the file is in sorted format
# -M = arrange based on months
# -s = stable sort
sort -nr numeric.txt
ls -l | sort -nk 5
sort -c test.txt

uniq – remove duplicate entries, must be in sorted format

Section titled “uniq – remove duplicate entries, must be in sorted format”
Terminal window
# -c = count frequency
# -d = print only the lines which are duplicated
# -u = print all unique lines
# -i = ignore case
sort file2 | uniq -c
sort file2 | uniq -u
sort file2 | uniq -cd
Terminal window
# -f = field to cut
# -d = delimiter to use for cutting
cut -d: -f1,6 /etc/passwd
ls -lrt | cut -d ' ' -f 1
Terminal window
# -l = number of lines
# -w = number of words
# -m = number of characters
wc -l
Terminal window
# -C = Complement character
# -d = delete character
tr A‑Z a‑z
tr -d 'e'
Terminal window
sed s/Amazing/super/gi textfile.txt
sed s/originalword/replaceword/g filename
sed -n '3,5p' textfile.txt
Terminal window
# -v = verbose
# -r = recursive
# -z = compress during transfer
# -h = human‑readable
# -P or --progress = shows real‑time progress
rsync -avzhe ssh --progress /root/pkgs root@127.0.0.1:/root/pkgs
rsync -zvh backup.tar.gz /tmp/backups/
Terminal window
# -o = save data to a file
# -L = follow redirect
# -C = resume an interrupted download
# -u = authentication
# -v = verbose
# --headers = specify request headers
# -d or --data = POST request body
# -X = specify the request type
# -k = disable certificate checking
curl -o logo.png https://reqbin.com/static/img/logo.png
curl --header 'Host: targetapplication.com' https://192.0.0.1:8080/
curl -X POST https://httpbin.org/post

Operations with compressed files – zless, zmore, zcat, zgrep

Section titled “Operations with compressed files – zless, zmore, zcat, zgrep”
Terminal window
nc -lv 1234 # start a listener on port 1234
nc -zv google.com 443 # check if port is open
nc -zv 10.0.2.4 1230‑1235 # scanning a port range
telnet host.com 1234 # connect to the port in the HOST
netstat -an | grep LISTEN