4

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
Sam
  • 7,252
  • 16
  • 46
  • 65
Painguy
  • 565
  • 6
  • 13
  • 25

3 Answers3

12

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

 if [ -e ./temp/file.txt ]; then
Dima Chubarov
  • 16,199
  • 6
  • 40
  • 76
2

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

John Carter
  • 53,924
  • 26
  • 111
  • 144
1

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

Sven
  • 22,475
  • 4
  • 52
  • 71