Cheatography
https://cheatography.com
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 |
Information about a |
-f: summary -k : search for words |
cp |
copy a to b |
-R: recursive -p: keep permissions |
mv |
moves x to y |
-f: force -b: create copy also |
mkdir |
makes directory |
-p: also path to it -m: permissions |
rm |
removes file or folder |
-r: recursive, -i: warns about each file |
cd |
-P:physical -L:logical |
cd -: previous cd ~: to home |
df |
file information |
-h: human readible -i: metadata |
ps |
information 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 |
information 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
Redirecting Output
Symbol Description
> Redirect stdout (overwrite)
>> Redirect stdout (append)
2> Redirect stderr
2>> Append stderr
&> Redirect both stdout and stderr
2>&1 Redirect stderr to stdout
Special File Descriptors
FD Description
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 connections |
curl |
Fetch data from URLs |
wget |
Download files |
du |
Disk usage |
du -h: human-readable sizes, du -sh * for summary |
kill |
Terminate by PID |
killall |
Kill processes by name |
e.g. killall firefox |
tee |
Output to screen and file simultaneously |
diff |
Compare files line-by-line |
Grep
grep johan /etc/passwd
-i: case insensitive
-v: show non-matching lines
-q: silent, can be passed with $?
-r: search recursively in folder
-w: search for complete word (not substr)
-E: search for two terms: grep -E "average|mean" file.txt
Find
find [options] [path...] [expression]
find -L /var/www -name ".js"*
-L : Follow symbolic links
-type f : search for file or [f: file, d:dir, l:symbolic link...]
-name <name>: name of file to search for
-iname <name>: name of file to search for (case-insensitive) |
|
|
Tar & gz
tar [options] [archive-file] [file(s)/dir(s)...]
eg: tar -cvf archive.tar file1 file2
eg: tar -xvf archive.tar
-c : Create a new archive
-x : Extract files from an archive
-v : Verbose output (list files being processed)
-f <file>: Use archive <file> name
-z : Use gzip compression (for .tar.gz or .tgz)
Gzip Compression (.gz)
gzip file.txt Compress file.txt → file.txt.gz
gzip -k file.txt Compress but keep original file
gzip -r folder/ Recursively compress all files in folder
gunzip file.txt.gz Decompress .gz back to original file |
File Permissions
Permissions are defined for 3 groups:[ User ] [ Group ] [ Others ]
r = read (4)
w = write (2)
x = execute (1)
- = no permission (0)
Order: rwx
To set permission: chmod [u][g][o] (eg: chmod 740 <file> / chmod rwxr----- <file>) |
xargs
xargs – Turn Input Into Arguments
Example Command Description
find . -name "*.log" | 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 "*.jpg" | xargs -I{} mv {} images/ Move files with placeholders |
|