File existence

Hi

I'm using the below command in shell script to check for file exists in the path

if [ ! -r $path/$test.csv ]
.....
fi

path and test are variables

path and the file exists but the commands inside if condition is executed (! operator used)

Is the above way of checking for file existence is correct?

Thanks

if [[ -e $path/$test.csv ]]; then
   ...
fi

Check the man page for test to see your option for switches.

1 Like

try this:

path=/home/something
filename=nameof_file
completepath=$path/$filename

if [ ! -r $completepath ] ; then
echo "NOT exits"
else
echo "Exists"
fi

Thanks it works

I have another question. Declared a variable like test="../path"

Referring the variable as $test/file_name.txt

Where does the directory 'path' needs to be created in the current directory? exists but still errors and in the directory before also exists

what does .. refers to?

.. refers to the next upper level of the current directory structure. When you have a script that is being executed from any path, it might be safer to use absolute paths instead.

1 Like