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

Posted on in Categories Howto, Linux, RedHat/Fedora Linux, Shell scripting, UNIX last updated January 25, 2016

With the help of BASH shell and IF command, it is possible to find out if file exists or not on the filesystem. A conditional expressions (also know as “evaluating expressions”) can be used by [[ compound command and the test ([) builtin commands to test file attributes and perform string and arithmetic comparisons.

Syntax

The general syntax is:
[ parameter FILE ]
OR
test parameter FILE
OR
[[ 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.

Please note that the [[ works only in Bash, Zsh and the Korn shell, and is more powerful; [ and test are available in POSIX shells. Let us see some examples.

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

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

[[ example

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

Find out if directory /var/logs exist or not

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

[[ example

$ [[ -d /var/logs ]] && echo "Directory exist" || echo "Directory does not exist"
$ [[ -d /dumper/fake ]] && echo "Directory exist" || echo "Directory does not exist"

Are two files are the same?

Use the -ef primitive with the [[ new test command:

[[ /etc/resolv.conf -ef /etc/resolv.conf ]] && echo "Same files" || echo "Noop"
[[ /etc/resolv.conf -ef /etc/passwd ]] && echo "Same files" || echo "Noop"

Shell script example

You can use conditional expressions in a shell script:

#!/bin/bash
FILE="$1"
 
if [ -f "$FILE" ];
then
   echo "File $FILE exist."
else
   echo "File $FILE does not exist" >&2
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 or backup source directory exits or not in shell scripts. See example script for more information.

34 comment

  1. 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.

  2. 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 [email protected]
    Many thanks in advance – Petrus

  3. 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
  4. 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

  5. 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.

  6. 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

  7. 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

  8. 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?

  9. 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 :)

    1. 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

  10. 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

  11. 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

  12. 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.

  13. Not Working for me for the below code

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

  14. 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

  15. i want to check if the file exists in a folder:
    if exists(delete the file in the folder)
    else
    create a new file in the same name.

    I tried the above codes which dint work for me.. any help would be of great help..

Leave a Comment