• Categories
    Category
    {{ postCtrl.tags }}
    • {{ category.tag_type }}

      • {{tag.tag_name}}
      • View more
  • Categories
    Category
    {{ postCtrl.tags }}
    • {{ category.tag_type }}

      • {{tag.tag_name}}
      • View more
  • News
  • Tutorials
  • Forums
  • Tags
  • Users
Tutorial News Comments FAQ Related Articles

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
{{postValue.id}}

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.

Tags:
gabriel
Author: 

Comments ( 0 )

No comments available

Add a comment
{{postCtrl.cmtErrMsg}}

Frequently asked questions ( 5 )

Q

What is the shebang line in shell scripting?

A

Shell scripts or programs often contain shebang at the top. In general, the Shebang (the series of characters "#!") is used to indicate the operating system to use a particular interpreter to process the rest of the file. Here, the symbol '#' is referred to as hash, and "!" is referred to as bang. This usually aids developers in avoiding repetitive work. Scripts are generally executed by the engine; therefore, shebang determines the location of the engine that will be used to run the script.

Q

How can conditional statements be used in bash?

A

The most common conditional statement in most programming languages is the if-elseif-else statement. The syntax of if-elseif-else statement in bash is a little bit different from other programming languages. ‘If’ statement can be declared in two ways in a bash script and every type of ‘if’ block must be closed with ‘fi’. ‘if’ statement can be defined by third brackets or first brackets like other programming languages.

Q

How to check a directory exists or not using bash?

A

Bash has many test commands to check if a file or directory exists or not and the type of the file. ‘-d’ option is used with a directory path as a conditional statement to check if the directory exists or not in bash. If the directory exists, then it will return true otherwise it will return false.

Q

How to make a bash file executable?

A

Executable bash files can be made by using ‘chmod’ command. Executable permission can be set by using ‘+x’ in chmod command with the script filename. Bash files can be executed without the explicit ‘bash’ command after setting the execution bit for that file.

Q

How to check a File exists or not using bash?

A

Bash has many test commands to check if a file or directory exists or not and the type of the file. ‘-f’ option is used with a file path as a conditional statement to check if the file exists or not in bash. If the file exists, then it will return true otherwise it will return false.

Back To Top!
Rank
User
Points

Top Contributers

userNamenaveelansari
135850

Top Contributers

userNameayanbhatti
92510

Top Contributers

userNamehamzaahmed
32150

Top Contributers

1
userNamelinuxhelp
31040

Top Contributers

userNamemuhammadali
24500
Can you help keel johnston ?
Unhide the folders on windows Explorer

Give any solutions to unhide folder using command prompt?

forum3

Networking
  • Routing
  • trunk
  • Netmask
  • Packet Capture
  • domain
  • HTTP Proxy
Server Setup
  • NFS
  • KVM
  • Memory
  • Sendmail
  • WebDAV
  • LXC
Shell Commands
  • Cloud commander
  • Command line archive tools
  • last command
  • Shell
  • terminal
  • Throttle
Desktop Application
  • Linux app
  • Pithos
  • Retrospect
  • Scribe
  • TortoiseHg
  • 4Images
Monitoring Tool
  • Monit
  • Apache Server Monitoring
  • EtherApe 
  • Arpwatch Tool
  • Auditd
  • Barman
Web Application
  • Nutch
  • Amazon VPC
  • FarmWarDeployer
  • Rukovoditel
  • Mirror site
  • Chef
Contact Us | Terms of Use| Privacy Policy| Disclaimer
© 2025 LinuxHelp.com All rights reserved. Linux™ is the registered trademark of Linus Torvalds. This site is not affiliated with linus torvalds in any way.