Shell Conditional Expressions For Files in If Loop

By | February 4, 2011

Bash shell script begginers often find it confusing in understainding how to use shell conditional expressions in if loop statements. If you are one of those then this article might just help you understand how to do exactly the same. I had real trouble in the beggining to trace this out, but not any more. Master the chart shown below and you will find it really easy to tackle conditional expressions for files in if loop.

Let me imagine that you have the basic understanding of the read (r), write (w) and execute (x) permissions in linux machine. If you don’t, then it is highly advisable to learn it first before you can go any futher.

Now, here is a table that has list of conditional expressions that can handly different operations while used in the shell script.

Conditional Expression Meaning
-a file True if file exists.
-b file True if file exists and is a block special file.
-c file True if file exists and is a character special file.
-d file True if file exists and is a directory.
-e file True if file exists.
-f file True if file exists and is a regular file.
-g file True if file exists and is set-group-id.
-h file True if file exists and is a symbolic link.
-k file True if file exists and its “sticky” bit is set.
-p file True if file exists and is a named pipe (FIFO).
-r file True if file exists and is readable.
-s file True if file exists and has a size greater than zero.
-t fd True if file descriptor fd is open and refers to a terminal.
-u file True if file exists and its set-user-id bit is set.
-w file True if file exists and is writable.
-x file True if file exists and is executable.
-O file True if file exists and is owned by the effective user id.
-G file True if file exists and is owned by the effective group id.
-L file True if file exists and is a symbolic link.
-S file True if file exists and is a socket.
-N file True if file exists and has been modified since it was last read.

Table credits: cyberciti.

Let me explain you how it works with a linux bash shell scripts.

#!/bin/bash

if [ -f /home/vamsi/text ]
then
echo “The file exists”
else
echo “The file doesn’t exist
exit
fi

The above program checks if the a regular file “/home/vamsi/text” exists. If there exists a regual file then the scripts says “The file exists” or else it says “The file doesm’t exits” and quits.

Please make sure to leave a space before and after the ‘-f’ in the above program. Your system might drop you an error if you miss this rule.

#!/bin/bash
if [ ! -x /home/vamsi/text ]
then
echo “The file doesn’t exists”
else
echo “The file exist
exit
fi

Similary ‘! -x’ checks if there exits an executable file in the path. If the file doesn’t exist, then the “then” loop is executed OR “else” loop is not executed. This is an exact opposite way of programming when compared to the previous one.

If you like this article, then please consider sharing it. Sharing really inspires us to do things much better!!

Leave a Reply

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