Sunday, 11 September 2011

Combine your UNIX commands with control operators


Run a command only if another command returns a zero exit status,

Below command is to change to the directory tmp/a/b/c and list the directory content.
~ $ cd tmp/a/b/c && ls -ltr
Execute second only if first returns non-zero, change to the directory tmp/a/b/c, and if does not exist, create the directory tree and list the content
~ $ cd tmp/a/b/c || mkdir -p tmp/a/b/c && ls -ltr

Another example,

~ $ cd tmp/a/b/c || mkdir -p tmp/a/b/c && touch myfile.txt

No comments:

Post a Comment