Bash cheatsheet
Published 8 months ago: June 01, 2020
Table of contents
- Find files with GREP
- Read x last lines from a file
- Copy a file
- Recursively search for filename
- Measure temperature
- TCPtrack
- Securely erase/replace free space on disk
- Permissions
- Change permissions
- Measure wireless (WiFi/WLAN) signal quality and strength
- Count words in a plain text file
- Diff - Find differences between two folders
- DU (disk usage)
This cheatsheet is a small collection of handy snippets for quick reference in tricky situations. Feel free to suggest edits, propose additions, complain about something, disagree with content or such and the like. Every feedback is a welcome one.
Find files with GREP
Search for a specific word, phrase or Regex in file contents.
# Search for a specific word or phrase in a specific file
grep "word" file.txt
# Same as above, but limiting to one extension/suffix
grep "word" *.txt
# or using wildcard to search multiple files
grep "word" *
# As above, while traversing subdirectories
grep -r "word" *
# Case-Insensitive search (ignore case)
grep -i "word" *
Read x last lines from a file
# example
tail -100 /var/log/apache2/error.log
Copy a file
sudo cp /path/to/source/file.txt /path/for/destination/file.txt
Recursively search for filename
# Add wildcard '*' on either side where required
find . -name "yourfilename*"
# Case insensitive
find . -iname "yourfilename*"
Measure temperature
# If command is not found, install lm-sensors
sudo apt install lm-sensors
# Then run
sensors
# or
sensors-detect
TCPtrack
tcptrack displays the status of TCP connections that it sees on a given network interface. tcptrack monitors their state and displays information such as state, source/destination addresses and bandwidth usage in a sorted, updated list very much like the top(1) command.
# If command is not found, install with
sudo apt install tcptrack
# Assumed network name - wlp2s0 - replace with your own.
sudo tcptrack -r 1 -i wlp2s0
Securely erase/replace free space on disk
# Example. Replace 'Desktop' with desired path
sfill -v Desktop/
Skip -v for less verbose output
Permissions
Change permissions
# Change owner user and group on a directory or file
chown yourusername:yourgroup <directory or file>
# Make a file editable for owner and group
chmod 774 file.txt
Find files not owned by user
sudo find ~yourusername/ \! -user yourusername -print
Measure wireless (WiFi/WLAN) signal quality and strength
iwconfig
# Output example
wlp2s0 IEEE 802.11 ESSID:"Network Name"
Mode:Managed Frequency:2.472 GHz Access Point: 1D:B7:2C:37:0F:D0
Bit Rate=144.4 Mb/s Tx-Power=22 dBm
Retry short limit:7 RTS thr:off Fragment thr:off
Power Management:on
Link Quality=53/70 Signal level=-57 dBm
Rx invalid nwid:0 Rx invalid crypt:0 Rx invalid frag:0
Tx excessive retries:0 Invalid misc:991 Missed beacon:0
Look for Signal level
, this example outputs -57dbm, a good signal. Not excellent, but good.
Count words in a plain text file
cat filename.txt | xargs -n1 | sort | uniq -c > newfilename.txt
Diff - Find differences between two folders
diff -r dir1 dir2 | grep dir1 | awk '{print $4}' > difference1.txt
# For skipping pipe output to file
diff -rq dir1 dir2
DU (disk usage)
Find the biggest files in a directory (and subdirs)
du -Sh | sort -rh | head -20
Directory size
du -sh *