Useful bash commands

Som command examples using AWK, sed, find, watch and aria2

Albert Lacambra BasilAlbert Lacambra Basil

Reset console colors to bash

echo -e "\033[0m"

AWK useful commands

print column n using a separator #:

awk -F# '{print $n}'

print column 3 of lines that start with a number:

awk '/^[0-9]/ {print $3}'

Variables:

NF: number of fields

NR: Number of record being processed

Avoid first record(row) and print last field (columnd):

awk '{if(NR>1) print $NF}':

More about built-in variables: 8 Powerful Awk Built-in

Some more tricks here

Match and replace using sed

sed -i -e ‘s/pattern/replace-for/g'

Apply commands using find

Execute rm on *.class files:

find . -name *.class -type f -exec rm;

Execute the sed param on files containing java in the name:

find . -type f |grep java|xargs sed -i -e ‘s/pattern/replace-for/g'

Rerun command using watch

To rerun automatically a command use:

watch -n 1 --differences {COMMAND TO REFRESH}

For example, to see creation of docker containers:

watch -n 1 --differences docker ps

Aria2, the wget alternative

Aria2 offers interesting options to download a file, like parallel downloading:

aria2c -x4 {utl} to use 4 connections