Shell Script To Check File Exists Under A Path

May 26, 2013 | By in SHELL SCRIPTS
| Reply More

This Shell script helps to check if the specified file exits under a given path. Script prompt you to provide file name and directory path.

Shell Script

#We tell user that he need to enter filename
echo -n "Please enter file to check: "
#We write filename to variable file
read file
#We tell user that he need to enter path to file
echo -n "Please enter path to check: "
#We write path to variable path
read path
#we check if we have read permission on path
if [ -r $path ]
#if we have read permissions, we check if file exist
if [ -f ${path}/${file} ]
#if file exist, we tell user
echo "File ${path}/${file} exist"
#end of if loop
#if we don't have read permissions on path
#we warn user that we don't have read permissions on path
echo "You don't have access to folder $path"

Script Output

test@server:~$ ls /tmp
haze-MsFUwz qtsingleapp-homeye-aeea-3e8
MozillaMailnews qtsingleapp-homeye-aeea-3e8-lockfile
pulse-2L9K88eMlGn7 ssh-UP2NOLoESr17
pulse-PKdhtXMmr18n unity_support_test.0

test@server:~$ ./
Please enter file to check: unity_support_test.0
Please enter path to check: /tmp
File /tmp/unity_support_test.0 exist

shell script check file


Share This :

Free Linux Ebook to Download

Leave a Reply

Commenting Policy:
Promotion of your products ? Comment gets deleted.
All comments are subject to moderation.