Advanced bash and zsh

General, useful things

curl cheat.sh/command_to_search shows a cheatsheet with options for this command

Snap

Offline .snap file install

To install a .snap file you downloaded you need to

sudo snap ack downloaded_file.snap
sudo snap install downloaded_file.snap

# if the ack doesn't work, you can always install it with
sudo snap install --dangerous downloaded_file.snap

Processing files and data

  • find . -iname "*file_to_search*" search something at current directory
  • locate file_to_search search through whole computer
  • rg general searching through data files. Better than grep -r
  • diff -y --suppress-common-lines file1 file2 | grep '^' | wc -l count number of changes between 2 files

Analyze logs in real time

With less +F we can inspect the file, stop to read and resume in real time. The advantage over tail -f is, with the former we can inspect the logs, but if there’s an error or something we want to stop and see, we have to exit and cat or vim the file.

less +F file_to_inspect # real time
ctrl + c # stops
shift + F # resumes

Discard normal & error output of a command

> /dev/null Redirects standard output to /dev/null
2>&1 redirects error output to same as standard output

command_to_execute > /dev/null 2>&1

Vimdiff

Useful to compare changes between up to 4 files at once. It uses the same commands as vim.

ctrl + w direction arrow moves the cursor in between file windows
:qa quit all windows without saving
:wqa same but saving

Awk

Language processor. Useful to process .csv or files which are structured by columns. By default, it separates columns by whitespaces.

Change line-separator

awk -F '\t' '{print}' file.txt Uses tabs instead of spaces as separator.

Working with filters

awk '$19 == "S"' file.txt show lines where the column #19 is equal to S awk -F '\t' '$2 == "18249"' file.txt uses both a filter and another line separator at the same time

awk '{print $4}' file.txt | sort | uniq print duplicates entries only once
awk -F '\t' '$2 == "18249" || $2 == "18258"' file.txt one condition or the other

Working with columns

awk '{print $4}' file.txt Prints only column #4
awk '{print NR, $4}' file.txt Print only column #4 with line number
awk 'length($2) > 0' file.txt print lines, where the second column has a length greater than 0.

Regex

awk '$2 ~ /^[0-9]+$/' fichero.txt get columns for which the following regex is true
!~ regex negation

Misc.

file_or_command | tee filename It takes an input, outputs it into the console and at the same time saves it into a file.

Reference(s)

https://github.com/jlevy/the-art-of-command-line?utm_campaign=explore-email&utm_medium=email&utm_source=newsletter&utm_term=weekly
https://www.youtube.com/watch?v=l8iXMgk2nnY
https://www.youtube.com/watch?v=1alWK5ByNMc