Learn to use Bash parameters:
- Create and move into a new directory:
mkdir foo && cd "$_"
Get the size of a file: du -sh file.txt.
Want to clear your screen but typing clear is too much work? Use ctrl+L instead.
Reload your bash profile: source ~/.bash_profile.
Need to remove those odd Windows line endings? dos2unix is your friend.
Redirect STDERR: 2> /dev/null or 2>&1.
Want to cat line by line? paste -d, file1 file2
Need a random sample of lines from a file? shuf -n 10 will give you 10 lines. In Mac you need to use gshuf.
Find
Looking for non-zero files? find -maxdepth 1 -size +0
Want directories modified within the last 30 minutes? find . -type d -mmin -30
Want to run a script over each file of a certain name in a directory?
find ../blah-directory -name "*rules.txt" -print0 |
xargs -0 -I {} ./myscript.sh {} | tee all_output.tsv
Get Public IP
dig +short myip.opendns.com @resolver1.opendns.com
Put this in your bashrc:
alias wanip='dig +short myip.opendns.com @resolver1.opendns.com'
Moreutils
Checkout moreutils for some useful cli, e.g., sponge.
# stop using those pesky temp files.
cut -d, -f2 top_10k.txt | sponge top_10k.txt
Awk
Prepend text.
awk '{print "http://" $0;}' < top_10k.txt
jq
Prettify the JSON: cat example.json | jq '.'
Get the number of elements cat example.json | jq 'length'