DISQUS

Tech-Recipes: Determine if file exists in a Bourne/bash shell script | Bourne shell scripting | Tech-Recipes

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

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

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

    for *.gz
    do
    my_command
    done