How to use Condition if file or directory is exist on Bash Script
- 00:44 cat /etc/os-release
- 01:02 vim filecheck
- 03:51 chmod +x filecheck
- 03:59 ll
- 04:14 ./filecheck
- 04:28 vim filecheck1
- 07:26 chmod +x filecheck1
- 07:38 ll
- 07:54 ./filecheck1
- 08:15 ll
- 08:31 vim dircheck
- 12:00 chmod +x dircheck
- 12:16 ll
- 12:31 ./dircheck
- 12:56 vim dircheck1
- 16:53 chmod +x dircheck1
- 17:03 ll
- 17:24 ./dircheck1
- 17:59 ll
To Use Condition If File Or Directory Is Exist On Bash Script
Introduction
Bash is used to automate regularly executed commands in Linux. Commands that are meant for operations on files/directories are quite frequently used, and usually, before we execute such a command, we need to check whether the particular file or directory exists.
Procedure:
Step 1 Check the Oracle Linux Version by using the below command
[root@linuxhelp ~]# cat /etc/os-release
NAME="Oracle Linux Server"
VERSION="9.2"
ID="ol"
ID_LIKE="fedora"
VARIANT="Server"
VARIANT_ID="server"
VERSION_ID="9.2"
PLATFORM_ID="platform:el9"
PRETTY_NAME="Oracle Linux Server 9.2"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:oracle:linux:9:2:server"
HOME_URL="https://linux.oracle.com/"
BUG_REPORT_URL="https://github.com/oracle/oracle-linux"
ORACLE_BUGZILLA_PRODUCT="Oracle Linux 9"
ORACLE_BUGZILLA_PRODUCT_VERSION=9.2
ORACLE_SUPPORT_PRODUCT="Oracle Linux"
ORACLE_SUPPORT_PRODUCT_VERSION=9.2
Step 2: Next create an one script file for Check if the file is exist
[root@linuxhelp ~]# vim filecheck
# !/bin/bash
# Check if the 'filecheck' exists
file="/root/filecheck"
if [ -f "$file" ]
then
# The 'filecheck' exists, so print a message
echo "The 'filecheck' exists."
else
# The 'filecheck' does not exist, so print a message and create it
echo "The 'filecheck' does not exist. Creating it now..."
touch $file
fi
Step 3: Execute the script first change the executable file permission by using the following command
[root@linuxhelp ~]# chmod +x filecheck
Step 4: List the file by using the following commands
[root@linuxhelp ~]# ll
total 12
-rw-r--r--. 1 root root 329 Oct 3 14:16 @
-rw-------. 1 root root 1067 Oct 3 08:10 anaconda-ks.cfg
drwxr-xr-x. 2 root root 6 Oct 3 13:33 Desktop
drwxr-xr-x. 2 root root 6 Oct 3 13:33 Documents
drwxr-xr-x. 2 root root 6 Oct 3 13:33 Downloads
-rwxr-xr-x. 1 root root 329 Oct 3 14:15 filecheck
drwxr-xr-x. 2 root root 6 Oct 3 13:33 Music
drwxr-xr-x. 2 root root 6 Oct 3 13:33 Pictures
drwxr-xr-x. 2 root root 6 Oct 3 13:33 Public
drwxr-xr-x. 2 root root 6 Oct 3 13:33 Templates
drwxr-xr-x. 2 root root 6 Oct 3 13:33 Videos
Step 5: Execute the script by using the following command. If the file is exists this will be the output
[root@linuxhelp ~]# ./filecheck
The 'filecheck' exists.
Step 6: Next create a one script file for Check if file is not exist
[root@linuxhelp ~]# vim filecheck1
# !/bin/bash
# Check if the 'filecheck2' exists
file=" /root/filecheck2"
if [ -f "$file" ]
then
# The 'filecheck2' exists, so print a message
echo "The 'filecheck2' exists."
else
# The 'filecheck2' does not exist, so print a message and create it
echo "The 'filecheck2' does not exist. Creating it now..."
touch $file
fi
Step 7: Execute the script first change the executable file permission by using the following command
[root@linuxhelp ~]# chmod +x filecheck1
Step 8: List the file by using the following commands
[root@linuxhelp ~]# ll
total 16
-rw-r--r--. 1 root root 329 Oct 3 14:16 @
-rw-------. 1 root root 1067 Oct 3 08:10 anaconda-ks.cfg
drwxr-xr-x. 2 root root 6 Oct 3 13:33 Desktop
drwxr-xr-x. 2 root root 6 Oct 3 13:33 Documents
drwxr-xr-x. 2 root root 6 Oct 3 13:33 Downloads
-rwxr-xr-x. 1 root root 335 Oct 3 14:20 filecheck
-rwxr-xr-x. 1 root root 343 Oct 3 14:25 filecheck1
drwxr-xr-x. 2 root root 6 Oct 3 13:33 Music
drwxr-xr-x. 2 root root 6 Oct 3 13:33 Pictures
drwxr-xr-x. 2 root root 6 Oct 3 13:33 Public
drwxr-xr-x. 2 root root 6 Oct 3 13:33 Templates
drwxr-xr-x. 2 root root 6 Oct 3 13:33 Videos
Step 9: Execute the script by using the following command. If the file is not exists this will be the output
[root@linuxhelp ~]# ./filecheck1
The 'filecheck2' does not exist. Creating it now...
Step 10: In the script file if the file is no exist and next condition is create that file. to check the file is created or not by using the following command
[root@linuxhelp ~]# ll
total 16
-rw-r--r--. 1 root root 329 Oct 3 14:16 @
-rw-------. 1 root root 1067 Oct 3 08:10 anaconda-ks.cfg
drwxr-xr-x. 2 root root 6 Oct 3 13:33 Desktop
drwxr-xr-x. 2 root root 6 Oct 3 13:33 Documents
drwxr-xr-x. 2 root root 6 Oct 3 13:33 Downloads
-rwxr-xr-x. 1 root root 335 Oct 3 14:20 filecheck
-rwxr-xr-x. 1 root root 343 Oct 3 14:25 filecheck1
drwxr-xr-x. 2 root root 6 Oct 3 14:26 filecheck2
drwxr-xr-x. 2 root root 6 Oct 3 13:33 Music
drwxr-xr-x. 2 root root 6 Oct 3 13:33 Pictures
drwxr-xr-x. 2 root root 6 Oct 3 13:33 Public
drwxr-xr-x. 2 root root 6 Oct 3 13:33 Templates
drwxr-xr-x. 2 root root 6 Oct 3 13:33 Videos
Step 11: Next create a one script file for Check if the directory is exist
[root@linuxhelp ~]# vim dircheck
# !/bin/bash
# Check if the /linuxhelp directory exists
directory=" /linuxhelp"
if [ -d "$directory" ]
then
# The /linuxhelp directory exists, so print a message
echo "The /linuxhelp directory exists."
else
# The /linuxhelp directory does not exist, so print a message and create it
echo "The /linuxhelp directory does not exist. Creating it now..."
mkdir $directory
fi
Step 12: To execute the script first change the executable file permission by using the following command
[root@linuxhelp ~]# chmod +x dircheck
Step 13: To list the file by using the following commands
[root@linuxhelp ~]# ll
total 20
-rw-r--r--. 1 root root 329 Oct 3 14:16 @
-rw-------. 1 root root 1067 Oct 3 08:10 anaconda-ks.cfg
drwxr-xr-x. 2 root root 6 Oct 3 13:33 Desktop
-rwxr-xr-x. 1 root root 395 Oct 3 16:19 dircheck
drwxr-xr-x. 2 root root 6 Oct 3 13:33 Documents
drwxr-xr-x. 2 root root 6 Oct 3 13:33 Downloads
-rwxr-xr-x. 1 root root 335 Oct 3 14:20 filecheck
-rwxr-xr-x. 1 root root 343 Oct 3 14:25 filecheck1
drwxr-xr-x. 2 root root 6 Oct 3 14:26 filecheck2
drwxr-xr-x. 2 root root 6 Oct 3 13:33 Music
drwxr-xr-x. 2 root root 6 Oct 3 13:33 Pictures
drwxr-xr-x. 2 root root 6 Oct 3 13:33 Public
drwxr-xr-x. 2 root root 6 Oct 3 13:33 Templates
drwxr-xr-x. 2 root root 6 Oct 3 13:33 Videos
Step 14: To execute the script by using the following command. If the Directory is exists this will be the output
[root@linuxhelp ~]# ./dircheck
The /linuxhelp directory exists.
Step 15: Next create a one script file for Check if file is not exist
[root@linuxhelp ~]# vim dircheck1
# !/bin/bash
# Check if the /linuxhelp1 directory exists
directory=" /linuxhelp1"
if [ -d "$directory1" ]
then
# The /linuxhelp1 directory exists, so print a message
echo "The /linuxhelp1 directory exists."
else
# The /linuxhelp1 directory does not exist, so print a message and create it
echo "The /linuxhelp1 directory does not exist. Creating it now..."
mkdir $directory
fi
Step 16: To execute the script first change the executable file permission by using the following command
[root@linuxhelp ~]# chmod +x dircheck1
Step 17: To list the file by using the following commands
[root@linuxhelp ~]# ll
total 24
-rw-r--r--. 1 root root 329 Oct 3 14:16 @
-rw-------. 1 root root 1067 Oct 3 08:10 anaconda-ks.cfg
drwxr-xr-x. 2 root root 6 Oct 3 13:33 Desktop
-rwxr-xr-x. 1 root root 391 Oct 3 16:22 dircheck
-rwxr-xr-x. 1 root root 399 Oct 3 16:28 dircheck1
drwxr-xr-x. 2 root root 6 Oct 3 13:33 Documents
drwxr-xr-x. 2 root root 6 Oct 3 13:33 Downloads
-rwxr-xr-x. 1 root root 335 Oct 3 14:20 filecheck
-rwxr-xr-x. 1 root root 343 Oct 3 14:25 filecheck1
drwxr-xr-x. 2 root root 6 Oct 3 14:26 filecheck2
drwxr-xr-x. 2 root root 6 Oct 3 16:21 linuxhelp
drwxr-xr-x. 2 root root 6 Oct 3 13:33 Music
drwxr-xr-x. 2 root root 6 Oct 3 13:33 Pictures
drwxr-xr-x. 2 root root 6 Oct 3 13:33 Public
drwxr-xr-x. 2 root root 6 Oct 3 13:33 Templates
drwxr-xr-x. 2 root root 6 Oct 3 13:33 Videos
Step 18: To execute the script by using the following command. If the directory is not exists this will be the output
[root@linuxhelp ~]# ./dircheck1
The '/linuxhelp1' does not exist. Creating it now...
Step 19: In the script file if the directory is no exist and next condition is create that file. to check the directory is created or not by using the following command
[root@linuxhelp ~]# ll
total 20
-rw-------. 1 root root 1067 Oct 3 08:10 anaconda-ks.cfg
drwxr-xr-x. 2 root root 6 Oct 3 13:33 Desktop
-rwxr-xr-x. 1 root root 369 Oct 4 07:30 dircheck
-rwxr-xr-x. 1 root root 375 Oct 4 07:36 dircheck1
drwxr-xr-x. 2 root root 6 Oct 3 13:33 Documents
drwxr-xr-x. 2 root root 6 Oct 3 13:33 Downloads
-rwxr-xr-x. 1 root root 318 Oct 4 07:21 filecheck
-rwxr-xr-x. 1 root root 326 Oct 4 07:25 filecheck1
drwxr-xr-x. 2 root root 6 Oct 4 07:26 filecheck2
drwxr-xr-x. 2 root root 6 Oct 4 07:31 linuxhelp
drwxr-xr-x. 2 root root 6 Oct 4 07:38 linuxhelp1
drwxr-xr-x. 2 root root 6 Oct 3 13:33 Music
drwxr-xr-x. 2 root root 6 Oct 3 13:33 Pictures
drwxr-xr-x. 2 root root 6 Oct 3 13:33 Public
drwxr-xr-x. 2 root root 6 Oct 3 13:33 Templates
drwxr-xr-x. 2 root root 6 Oct 3 13:33 Videos
Conclusion:
We have reached the end of this article. In this guide, we have walked you through the steps required to use Condition if file or directory is exist. Your feedback is much welcome.
Comments ( 0 )
No comments available