• 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 exists on Bash Script on Ubuntu 22.04

  • 00:48 lsb_release -a
  • 01:05 vim test.sh
  • 02:21 chmod +x test.sh
  • 02:27 ./test.sh
  • 02:39 vim test.sh
  • 02:51 ./test.sh
  • 03:00 vim test.sh
  • 03:59 ./test.sh
  • 04:07 vim test.sh
  • 04:20 ./test.sh
  • 04:28 vim test.sh
  • 07:02 ./test.sh /etc/passwd
  • 07:11 ./test.sh /etc/passws
{{postValue.id}}

To Use Condition If A File Or Directory Exists On Bash Script On Ubuntu 22.04

Introduction:

By using bash script you can easily find out if a regular file does or does not exist in Bash shell. The -d flag tests whether the provided name exists and is a directory. To test for regular files instead, we can use the -f flag. To test for both files and directories, we can use the -e flag.

Procedure:

Step 1: Check the OS version by using the below command

root@linuxhelp:~# lsb_release -a
No LSB modules are available.
Distributor ID:	Ubuntu
Description:	Ubuntu 22.04.3 LTS
Release:	22.04
Codename:	jammy

Step 2: Create a file by using Vim editor by using the below command

root@linuxhelp:~# vim test.sh
Add the following lines in the file
#!/bin/bash
[ -f /etc/resolve.conf ] &&
echo "File exists" ||
echo "File does not exists"

Step 3: Now give execute permission by using the following command

root@linuxhelp:~# chmod +x test.sh

Step 4: Run the script by using the below command

root@linuxhelp:~# ./test.sh
File does not exist

Step 5: Change the line in the file

root@linuxhelp:~# vim test.sh
[ -f /etc/resolv.conf ] &&

Step 6: Again run the script by using the below command

root@linuxhelp:~# ./test.sh
File exit

Step 7: Now search for the directory, For that edit the file by using the below command

root@linuxhelp:~# vim test.sh
Add the lines in the file
#!/bin/bash
[ -d /var/logs ] &&
echo "Directory exist" ||
echo "Directory does not exist"

Step 8: Run the script by using the below command

root@linuxhelp:~# ./test.sh 
Directory does not exist

Step 9: Change the line in the file by using the below command

root@linuxhelp:~# vim test.sh
[ -d /var/log ] &&

Step 10: Run the script by using the below command

root@linuxhelp:~# ./test.sh 
Directory is  exist

Step 11: Now use the if condition in the file to that edit the file

root@linuxhelp:~# vim test.sh
#!/bin/bash
FILE="$1"
if [ -f "$FILE" ]
then
        echo "File $FILE exist"
else
        echo "$FILE does not exist" >&2
fi

Step 12: Now run the script with an argument

root@linuxhelp:~# ./test.sh /etc/passwd
File /etc/passwd exist

Step 13: Again run the script with a different argument

root@linuxhelp:~# ./test.sh /etc/passws
/etc/passws does not exist

Conclusion:

We have reached the end of this article. In this guide, we have walked you through the steps required to use Condition if a file or directory exists on Bash Script on Ubuntu 22.04. Your feedback is much welcome.

Tags:
lucas
Author: 

Comments ( 0 )

No comments available

Add a comment
{{postCtrl.cmtErrMsg}}

Frequently asked questions ( 5 )

Q

What are bash scripts used for?

A

A bash script is a file containing a sequence of commands that are executed by the bash program line by line. It allows you to perform a series of actions, such as navigating to a specific directory, creating a folder, and launching a process using the command line.

Q

What is the first line of the bash script?

A

The first line (/bin/bash) is used in every bash script.

Q

How do I exit a Bash script?

A

We use exit to exit from a Bash script.

Q

How do I check if a file exists in Bash?

A

if test -f /path/to/file; then echo "File exists." fi. To check whether a file does not exist

Q

How to read the value in Bash?

A

To read the Bash user input, we use the built-in Bash command called read.

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 Jayce ?
What are the types of table used in IPtables

What are the various types of table used in IPtables and how to use that for my server security?

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.