Search Windows and Linux Networking

Friday, March 7, 2014

How to check File or Folder exist in Bash Scripting

How to check file of folder exist in Bash scripting.


When we write any bash scripting many time, we wanted to performing any task base on validating  for file or folder is exist or not . There are many ways to check for file or folder in Bash Scripting. In following example we will see how to check file or folder exist or not to do some task.


Suppose you wanted to check if folder name Backup exist in path /home/Backup and if it does not exist then create new folder as Backup in /home directory and if it exist the display the massage saying Backup folder exist. and you also wanted to check if file name backup.tar exist in path /home/Backup directory and if it doest not exist then create tar file as backup.tar in /home/Backup directory and if it exist then display massage that saying backup.tar file exist.


!#/bin/bash

# SCRIPT: backup.sh
# AUTHOR: Your Name.
# CONTACT : Your Contact details.
# DATE: 06th Mar, 2014
# REV: 1.0 A

# PURPOSE: To check Backup folder and backup.tar file exist or not. If not exist then create  Backup directory in path /home and backup.tar file in /home/Backup direcoty.

# 1st Way to check folder exist.

test -d /home/Backup
if [ "$?" -eq 0 ]
then
   echo "\"/Backup\" Directory exist in /home"
else
   echo "\"/Backup\" Directory does not exist in /home"
   echo "Creating /\"Backup\" Directory in \"/home\" Directory."
   mkdir /home/Backup
fi


# 2nd Way to check folder exist.

if test -d /home/Backup
then
   echo "\"/Backup\" Directory exist in /home"
else
   echo "\"/Backup\" Directory does not exist in /home"
   echo "Creating /\"Backup\" Directory in \"/home\" Directory."
   mkdir /home/Backup
fi

# 3rd Way to check folder exist.

if [ -d /home/Backup ]
then
   echo "\"/Backup\" Directory exist in /home"
else
   echo "\"/Backup\" Directory does not exist in /home"
   echo "Creating /\"Backup\" Directory in \"/home\" Directory."
   mkdir /home/Backup
fi

# There are also many other ways to check folder. for like using "ls -ld /home/Backup" command .
# To check file method is same as above just replace -d option with -f if you wanted to check file and if you wanted to check link replace with -l option.

# 1st Way to check file exist.

test -f /home/Backup/backup.tar
if [ "$?" -eq 0 ]
then
   echo "\"backup.tar\" file exist in /home/Backup Directory"
else
   echo "\"backup.tar\" file does not exist in \"/home/Backup\" Directory"
   echo "Creating \"backup.tar\" file in \"/home/Backup\" Directory."
   cd /
   tar -cvpf /home/Backup/backup.tar /home/sandeep
fi

# 2nd Way to check file exist.

if test -f /home/Backup/backup.tar
then
   echo "\"backup.tar\" file exist in /home/Backup Directory"
else
   echo "\"backup.tar\" file does not exist in \"/home/Backup\" Directory"
   echo "Creating \"backup.tar\" file in \"/home/Backup\" Directory."
   cd /
   tar -cvpf /home/Backup/backup.tar /home/sandeep
fi

# 3rd way to check file exist.

if [ -f /home/Backup/backup.tar ]
then
   echo "\"backup.tar\" file exist in /home/Backup Directory"
else
   echo "\"backup.tar\" file does not exist in \"/home/Backup\" Directory"
   echo "Creating \"backup.tar\" file in \"/home/Backup\" Directory."
   cd /
   tar -cvpf /home/Backup/backup.tar /home/sandeep
fi


# END

you can use any one method which is comfortable to you .

No comments:

Post a Comment