Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if file exist Linux bash [duplicate]

Tags:

file

linux

bash

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
like image 869
Painguy Avatar asked Dec 07 '22 14:12

Painguy


2 Answers

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

 if [ -e ./temp/file.txt ]; then
like image 79
Dmitri Chubarov Avatar answered Dec 16 '22 08:12

Dmitri Chubarov


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

like image 20
John Carter Avatar answered Dec 16 '22 08:12

John Carter