Linux check path exists

How Do I Check if a Directory Exists or Not in a Bash Shell Script?

In Linux, checking the existence of the directory is necessary before performing any operation on it. Fortunately, the bash script facilitates the user to check the existence of the particular directory by making a script. The user can examine the “d” operator to deal with the directory-related tasks in the bash script.

The subsequent post will describe the methods to check the existence of the directory in a Bash Shell Script.

  • Using -d Operator
  • Using -d and && Operator
  • How to Deal with Directory Having Symbolic Link?

Method 1: Check the Existence of the Directory Using -d Operator

The following script will take the directory path/name from the user and will display the appropriate message about the existence of the directory:

#!/bin/bash read -p "Enter Directory Path/Name: " DIR if [ -d "$DIR" ]; then echo "The Given Directory $DIR Exists." else echo "There is no Existence of the Directory $DIR" fi

  • The “read” property to take the directory path/name from the users in “DIR.”
  • The “d” operator in the “if” to check the existence of the directory entered by the user.
  • The “then” section prints messages through echo if the directory exists.
  • The “else” portion displays the message through echo if the directory doesn’t exist.

Save the script file and run it using the bash command in the terminal:

The given directory “Henry” exists as shown.

Method 2: Check the Existence of the Directory Using ! and -d

The user can also use the “!” operator to check the directory existence as shown in the following script:

#!/bin/bash read -p "Enter Directory Path/Name: " DIR if [ ! -d "$DIR" ]; then echo "There is no Existence of the Directory $DIR" else echo "The Given Directory $DIR Exists." fi

Save the above script and run it in the terminal:

The given directory path “/home/dir” doesn’t exist.

Method 3: Check the Existence of the Directory Using -d and && Operator

Another possible way to check the existence of the directory is by using the “d” and “&&” operators as utilized in the below script:

#!/bin/bash read -p "Enter Directory Name/Path: " DIR [ -d "$DIR" ] && echo "$DIR Exist."

  • The “read” property to take the directory path/name from the users in “DIR.”
  • The “[ -d “$DIR” ]” expression to check the existence of the entered directory.
  • The “&&” operator and echo statement is only executed if the directory exists.
Читайте также:  Узнать свой ip linux команда

Save the above script in the file and run it in the terminal:

The entered directory “Downloads” exist in the directory.

How to Deal With Directory Having Symbolic Link?

Sometimes the entered directory may be a symbolic link directory, and the bash script also deals with the symbolic link directory through the “L” operator. The user needs to use the nested if/else in the bash script for that purpose. The following script will check whether the entered directory is a symbolic link or a regular directory:

#!/bin/bash read -p "Enter Directory Path/Name: " DIR if [ -d "$DIR" ]; then if [ -L "$DIR" ]; then echo "The $DIR is a Symbolic Link Directory." else echo "The $DIR is a Regular Directory." fi else echo "There is no Existence of the Directory $DIR" fi

  • The “read” property to take the directory path/name from the users in “DIR.”
  • The “d” operator in the “if” to check the existence of the directory entered by the user.
  • The “then” section further checks if the directory is a symbolic link or a regular directory using the “L” operator.
  • The last “else” portion will be executed if the directory doesn’t exist.

Save the script in a file and exit from the editor.

Check the script by entering a symbolic link directory:

The given directory “S-Henry” is a symbolic link directory.

Conclusion

In the bash script, to check whether the directory exists or not, utilize the “d” operator or “L” operator if the directory is symbolically linked. The user can also use the “!” and “&&” operator along with the “d” and “L” operators to check the existence of the directory.

This write-up has illuminated the examples to check the existence of the directory in the bash script.

Источник

Bash Check If Directory Exists

Directories and folders are the main and quite important parts of any operating system. Without the directories and files, our system doesn’t get completed. The directories are used to store the sub-folders and files that hold data in them for security and personal work. Within the Linux operating system, we have also got the same file system i.e., directories and sub-folders. Bash programming came up with some of the very simple commands and statements to check if the specific directory of a file exists in our system or not. Therefore, we have decided to write this article to check if the directory exists in our Linux system or not.

Читайте также:  Операционная система linux фирма

Example 01: Check If File Exists

Let’s get started with the basic example. We will be having a look at checking a simple file in our Linux system first i.e., if exists or not. Therefore, we have been creating a new text type file named “new.txt” within Ubuntu’s home folder with the “touch” instruction. We have added a one-line text in the file and displayed it on the shell using the “cat” instruction shown below. The output of the below-stated command is attached in the image.

Now, it’s time to create a new bash file with the “touch” instruction named “direc.sh” as below. We need to open this empty file to start coding in it. For this, we have been using the “nano” instruction to launch it within the GNU Nano editor. The output of the below-stated command is attached in the image.

Now, the empty file has been opened in the nano editor. Within the first line of code, we have initialized a file variable “F” holding a path to a file “new.txt” as “/home/linux/new.txt”. The “if-then” statement of bash has been utilized here to check if the file “new.txt” exists or not. The “if” clause is started with the keyword “test” followed by the flag “-f” for files. Within inverted commas, we have added the variable “$F”. After this, the “then” clause started with the “echo” statement using the variable name to show if it exists or not. The “then” part of the “if-then” statement will only be executed when the condition “if” will be true.

Let’s run the bash file using the “bash” keyword followed by the name of a file “direc.sh”. As the file exists in the home directory of our system, thus it executed the echo statement and is showing that the file exists. The output of the below-stated command is attached in the image.

The same thing can be achieved with the use of square brackets around the condition of the “if” clause without using the keyword “test” as shown below. Let’s execute it to see its result in the bash output screen within the shell.

After running this updated code, we have got the very same result i.e. file exists. The output of the below-stated command is attached in the image.

Example 02: Check If Directory Exists

Let’s take a look at the code that is used to check if the directory of the folder exists in our system or not. For that, we will be using a purely new folder. Therefore, within the terminal shell query area, we have tried the “mkdir” command to create a new directory named “new”. This newly created directory will be used within our code to check if it exists or not. The list command is executed to see all the existing directories and files in the home folder. We can see the “new” directory listed in the shown output beneath the “Music” folder and after the “Downloads”. The output of the below-stated command is attached in the image.

Читайте также:  Linux usr local sbin

Let’s open up the same “direc.sh” file in Ubuntu’s nano editor to create a new code. After the file is launched, we need to create a new directory variable “D” holding a path to a newly created directory named “new” as “/home/Linux/new”. The overall work to check the directory existence has been done within the “if-then-else” statement of bash. So, the “if” statement has been started with the condition to check the directory in a system using the “-d” flag for “directory” along with the directory variable in inverted commas. This condition has been utilized within the square brackets. If the condition got satisfied and the directory exists, the “then” statement will be executed along with its “echo” statement. Otherwise, the “else” part of the statement will be utilized along with its “echo” statement showing that the file doesn’t exist. The overall statement will be closed by the “fi” keyword as shown below.

Now, it’s time to run our bash code in the terminal shell using the “bash” query shown in the image. After running it, we have got the success message showing that the directory exists. The output of the below-stated command is attached in the image.

If you want to achieve the else part execution in the shell terminal, you must have to delete the directory so that the condition doesn’t get satisfied. Therefore, we have deleted the newly made empty directory “new” from the home folder of our Ubuntu 20.04 system. After this, we have listed the contents of the home folder using the list command and found that there is no directory of the name “new” as below. After running the same “direc.sh” bash file with the “bash” instruction, we have got the output showing that the else part of the code has been executed i.e., directory doesn’t exist.

Conclusion

Finally! We have done the explanation of checking out if the directory exists in our Ubuntu 20.04 system or not. For this, we have tried the bash script to achieve our goal. We have also discussed the use of “-f” for file checking and “-d” for directory checking in the system. All the examples are simple and according to our user choice.

About the author

Omar Farooq

Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.

Источник

Оцените статью
Adblock
detector