shell script to check if file is present or not in a directory

Open your favorite editor and create a script as below,

#!/bin/sh

#get the name of file to check if its present or not in pwd
FILE_TO_CHECK=$1

if [ ! -f $FILE_TO_CHECK ]
then
echo "file $FILE_TO_CHECK does not exits"
else
echo "file $FILE_TO_CHECK is present"
fi

Now, lets say you want to search myfile.pdf in a big list of files in a directory, then cd to that directory, copy this script there, and run the script as,

 $ bash script_to_check_file.sh myfile.pdf 

Leave a Comment