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
link|improve this question

80% accept rate
This really belongs on stackoverflow – Kevin Kuphal Aug 5 '09 at 13:52
4  
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. – thepocketwade Aug 5 '09 at 13:54
If you're not even able to read a man page, you really should have a look at superuser.com – Benoit Aug 5 '09 at 13:59
2  
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/… – Kyle Brandt Aug 5 '09 at 14:03
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 – Benoit Aug 5 '09 at 15:47
feedback

6 Answers

up vote 8 down vote accepted

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.

link|improve this answer
this is bash / ksh only, not posix. – chris Aug 5 '09 at 14:31
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 Aug 5 '09 at 14:32
change line 1 to #!/bin/bash, then you don't have to think about if it is ksh or bash. – Johan Aug 5 '09 at 14:35
@johan: Yes, and redirecting stderr of a compiler fixes all it's complaints. – chris Aug 5 '09 at 15:04
feedback

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

link|improve this answer
+1, ditto...... – nik Aug 5 '09 at 13:55
feedback

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.

link|improve this answer
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. – Craig Sanders Aug 5 '09 at 21:54
feedback

Reference page for file testing

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

link|improve this answer
feedback

There is always the man pages:

man test

link|improve this answer
feedback

Just to note that if you want something that works across all sh shells (not only bash) and cross-platform, the only way is:

ls filename >/dev/null 2>&1
if [ $? = 0 ]; then
   echo "File exists"
fi
link|improve this answer
Check it for folder with same name.... – Sergey Aug 5 '09 at 14:30
You can get rid of the $? nonsense. if ls filname > /dev/null ; then echo file exists ; fi – chris Aug 5 '09 at 14:34
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.