Determine if file exists in a Bourne/bash shell script


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

The Conversation

Follow the reactions below and share your own thoughts.

  • 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.

        • Incognito

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

      • 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

        😉

        • Drain

          You are rude.

          • sims

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

  • iGuide

    Shouldn’t that one have some brackets or something?

  • Joeblackspirit

    Perfect and simple!!

  • Anonymous

    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.

    • Anonymous

      Well, I’ve found the solution:

      for *.gz
      do
      my_command
      done

  • Jscrobinson

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

  • Shabeer

    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$

  • just where I was looking for

  • it doesn’t matter

    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? 

    • Teen

      idiot

  • NeoVG

    @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.