Check if file exist Linux bash

17,053

Solution 1

You are using absolute paths in your test while you should be using relative paths:

 if [ -e ./temp/file.txt ]; then

Solution 2

/temp/file.txt vs /docs/text/temp/file.txt?

Solution 3

You script looks in /temp while you are looking in /docs/text/temp

Share:
17,053
Painguy
Author by

Painguy

Updated on June 08, 2022

Comments

  • Painguy
    Painguy about 2 years

    So I'm trying to check if a file exists or not and then the script is supposed to do something if it does. The problem I'm having is actually getting it to recognize that something is actually there.

    if [ -e /temp/file.txt ]; then
            echo "file found!"
            sudo cp -r temp/* extra
    else
            echo "file not found! Creating new one..."
            ./create.sh
    fi
    

    below is an example of the files in the directory I'm testing. they are clearly there, but for some reason I can't get the script to see that. what am I doing wrong?

    nima@mkt:/docs/text$ ls -a temp
    .  ..  more  file.txt  file2.txt