Determine if file exists in a Bourne/bash shell script

Contributor Icon Contributed by Rex Date Icon October 20, 2003  
Tag Icon Tagged: Bourne shell scripting


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

Previous recipe | Next recipe |
 
  • DaVince
    Just what I needed, thanks.
  • anonymous
    if [ -f testfile ];
    then
    echo Blablabla
    fi
  • James
    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.
  • Adam
    I don't think you're using it quite right Young Padawan
  • ayw
    if [ -e testfile ]; tests whether a file exists.

    if [ -f testfile ]; only tests whether file is a regular file
  • David
    No. From the man page: " -f filename
    True if filename exists and is a regular file.""
  • Third Rook
    Regardless what the man page says. -f didn't work in my script, and -e did.

    Thanks ayw.
  • Shouldn't that one have some brackets or something?
  • Joeblackspirit
    Perfect and simple!!
  • dish
    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.
  • dish
    Well, I've found the solution:

    for *.gz
    do
    my_command
    done
blog comments powered by Disqus