How to check a file exists

16,921

Solution 1

You probably want /bin/bash unless you need to use /bin/sh, /bin/sh is more restricted. So if you are using bash:

Like so:

 if [[ -e filename ]]; then
    echo 'exists'
 fi

If your filename is in a variable, then use the following, the double quotes are important if the file has a space in it:

if [[ -e "$myFile" ]]; then
   echo 'exists'
fi

If you are using sh, and want to be compatible with the IEEE Std 1003.1,2004 Edition, then use single brackets instead. The -e switch is still supported.

Solution 2

http://tldp.org/LDP/abs/html/fto.html

Solution 3

if [ -f filename ]

will test for the existence of a regular file. There are other switches you can pass it to check for an executable or other attributes of a file.

Solution 4

Reference page for file testing

Once you run through all those pages,
Keep this Reference sheet handy.

Share:
16,921

Related videos on Youtube

Simon Hodgson
Author by

Simon Hodgson

Software Engineer, IT Manager, Sys Admin, Telecomms Engineer

Updated on September 17, 2022

Comments

  • Simon Hodgson
    Simon Hodgson almost 2 years

    How do I determine that a file exists using a shell script?

    I.e:

    #!/bin/sh
    
    if [ Does File Exist? ]
    then
        do this thing
    fi
    
    • Kevin Kuphal
      Kevin Kuphal almost 15 years
      This really belongs on stackoverflow
    • thepocketwade
      thepocketwade almost 15 years
      Not necessarily, this kind of thing is important in init scripts and other sysadmin tools. Thus, it shouldn't necessarily be migrated from either site to the other.
    • Benoit
      Benoit almost 15 years
      If you're not even able to read a man page, you really should have a look at superuser.com
    • Kyle Brandt
      Kyle Brandt almost 15 years
      Benoit: The question is fine, I think, if you listen to podcast #58, they want questions like this. As a demo, Joel asked how to move the turtle in LOGO: stackoverflow.com/questions/1003841/…
    • Benoit
      Benoit almost 15 years
      Kyle: As far as I understand, SF is for sys/admin related questions (even simple ones yes). But this questions is more about learning how to use an O/S, not about managing a server. That's why I think this question belongs to superuser.com
  • Sergey
    Sergey almost 15 years
    Check it for folder with same name....
  • chris
    chris almost 15 years
    this is bash / ksh only, not posix.
  • chris
    chris almost 15 years
    Single brackets are an alias for test, which is a shell built-in. [[ ]] is a part of the extended bourne shell syntax of ksh and was adopted by bash.
  • chris
    chris almost 15 years
    You can get rid of the $? nonsense. if ls filname > /dev/null ; then echo file exists ; fi
  • Johan
    Johan almost 15 years
    change line 1 to #!/bin/bash, then you don't have to think about if it is ksh or bash.
  • chris
    chris almost 15 years
    @johan: Yes, and redirecting stderr of a compiler fixes all it's complaints.
  • cas
    cas almost 15 years
    that works, but '-e' is the test for existence (regardless of what it is - file, symlink, device node, named pipe etc). '-f' tests whether it is a regular file. in bash, run 'help test' for a full list of such tests.