This script shows if a file exit’s or not in a given directory. This can be done using either test command or inbuilt commands such as [ or [[ commands
#!/bin/bash
#Author: Surendra Anne(surendra at linuxnix.com)
#Purpose: To check if a file exit's or not.
#Date: 2012-07-31
#Modified:: :
read -p "Please enter a file name with full path"
if [ -f $REPLY ]
then
echo "The file $REPLY exists"
else
echo "File not found"
fi
We have [ inbuilt command which will do file/folder checking with. So -f is used to check if a file exit's or not.
Save the file as chkfile1.sh and exit it.
Execute the script
Step1:change permissions to execute permissions as below
chmod +x chkfile1.sh
Step2: Executing shell script
bash chkfile1.sh
Please comment your thoughts on this.
The following two tabs change content below.
Mr Surendra Anne is from Vijayawada, Andhra Pradesh, India. He is a Linux/Open source supporter who believes in Hard work, A down to earth person, Likes to share knowledge with others, Loves dogs, Likes photography. He works as Devops Engineer with Taggle systems, an IOT automatic water metering company, Sydney . You can contact him at surendra (@) linuxnix dot com.
Latest posts by Surendra Anne (see all)
- Docker: How to copy files to/from docker container - June 30, 2020
- Anisble: ERROR! unexpected parameter type in action:
Fix - June 29, 2020 - FREE: JOIN OUR DEVOPS TELEGRAM GROUPS - August 2, 2019
- Review: Whizlabs Practice Tests for AWS Certified Solutions Architect Professional (CSAP) - August 27, 2018
- How to use ohai/chef-shell to get node attributes - July 19, 2018