Linux bash cheat sheet

Some definitions

Linux is an open-source operating system. It comes under different “flavours” or distributions such as Ubuntu, Arch Linux, Fedora, etc…

Bash – for Bourne Again SHell – is Linux’ command line interpreter (or shell) that provides a command line interface inside a terminal emulator.


Cheat sheet

Find files

Find files match a pattern

find -name "query"

Find files match a pattern, case insensitive

find -iname "query" 

Find all files that don’t contain the pattern

find -not -name "query_to_avoid"

Find all files in current directory

find . -type f

Find all directories in current directory

find . -type d

Find all symbolic links in current directory

find . -type l 

Find all items ending in “.conf” in current directory

find . -name *.conf

Find all files in the current directory starting with “pro”

find . -name pro* 

Find all items containing “yolo” in current directory

find . -name *yolo*

Find pattern ‘tensorflow’ in python files in the current directory

find . -name *.py | grep -rnw tensorflow 

Option explained:
-r or -R : recursive
-n : show line number
-w : match the whole word.
-l (lower-case L) : give the file name of matching files (optional).

Find pattern in file recursively

grep -rnw '/path/to/somewhere/' -e 'pattern'
grep -r 'pattern'

Move file with extension recursively to a directory

find /thisdir -type f -name '*.ogg' -exec mv -i {} /somedir \;

Backup installed packages

Write list of packages

sudo dpkg --get-selections "*" > my_packages.txt

Install list of packages from txt file

sudo dpkg --set-selections < my_packages.txt
sudo apt-get -u dselect-upgrade

Command line history

Search command line history

ctrl + r

View command line history


Copy to/from SSH server

When logged in locally, copy from local to remote

scp /file/to/send username@remote:/where/to/put
rsync /file/to/send username@remote:/where/to/put

When logged in locally, from remote to local

scp username@remote:/file/to/send /where/to/put
rsync username@remote:/file/to/send /where/to/put

When logged in remotely, copy from remote to local

sftp username@remote
get -r "remote/dir/path" "local/dir/path"

When logged in remotely, copy from local to remote

sftp username@remote
put -r "local/dir/path" "remote/dir/path"

(note: in Ubuntu, you may encounter this error: “Couldn’t canonicalise: No such file or directory”.
solution: create a directory on the remote first using mkdir)

Launch a job on an SSH server and be able to log off without killing the job

nohup python >& &

File ownership / make executable

Change directory ownership

chown -R user:group /directory/of/interest

Make script executable

chmod +x "file name"

System specs

Disk usage of the entire filesystem

df -h --total
df -h

Disk usage of current directory

du -sh .

Show disk usage of the 40 biggest subfolders in the current directory (ranked)

du -hsx * | sort -rh | head -n 40

Show disk usage of all the subfolders in the current directory one by one

du -hsx *

Show disk usage of the biggest subfolders in the current directory (ranked), with hidden directories

du -sch .[!.]* * | sort -rh

In Ubuntu, this nice tool can explore the disk usage of your entire system quickly

sudo apt-get install ncdu

View system specs (and export them to a html file)

sudo lshw
sudo lshw -html > specs.html

Check Linux Kernel

uname -a

Check Ubuntu version

lsb_release -a

Print total, free and used RAM memory

free -h

List CPU and processor info


Print generic hardware information


Print PCI buses, including graphics card, network adapter


List block devices (storage and partitions)


List devices connected via USB

cd /dev

View running processes


Print the process ID of a running task

pidof firefox

Kill a running job

kill ‘process ID’

(find process ID using the ‘top’ command or using pidof)

Use SLURM (job scheduler for computing clusters)

Submit job


View queue

squeue -u "user name"

Resolve merging conflicts with Git

git mergetool --tool=emerge

It will open 3 windows: version a on top left, version b on top right and final version at the bottom.
– press n for next change
– press a or b to choose which version I want
– press q to quit and save

Manipulate PDF documents with pdftk

Cut pdf pages

pdftk full-pdf.pdf cat 12-15 output outfile_p12-15.pdf

Merge pdf pages

pdftk file1.pdf file2.pdf file3.pdf cat output newfile.pdf

Create video from images using ffmpeg

ffmpeg -i input_0%d.png -vcodec libx264 ouput.mp4
ffmpeg -i input_%d.png -filter:v "setpts=10.0*PTS" -vcodec libx264 ouput.mp4
ffmpeg -start_number 1 -i input%d.png -filter:v "setpts=2*PTS" -filter:v "crop=490:360:15:15" -r 48 -c:v libx264 -crf 0 -profile:v high444 -preset slow -b:v 15M ouput.mp4

Create gif from video using ffmpeg

ffmpeg -t 30 -i input.mp4 -vf "fps=10,scale=1280:-1:flags=lanczos,split[s0][s1];[s0]palettegen[p];[s1][p]paletteuse" -loop 0 output.gif

Create gif from images using convert

convert -resize 50% -delay 20 -loop 0 input_{0..99}.jpg output.gif

Other tricks

Useful shortcuts

Open a terminal new widow


Open a new terminal tab


Close the current terminal tab


Kill the current running process


Exit Python interpreter / log out from SSH


Copy-paste in terminal with a mouse

  1. select text
  2. move cursor where you want to paste
  3. paste with a middle mouse click

Insert the euro sign (€)


Foreground / background

Put task in background

ctrl + c 

Bring it back to the foreground


Nicely formated ls command

ls -lrt

Download from website