I/O Redirection on tldp.org


  • stdout (file descriptor = 1)
  • stderr (file descriptor = 2)
$ echo "yo" > stdout_replacement.txt
$ sdtbhtb 2> stderr_replacement.txt
$ echo "hello world" 1>output.log 2>debug.log


move all files except some

mkdir archived
mv !(archived) archived/

Create several folders at once

mkdir /docs/folder{1..5}

Script on one line

tests: https://en.wikipedia.org/wiki/Test_(Unix)

if [ sometest ] ; then instruction1; instruction2; instruction19; fi
if [ ! -e /dir ] ; then mkdir /dir; chown leo /dir; fi

Replacing a pattern in a variable inline

To replace the first occurrence of a pattern with a given string, use ${parameter/pattern/string}

To replace all occurrences, use ${parameter//pattern/string}

Here: replace every occurrence of an empty space with an underscore (useful for those hated M$ paths full of it)

DIRBASENAME=`basename ${directory// /_}`

Curly Braces