Bash script File test operators with examples

There are situation where we need to use different types of test operators in bash script. The file test operators help us to do some test’s on a particular file before attempting to perform some action. The file test operators are mostly used in the if clause of the bash script.

The common file test operators used in Linux are listed below.

-b FILE  –   FILE exists and is block special
-c FILE  –   FILE exists and is character special
-d FILE  –   FILE exists and is a directory
-e FILE  –   FILE exists
-f FILE  –   FILE exists and is a regular file
-g FILE  –   FILE exists and is set-group-ID
-G FILE  –   FILE exists and is owned by the effective group ID
-h FILE  –   FILE exists and is a symbolic link (same as -L)
-k FILE  –   FILE exists and has its sticky bit set
-L FILE  –   FILE exists and is a symbolic link (same as -h)
-O FILE  –   FILE exists and is owned by the effective user ID
-p FILE  –   FILE exists and is a named pipe
-r FILE  –   FILE exists and read permission is granted
-s FILE  –   FILE exists and has a size greater than zero
-S FILE  –   FILE exists and is a socket
-u FILE  –   FILE exists and its set-user-ID bit is set
-w FILE  –   FILE exists and write permission is granted
-x FILE  –   FILE exists and execute permission is granted

Through file operation, you can reduce the number of errors that your bash script will encounter. The syntax is as follows:

if [ -option <filename> ] then
execute command 1
else
execute command 2
fi

The below example’s will help you to understand the way of using file test operators along with if clause.

1. How to check the existence of a file.

The below pasted if clause checking the existence of /etc/passwd file.

#!/bin/bash
file=”/etc/passwd”
if [ -e $file ] then
echo “File exists”
else
echo “File not found”
fi

First, we assign the value “/etc/passwd” to the variable “file”, you can replace the value of the variable “file” with your own file name. Next we are checking the existence of file with the help of if clause,
if it exists then it returns the value “file exist” otherwise it displays the value “file not exist”

Sample output:

[root@HostDire ~]# ./iftest.txt
File exists
[root@HostDire ~]#

2.How to check the existence of a directory.

#!/bin/bash
folder=”/opt/test”
if [ -d $folder ] then
echo “folder exist”
else
mkdir /opt/test
echo -e “created the folder /opt/test”
fi

This is actually checking whether the folder “/opt/test” exists or not. If it’s not found then the second command will create the same.

3. How to check the read, write and execute access of a file

The below simple bash script will help you to check the read, write and execute permission of a file.

#!/bin/bash
file=”/etc/passwd”

if [ -r $file ] then
echo “File has read permission”
else
echo “File does not have read permission”
fi

if [ -w $file ] then
echo “File has write permission”
else
echo “File does not have write permission”
fi

if [ -x $file ] then
echo “File has execute permission”
else
echo “File does not have execute permission”

[DISPLAY_ACURAX_ICONS]
Total
0
Shares
Leave a Reply

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