Linux/UNIX: Find Out If File Exists With Conditional Expressions

by nixcraft · 12 comments

With the help of BASH shell and IF command it is possible to find out if file exists or not. Generally, this is known as conditional expressions. Conditional expressions are used by the [[ compound command and the test and [ builtin commands to test file attributes and perform string and arithmetic comparisons. General syntax:

[ parameter FILE ]
OR
test parameter FILE

Where parameter can be any one of the following:

  • -e: Returns true value if file exists
  • -f: Return true value if file exists and regular file
  • -r: Return true value if file exists and is readable
  • -w: Return true value if file exists and is writable
  • -x: Return true value if file exists and is executable
  • -d: Return true value if exists and is a directory

Examples

Find out if file /etc/passwd file exists or not

Type the following commands:
$ [ -f /etc/passwd ] && echo "File exists" || echo "File does not exists"
$ [ -f /tmp/fileonetwo ] && echo "File exists" || echo "File does not exists"

Find out if directory /var/logs exists or not

Type the following commands:
$ [ -d /var/logs ] && echo "Directory exists" || echo "Directory does not exists"
$ [ -d /dumper/fack ] && echo "Directory exists" || echo "Directory does not exists"

You can use conditional expressions in a shell script:

#!/bin/bash
FILE=$1
 
if [ -f $FILE ];
then
   echo "File $FILE exists"
else
   echo "File $FILE does not exists"
fi
 

Save and execute the script:
$ chmod +x script.sh
$ ./script.sh /path/to/file
$ ./script.sh /etc/resolv.conf

You can use this technique to verify that backup directory and backup source directory exits See example script for more information.

Featured Articles:

Want to read Linux tips and tricks, but don't have time to check our blog everyday? Subscribe to our daily email newsletter to make sure you don't miss a single tip/tricks. Subscribe to our weekly newsletter here!

{ 12 comments… read them below or add one }

1 loony 10.27.07 at 2:17 pm

For me the if statement only works when using the FILE var with quotes like this:

if [ -f "$FILE" ];

2 aditya 05.12.08 at 5:21 am

script was very userful

3 Mehdi Anis 12.03.08 at 4:49 pm

I need to check a file on daily basis that is created by anotehr process. The file format is:

DataFile_MMDDYYYY.csv

So, I need to get today’s date in MMDDYYYY format, concate that with a string variable, FileName = “DataFile_” + DatePart + “.csv”

Then look for that FileName file if exists then run a command (sas sendemail.sas).

How can I achieve that? Thanks.

4 thant 12.08.08 at 4:57 pm

DatePart=`date ‘+%m%d%Y`
FileName =DataFile_${DatePart}.csv

5 Don Petrus 01.14.09 at 3:03 pm

Hello Volks:
I would like to look for a File: if it exists – AND – itĀ“smaller (lt/gt) than 1000000, then … echo ” File too small” , return 1 … and so on:

#!/bin/bash
FILE=nec2.txt
if -f [ "$FILE" ] && -f [ "$FILE" -lt 1000000 ] ;
then
echo “File $FILE too small”
else
echo “File $FILE does not exists”
fi

-rw-rw—- 1 cas lokus 31626 2008-12-15 14:00 xnec2.txt

I use linux 2.4.21-278-smp

Can you please help there ? Pleas reply to donpetrus@hotmail.com
Many thanks in advance – Petrus

6 Bram 04.06.09 at 9:02 am

Bash knows the NOT procedure for IF. So No ELSE would be used. watch the !

#!/bin/bash
FILE=$1
if [ ! -f $FILE ];
then
   echo "File $FILE does not exists"
fi
7 Zach 10.01.09 at 8:52 pm

I just hacked this together. I plan to use it for my other scripts. The exit statements make the script exit with a 0 (bash standard for successful completion) or a 1 (non-zero is the bash standard for a failed action)

#!/bin/bash
FILE=$1

if [ -f $FILE ];
then
exit 0
else
exit 1
fi

8 Rajee 10.09.09 at 10:06 am

How to check some set of files in the following directory and do the process.
my statement following is not working: pls explain
if [ -f "$ROOT/abc/files/CUST*" ]; then
echo “inside customer file exists”
else
echo “No customer file exists”
fi
CUST is the post fix of the files, expected to have more than 1 file on some days.

9 Fred 11.17.09 at 1:31 pm

For me, it only works without variables, like

if [ -f /home/user/hey.jpg ]; then
….
else
….
fi

Worth trying.

10 Debbie 11.24.09 at 7:41 pm

I want to test if any file ending in *.DATGO exists.
Do you always have to use the exact name of the file?

11 krishna 01.24.10 at 1:21 pm

I need the shell script to check whether the file exist or not
and whether the format of the file is .dat or not

expecting the help ASAP…pls

12 amit 03.11.10 at 5:09 pm

the only thing i can say is that -

if statement does’nt allow wild card, you have to think a work around for this

Leave a Comment

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Previous post:

Next post: