Determine if file exists in a Bourne/bash shell script

Contributor Icon Contributed by Rex  
Tag Icon Tagged: Bourne shell scripting  


if [ -f testfile ]
then
echo testfile exists!
fi

 

21 Comments -


  1. DaVince said on October 27, 2008

    Just what I needed, thanks.

  2. anonymous said on November 24, 2008

    if [ -f testfile ];
    then
    echo Blablabla
    fi

  3. James said on November 26, 2008

    total crap, it doesn’t actually test if the file is there or not, it just tests if the command line argument is there. There’s a giant f*cking differences.

  4. Adam said on December 3, 2008

    I don’t think you’re using it quite right Young Padawan

  5. ayw said on February 10, 2009

    if [ -e testfile ]; tests whether a file exists.

    if [ -f testfile ]; only tests whether file is a regular file

  6. iGuide said on March 5, 2009

    Shouldn’t that one have some brackets or something?

  7. David said on March 24, 2009

    No. From the man page: ” -f filename
    True if filename exists and is a regular file.”"

  8. Joeblackspirit said on May 29, 2009

    Perfect and simple!!

  9. Third Rook said on July 26, 2009

    Regardless what the man page says. -f didn’t work in my script, and -e did.

    Thanks ayw.

  10. Anonymous said on September 9, 2009

    And what to do if I have several files smth.gz but I don’t know how much and I don’t know actual names.
    I need to run some command if one of such files exists.

  11. Anonymous said on September 10, 2009

    Well, I’ve found the solution:

    for *.gz
    do
    my_command
    done

  12. Incognito said on September 5, 2010

    -f didn’t work for me either.
    Maybe different Version of bash or whatever.
    -e worked well.

  13. Blake Galbreath said on September 26, 2010

    Um…

    “True if filename exists AND is a regular file”

    So, let me break it down for you idiots.

    -f tests for an existing REGULAR file. If the file exists, but is NOT regular, then -f returns FALSE.

    -e tests for an existing file, regardless if its regular or not. If the file is there (regular or not) it returns TRUE.

    Hope I helped

    ;)

  14. Drain said on September 30, 2010

    You are rude.

  15. sims said on December 9, 2010

    Well, at least they are acting like idiots. WTF does “AND” mean to you?

  16. Jscrobinson said on March 10, 2011

    I think a lot of you should grow up a little bit and try and do something about your bad language.

  17. Shabeer said on March 14, 2011

    does the -f or -s option work if the filename has a white space. below is the file and when I use the file name in if condition it throws an error. I tried with double quotes, single quotes etc.. if condition throws an error

    bash-3.00$ ls file name
    file name
    bash-3.00$

  18. Jan Stammes said on April 21, 2011

    just where I was looking for

  19. it doesn't matter said on June 11, 2011

    Swearing in this way is just a demonstration of childish bad behavior.  If you don’t want to help or you don’t have anything constructive to say then please don’t say it. 
    If you disagree with someone then explain your opinion respectfully, you may both be right or either of you may be wrong.  But either way there is no need for bad language is there? 

  20. Teen said on June 28, 2011

    idiot

  21. NeoVG said on August 22, 2011

    @Shabeer: You have to escape the whitespaces. So try this:

    bash-3.00$ ls file \name

    @Others: -f fails, if the target is a symbolic link, as a link is NO regular file, although you can read it like one.

 

RSS feed for comments on this post. TrackBack URL

Leave a comment -