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 
Android Android Commands Android Java Applications Application Libraries Application Stack / User Interface Bash / Shell Scripts Commands and Packages Compilation Content Management System - CMS Core Kernel C Programs Development & Build Development Environment Setup Errors & Failures git Go Language Programs Hardware Platforms HTML JAVA Programs Kernel & Device Drivers Kernel Booting and Porting Linux, OS Concepts and Networking Linux Device Drivers Linux Host, Ubuntu, SysAdmin Linux Kernel Linux Networking Middleware Libraries, HAL Monetization / Google AdSense Moodle NDK / Middleware / HAL OS Concepts PHP Programming Languages RaspberryPi Scripting and Automation Search Engine Optimisation ( SEO ) Social Media Socurce Code Management ( SCM ) System Administration, Security Testing and Debugging Uncategorized Userspace Utilities Web design and development Wordpress Yocto / Bitbake / Openembedded

Leave a Reply / Ask Question