bash hacks


Simple as a cup of water:

some recipes collected here and there to make our bash (sorry tcsh users) usage easier

Here I'm going to keep posting some tricks/shortcuts to make life easier using bash. These tips are faaaar away of covering all the facets of this terminal language. If you have more suggestions I can do my best and post my approach, just let me know (twitter or insta)

To help organize I'll use a bracket at the beginning of each description showing some keywords

  • [for loop, str replacing] iterate over a list, also changing part of the string in each step
var=(bee01 bee02 bee03 bee04)
for k in ${var[@]}
    # Change part of the string
    echo "$k" is the original string
    echo "$modif" is the modified version
  • [rename] rename a bunch of files by replacing a substring
for i in *theseFiles
    mv "$i" “${i/replaceThisString/newString}”
  • [ls] get the full path (up to the upper level!)
ls -d -1 $PWD/*.extension
  • [awk] write as comma-separated a list of elements
awk -vORS=, '{ print $1 }' list_of_values.txt | sed 's/,$/\n/'
  • [awk, csv] get only 2nd column of a csv file, from row 5 onwards
awk -F "," 'FNR > 5 {print $2}' file.csv