Skip to main content

Bash tools

Awk

First and third column from file, seperated by spaces
awk -F " " '{print $1,$3'} result.txt

Print from line 156 to line 170
awk 'NR >= 156 && NR <= 170' log.txt

Cat

Copy Data to Other File
cat file1 > file2

Combine Files And Sort Alphabetically
cat file1 file2 | sort > file3

Create new file that consists of the contents of file1, followed by keyboard.
cat file1 -> file2

Multi line to singe line
cat multiline.txt | tr -d '\n' > oneline.txt

Cut

First and third column from file, seperated by spaces
cut -d' ' -f1,3 result.txt

Find

Search for a file
find location -name test.zip
find / -name phppgadmin.conf

Search filetype
find / -i name "*.txt"

Accessed less than 3m ago
find / -amin -3

Accessed two days ago
find / -time -2

Smaller than 5MB, bigger than 2MB
find / -size -5M -and -size +2M

Remove all .DS_Store files
find . -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 files
grep "hello\.gif" *.txt

Exactly and solely "apple"
grep -x "apple" list.txt

Everything else but not "apple"
grep -v "apple" fruitlist.txt

Match insensitive
grep -i "fruit" fruitlist.txt

Match apple and Apple
grep "[Aa]pple" fruitlist.txt

Grep Terminal History
history | grep "ionic"

Output non duplicates
grep -vFf file1 file2

iPerf3
# on receiver
iperf3 -s -p 7575
# on client
iperf3 -c <receiver-ip> -p 7575
Netcat

Scan ports
nc -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 machines
ssh-copy-id <ip-or-hostname>

Tar

Archive a directory / File
tar -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 groups
compgen -u
compgen -g

Add user to sudoers
sudo usermod -aG sudo qrl

Zip

Compress a directory / File
zip -r <test.zip> <folder/file>

Decompress
unzip <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/