Show Menu
Cheatography

Linux Cheat Sheet (DRAFT) by

Linux commands that are useful or noteworthy

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Commonly Used

man
Inform­ation about a
-f: summary -k : search for words
cp
copy a to b
-R: recursive -p: keep permis­sions
mv
moves x to y
-f: force -b: create copy also
mkdir
makes directory
-p: also path to it -m: permis­sions
rm
removes file or folder
-r: recursive, -i: warns about each file
cd
-P:phy­sical -L:logical
cd -: previous cd ~: to home
df
file inform­ation
-h: human readible -i: metadata
ps
inform­ation on process
-a: all in terminal, -T: threads etc.
uniq
removes non-unique lines
eg: a.txt | unique | less
head / tail
displays first/last part of file
wc
inform­ation on text in file
-c: bytes, -w: words -l: lines
echo
repeat
-n: no linebreak, -e: interpet special chars

Pipes & Output

Pipe
cmd1 | cmd2 = Send stdout of cmd1 to cmd2

Redire­cting Output
Symbol Descri­ption
> Redirect stdout (overw­rite)
>> Redirect stdout (append)
2> Redirect stderr
2>> Append stderr
&> Redirect both stdout and stderr
2>&1 Redirect stderr to stdout

Special File Descri­ptors
FD Descri­ption
0 stdin
1 stdout
2 stderr
 

Diverse

uname
System­/kernel info
-a: all info, -r: kernel version
uptime
How long system been up
top / htop
system monitor
ifconfig
network interfaces
(older version)
ip
replaces ifconfig
ip a: addresses, ip r: routes
netstat
Show open ports and connec­tions
curl
Fetch data from URLs
wget
Download files
du
Disk usage
du -h: human-­rea­dable sizes, du -sh * for summary
kill
Terminate by PID
killall
Kill processes by name
e.g. killall firefox
tee
Output to screen and file simult­ane­ously
diff
Compare files line-b­y-line

Grep

grep str file
grep johan /etc/p­asswd
-i: case insens­itive
-v: show non-ma­tching lines
-q: silent, can be passed with $?
-r: search recurs­ively in folder
-w: search for complete word (not substr)
-E: search for two terms: grep -E "­ave­rag­e|m­ean­" file.txt

Find

find [options] [path...] [expre­ssion]
find -L /var/www -name ".js"*

-L : Follow symbolic links
-type f : search for file or [f: file, d:dir, l:symbolic link...]
-name <na­me>: name of file to search for
-iname <na­me>: name of file to search for (case-­ins­ens­itive)
 

Tar & gz

tar [options] [archi­ve-­file] [file(­s)/­dir­(s)...]
eg: tar -cvf archiv­e.tar file1 file2
eg: tar -xvf archiv­e.tar

-c : Create a new archive
-x : Extract files from an archive
-v : Verbose output (list files being processed)
-f <fi­le>: Use archive <fi­le> name
-z : Use gzip compre­ssion (for .tar.gz or .tgz)

Gzip Compre­ssion (.gz)
gzip file.txt Compress file.txt → file.t­xt.gz
gzip -k file.txt Compress but keep original file
gzip -r folder/ Recurs­ively compress all files in folder
gunzip file.t­xt.gz Decompress .gz back to original file

File Permis­sions

Permis­sions are defined for 3 groups:[ User ] [ Group ] [ Others ]

r = read (4)
w = write (2)
x = execute (1)
- = no permission (0)
Order: rwx

To set permis­sion: chmod [u][g][o] (eg: chmod 740 <fi­le> / chmod rwxr----- <fi­le>)

xargs

xargs – Turn Input Into Arguments

Example Command Descri­ption
find . -name "­*.l­og" | xargs rm Delete all .log files
echo "­file1 file2" | xargs ls -l Run ls -l on each file
cat list.txt | xargs -n 1 echo Print each line as argument separately
find . -name "­*.j­pg" | xargs -I{} mv {} images/ Move files with placeh­olders