How to Check File Exists under Directory Path using Script

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

Check File Exists - 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 ] then
#if we have read permissions, we check if file exist
if [ -f ${path}/${file} ] then
#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

[email protected]:~$ ls /tmp
haze-MsFUwz qtsingleapp-homeye-aeea-3e8
MozillaMailnews qtsingleapp-homeye-aeea-3e8-lockfile
pulse-2L9K88eMlGn7 ssh-UP2NOLoESr17
pulse-PKdhtXMmr18n unity_support_test.0

[email protected]:~$ ./
Please enter file to check: unity_support_test.0
Please enter path to check: /tmp
File /tmp/unity_support_test.0 exist


Bobbin Zachariah 6:25 pm

About Bobbin Zachariah

Founder of LinOxide, passionate lover of Linux and technology writer. Started his career in Linux / Opensource from 2000. Love traveling, blogging and listening music. Reach Bobbin Zachariah about me page and google plus page.

Author's All Posts
Like to become part of Linoxide Team and contribute tips? Contact us here.


Your email address will not be published. Required fields are marked *

All comments are subject to moderation.