Customize Consent Preferences

We use cookies to help you navigate efficiently and perform certain functions. You will find detailed information about all cookies under each consent category below.

The cookies that are categorized as "Necessary" are stored on your browser as they are essential for enabling the basic functionalities of the site. ... 

Always Active

Necessary cookies are required to enable the basic features of this site, such as providing secure log-in or adjusting your consent preferences. These cookies do not store any personally identifiable data.

No cookies to display.

Functional cookies help perform certain functionalities like sharing the content of the website on social media platforms, collecting feedback, and other third-party features.

No cookies to display.

Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics such as the number of visitors, bounce rate, traffic source, etc.

No cookies to display.

Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.

No cookies to display.

Advertisement cookies are used to provide visitors with customized advertisements based on the pages you visited previously and to analyze the effectiveness of the ad campaigns.

No cookies to display.

Checking files & directories in your Bash script

It’s good practise to perform all kinds of tests on files that you are processing in a Bash script, in particular if it is a file that the user has provided. Here is a reminder of the test flags you can use as part of the test command:

Switches to check:

-b filename – Block special file
-c filename – Special character file
-d directoryname – Check for directory Existence
-e filename – Check for file existence, regardless of type (node, directory, socket, etc.)
-f filename – Check for regular file existence not a directory
-G filename – Check if file exists and is owned by effective group ID
-G filename set-group-id – True if file exists and is set-group-id
-k filename – Sticky bit
-L filename – Symbolic link
-O filename – True if file exists and is owned by the effective user id
-r filename – Check if file is a readable
-S filename – Check if file is socket
-s filename – Check if file is nonzero size
-u filename – Check if file set-user-id bit is set
-w filename – Check if file is writable
-x filename – Check if file is executable

How to use:

#!/bin/bash
file=./file
if [ -e "${file}" ] ; then
    echo "File exists"
else 
    echo "File does not exist"
fi 

test expression can be negated by using the ! operator

#!/bin/bash
file=./file
if [ ! -e "${file}" ] ; then
    echo "File does not exist"
else 
    echo "File exists"
fi 

Source: GroundZero @ StackOverflow
Note: To be risk-adverse I like to use double brackets [[ ]] around the tests and brackets {} around the variable names.

Additionally it might be useful here to learn additional comparison operators:

[ ( EXPR ) ]Returns the value of EXPR. This may be used to override the
normal precedence of operators.
[ EXPR1 -a EXPR2 ]True if both EXPR1 and EXPR2 are true.
[ EXPR1 -o EXPR2 ]True if either EXPR1 or EXPR2 is true.
[ -z STRING ]True of the length if “STRING” is zero.
[ -n STRING ] or [ STRING ]True if the length of “STRING” is non-zero.
[ STRING1 == STRING2 ]True if the strings are equal. “=” may be used instead
of “==” for strict POSIX compliance.
[ STRING1 != STRING2 ]True if the strings are not equal.
[ ARG1 OP ARG2 ]“OP” is one of -eq-ne-lt-le-gt or -ge.
These arithmetic binary operators return true if “ARG1” is equal to,
not equal to, less than, less than or equal to, greater than, or greater
than or equal to “ARG2”, respectively. “ARG1” and “ARG2” are integers.

Source

Feature Image by Gerd Altmann from Pixabay