Creating this post to dump here various bash shell and script commands, tips and tricks which I found useful in my everyday work. Will update it with new info continually…
Check in bash if a file exists or not and do something in each case:

if [ -f $FILE ];
 echo "File $FILE exists."
 echo "File $FILE does not exist."

Check only if a file DOES NOT exist:

if [ ! -f $FILE ]; then
  echo "File not found!"

In the above examples FILE can be set to any file name or any of the command line arguments (you need to add these lines BEFORE the ‘if’ statements):

FILE=/tmp/foo.txt  # set file to some knowon file name
FILE=$3            # set file to argument 3 from the bash command line

Leave a Reply

Your email address will not be published. Required fields are marked *

Set your Twitter account name in your settings to use the TwitterBar Section.