Shell (unix) commands you always forget
This is a list of typical commands that I often have to Google because I forget either their usage or important flags.
Using grep with perl type regex
ls | grep -P "dirNo\d\d$"Using the -P flag lets you search for more advanced regex expressions. Other useful grep flags:
- -iignore case
- -oprint only matching
- -vinvert matching
- -A numbershow lines after match
- -B numbershow lines before match
Using grep without pipe
grep "hell\w" . -Rsearches after pattern hell\w in current directory and sub-directories. Add -h to ignore filenames in output.
For loops - range
for ((i = 1; i<=10; i++)); do echo $i; doneFor loops - command
for v in `ls`; do echo $v; doneBash script for traversing arguments and line breaks
#!/bin/bash
IFS=$'\n'
SENTENCES="line 1${IFS}line2${IFS}line3"
for arg in "$@" # for all input arguments
 do
 #automatically loops through line breaks duo to IFS value
   for s in $SENTENCES
     do
       echo "$arg=$s"
     done
 doneSimple sed commands
Search and replace file content:
sed -i "s/<searchpattern>/<replace>/g" <files>From pipe and print result:
sed -n "s/<searchpattern>/<replace>/p"To replace matched regex groups, use \1, \2 and so on.
Finding size of subfolders, sorted.
du -h -d 1 | sort -hr- du -h d -1is the disc usage command with the- -hflag for outputting human readable sizes and- -d 1for depth of 1 sub dirs.
- sort -hrsorts human readable sizes in reverse order.