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 Bash / Shell Scripts Bluetooth driver Build Frameworks Commands and Packages Core Kernel C Programs Development Environment Setup Documents / Books Errors & Failures File Systems Framebuffer / Display Driver git Go Language Programs Hardware Platforms Home 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 NDK / Middleware / HAL Network Driver OS Concepts PHP Procfs Filesystem Programming Languages RaspberryPi Scripting and Automation Search Engine Optimisation ( SEO ) Socurce Code Management ( SCM ) System Administration, Security Testing and Debugging Uncategorized Userspace Utilities Web design and development Wordpress Yocto / Bitbake / Openembedded

Leave a Reply