Conditional Expressions for Files in Bash

What are conditional expressions?
When we use conditional statements like if
we need to pass some conditions to check whether it's true or not and based on that information we execute some commands. For example:
i=4
if i>2:
print("i is greater than 2")
else:
print("i is lesser than 2")
The above program checks that if the value of i is greater than 2 or not if true it prints "i is greater than 2". The i>2
is the conditional expression.
Likewise bash also have conditional expressions but the syntax is unique. Conditional expressions in bash are targeted more towards the Linux usability like checking conditions on files, directories etc.
Conditional Expressions for Files
Here is a example code:
#! /usr/bin/env bash
filename=./array5.sh
if [[ -a ${filename} ]]; then
echo "The file exists!"
else
echo "The file doesn't exists!"
fi
if [[ -d ${filename} ]]; then
echo "This is a directory!"
else
echo "This is not a directory!"
fi
if [[ -r ${filename} ]]; then
echo "The file exists and is readable"
fi
if [[ -s ${filename} ]]; then
echo "The file exists and file size is greater than zero"
fi
if [[ -w ${filename} ]]; then
echo "The file exists and file is writable"
fi
if [[ -x ${filename} ]]; then
echo "The file exists and file is executable"
fi
if [[ -L ${filename} ]]; then
echo "The file exists and file is symbolic link"
else
echo "Not a symbolic Link!"
fi
In the above code we have multiple if then
statements that the checking some conditions on file array5.sh
whole name is stored at the top in the variable name filename
.
Let's see the first if then
statement block:
filename=./array5.sh
if [[ -a ${filename} ]]; then
echo "The file exists!"
else
echo "The file doesn't exists!"
fi
Here I have condition
[[ -a ${filename} ]]
for if statement.This will check whether the file exists or not in the current directory(because the file path is
./
array5.sh
,.
means the current directory) .If the file exists then the program will execute the
then
block and messageThe file exists!
will be printed.If the file
array5.sh
doesn't exists in the current directory then the program will executeelse
block and messageThis file doesn't exists!
will be printed in the console.The ending
fi
is the syntax of the bashif
statement, it is there to indicate the closing of theif
block.
There are other conditional expressions for files:
[[ -d ${filename} ]]
: True if the file is a directory.[[ -r ${filename} ]]
: True if file exists and readable.[[ -s ${filename} ]]
: True if file exists and has size larger than zero.(To check empty files)[[ -w ${filename} ]]
: True if file exists and writable.[[ -x ${filename} ]]
: True if file exists and executable.[[ -L ${filename} ]]
: True if file is a symbolic link.
Note: The -x
,-w
, -r
flags check the file permissions for the user that is running the script.
Subscribe to my newsletter
Read articles from Arpit Shraddhansh directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
