≡ Menu

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

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.

Tweet itFacebook itGoogle+ itPDF itFound an error/typo on this page?

{ 31 comments… add one }

  • loony October 27, 2007, 2:17 pm

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

    if [ -f “$FILE” ];

  • aditya May 12, 2008, 5:21 am

    script was very userful

  • Mehdi Anis December 3, 2008, 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.

  • thant December 8, 2008, 4:57 pm

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

  • Don Petrus January 14, 2009, 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

  • Bram April 6, 2009, 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
  • Zach October 1, 2009, 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

    • Satyajit Sahoo September 20, 2012, 9:10 am

      Just this much will do,

      #!/bin/bash
      FILE=$1

      [[ -f $FILE ]]

  • Rajee October 9, 2009, 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.

  • Fred November 17, 2009, 1:31 pm

    For me, it only works without variables, like

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

    Worth trying.

  • Debbie November 24, 2009, 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?

  • krishna January 24, 2010, 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

  • amit March 11, 2010, 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

  • Anthony Youngman May 28, 2010, 8:40 pm

    ls D
    blah blah blah
    drwx—— Program Files

    if -d D/Program\ Files returns FALSE

    It would be nice if I could find some way of successfully testing for names with spaces :-(

    Cheers,
    Wol

    • Sergani August 2, 2011, 1:26 am

      Try ls -1D (that’s ls -‘ONE’ -D)

  • Jasper August 2, 2010, 10:57 am

    Hi,

    I need to check if a file with the current date exists in a folder. How would I do that?

  • Betulino August 12, 2010, 8:06 am

    Hi,

    I have /media/USBDisk0 and that the mount point for my USBDisk.
    And a hard link named /media/TheData/Data -> /media/USBDisk0/Data
    When the USB is mounted, the hard link is OK, but If something fail, the hard link is broken…. Can use the if to test the hard link?

  • sonal February 7, 2011, 6:34 am

    i need to check all the files in the current directory and display them.how can i do this?

    • Sergani August 2, 2011, 1:30 am

      cd /path/to/directory
      for i in $(ls -1 *)
      do
      if [ -f $i ] ; then
      ll $i
      else
      echo “$i is not a file”
      fi

  • Lalit Mohan March 7, 2011, 6:35 am

    I want to check file that i fetching is complete or not, if file is still copying than wait for few minutes, can this things possible, Reply me Please!!!

  • Ked June 28, 2011, 1:05 pm

    That was a great help, I needed a script to check if the .parentlock file was present in the Thunderbird profile directory and then delete it if it was.

    Worked first time :)

    • Sergani August 2, 2011, 1:32 am

      you could also have done the following:

      find /home/username/thunderbird-directory/ -iname .parentlock -ok rm {} ;\

      it’ll find the file and attempt to remove it, but will ask you first… if you need it to remove it right away, replace -ok with -exec

  • Ravi Theja September 13, 2011, 8:05 pm

    if [ -e filename ] works for me and checks fine ONLY when I’m logged in as a power broker. It doesn’t test when I log in as a normal user or call the script from a different scheduling tool.

    Please help me ASAP

  • chey December 2, 2011, 3:41 pm

    Thanks for the script, it was very useful.

  • Rahul May 14, 2012, 7:20 pm

    i need shell script where the i need to connect from one server to another server…
    and check for file *.mediate.log in path /home/apps/ and if found it need to execute a script prescript.sh in the same path and return a status to the server…and if the files doesn’t exist it shud return another status…and if the job fails becoz of any resin it need to return another status…
    Can you please help me with this ASAP…very urgent

  • srini September 5, 2013, 9:06 am

    I have a file with date and time. but I want to check with date part only file exist or not without changing the file.

    Can any one pleae help me.

    • Manoj December 3, 2013, 4:22 pm

      Can we find content in file with if condition, for an example if ‘abc’ is there in xyz file or not.

      Regards,
      Manoj

  • Santosh July 31, 2014, 11:20 am

    Not Working for me for the below code

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

    • newton August 12, 2015, 4:38 am

      How are you running the script?

  • Shahid October 15, 2014, 2:25 pm

    I need to check if in
    1.example1.sh test.log exists
    if 1 is yes then execute 2 else execute 5
    2. get the current date from test.log file
    3. get current system date
    4. if date in step 2 + 1 day <=date in step 3 then step 5 else step 6
    5. execute example2.sh
    6.end

    example2.sh
    writes to test.log
    and also contains current date and current time

  • Ikem February 3, 2015, 4:56 pm

    Very useful is also the test parameter “-s” which checks if a file exists and has a size greater then zero.

Leave a Comment