Bash tools
Awk
First and third column from file, seperated by spacesawk -F " " '{print $1,$3'} result.txt
Print from line 156 to line 170awk 'NR >= 156 && NR <= 170' log.txt
Cat
Copy Data to Other Filecat file1 > file2
Combine Files And Sort Alphabeticallycat file1 file2 | sort > file3
Create new file that consists of the contents of file1, followed by keyboard.cat file1 -> file2
Multi line to singe linecat multiline.txt | tr -d '\n' > oneline.txt
Cut
First and third column from file, seperated by spacescut -d' ' -f1,3 result.txt
Find
Search for a filefind location -name test.zip
find / -name phppgadmin.conf
Search filetypefind / -i name "*.txt"
Accessed less than 3m agofind / -amin -3
Accessed two days agofind / -time -2
Smaller than 5MB, bigger than 2MBfind / -size -5M -and -size +2M
Remove all .DS_Store filesfind . -name '*.DS_Store' -type f -delete
Find files older than 7 days
find <dir> -type f -mtime +7
Mtime
+n More than n.
n Exactly n.
-n Less than n.
You can write -mtime 6 or -mtime -6 or -mtime +6:
Using 6 without sign means "equal to 6 days old — so modified between 'now - 6 * 86400' and 'now - 7 * 86400'" (because fractional days are discarded).
Using -6 means "less than 6 days old — so modified on or after 'now - 6 * 86400'".
Using +6 means "more than 6 days old — so modified on or before 'now - 7 * 86400'" (where the 7 is a little unexpected, perhaps).
Grep
Flag | Description |
---|---|
-i | Ignore Case |
-l | Print name of each file which contains a match |
-v | Return all lines which don't match the pattern |
-c | Print a count of matching lines |
-n | Print the line nr before each line that matches |
List all files in directory where file name contains "test"ls | grep "test"
grep "" -l "test"
To search for a line containing text hello.gif in any .txt filesgrep "hello\.gif" *.txt
Exactly and solely "apple"grep -x "apple" list.txt
Everything else but not "apple"grep -v "apple" fruitlist.txt
Match insensitivegrep -i "fruit" fruitlist.txt
Match apple and Applegrep "[Aa]pple" fruitlist.txt
Grep Terminal Historyhistory | grep "ionic"
Output non duplicatesgrep -vFf file1 file2
iPerf3
# on receiver
iperf3 -s -p 7575
# on client
iperf3 -c <receiver-ip> -p 7575
Links
Create symbolic linkln -s /source /destination
Remove linkunlink /destination/script.sh
Netcat
Scan portsnc -vvvnz -w5 185.31.92.195 5577 5500
Netstat
sudo netstat -lntp
l - listening ports
n - no hostnames
t - tcp
p - processes
SSH
ssh-key to machinesssh-copy-id <ip-or-hostname>
Tar
Archive a directory / Filetar -zcvf archive.tar.gz test/
Unarchive
tar -zxvf archive.tar.gz
tar -zxvf archive.tar.gz -C <output-dir>
tar xopf thing.tar
TCP
tcpdump -n -v -i eth0 -s 0 port 80 -w /home/$USER/tcpdump.pcap
-n don't convert addresses to names
-v verbose
-i interface eth0
-s snapshot length
Users
Remove user from group
passwd -d <username>
Reload bash user. ~/.bash_profile
Display all users or groupscompgen -u
compgen -g
Add user to sudoerssudo usermod -aG sudo qrl
Zip
Compress a directory / Filezip -r <test.zip> <folder/file>
Decompressunzip <test.zip> -d <destination_folder>
Wget
Save website
wget --mirror --convert-links --adjust-extension --page-requisites --reject-regex "report" --no-warc-compression --warc-file="kalaralli" https://qrl.ee/kalaralli/