Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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



if [ Does File Exist? ]
    do this thing
share|improve this question
This really belongs on stackoverflow – Kevin Kuphal Aug 5 '09 at 13:52
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 – Benoit Aug 5 '09 at 13:59
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:… – 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 – Benoit Aug 5 '09 at 15:47
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'

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'

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.

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

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

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.

share|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. – cas Aug 5 '09 at 21:54

Reference page for file testing

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

share|improve this answer

There is always the man pages:

man test

share|improve this answer

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"
share|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

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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