Linux shell if directory exists

Linux Shell Check: How to Verify If a Directory Exists or Not

Learn how to check if a directory exists or not in Linux shell with various commands including test, find, mkdir, if-then-else statement in a Bash script, and more.

  • Using the Test Command to Check If a Directory Exists
  • Checking If a Directory is Empty Using the Find Command
  • Creating a Directory Only If It Doesn’t Already Exist Using the Mkdir Command
  • Using the Test Command to Check If a File Exists
  • Using an If-Then-Else Statement in a Bash Script to Check If a Directory Exists
  • Other helpful code examples for checking if a directory does not exist in Linux shell
  • Conclusion
  • How do you check if a directory doesn’t exist?
  • How do I check if a directory is empty in shell?
  • How to check if dir exists in Bash?
  • How to check file or directory in shell script?

As a developer, system administrator or IT professional, one of the most important tasks you may need to perform on a Linux server is to verify the existence of a directory. Whether you are writing Bash scripts, performing system maintenance or running applications, you will need to check if a directory exists or not.

In this blog post, we will explore different ways to check if a directory exists or not in a Linux shell, and how to check if a directory is empty or not. We will also discuss how to create a directory only if it doesn’t already exist, how to check if a file exists, and how to use an if-then-else statement in a Bash script to check if a directory exists.

Using the Test Command to Check If a Directory Exists

The test command, also known as [ command, is a shell built-in command that can be used to check file types and compare values. We can use this command to check if a directory exists or not.

To check if a directory exists, we can use the -d option with the test command. The -d option checks if the file is a directory. If it is, it returns true, and if it’s not, it returns false.

if [ -d /path/to/directory ]; then echo "Directory exists" else echo "Directory does not exist" fi 

In the above example, we use an if-then statement to check if the directory exists or not using the -d option with the test command. If the directory exists, the script will display “Directory exists”, and if not, it will display “Directory does not exist”.

Читайте также:  Linux console format disk

We can also use the ! symbol followed by the -d option to check if a directory does not exist.

if [ ! -d /path/to/directory ]; then echo "Directory does not exist" else echo "Directory exists" fi 

In this case, we use the ! symbol to negate the -d option, which means that if the directory does not exist, the script will display “Directory does not exist”, and if it does exist, it will display “Directory exists”.

Checking If a Directory is Empty Using the Find Command

We can also use the find command to check if a directory is empty or not. The find command is used to search for files and directories in a directory hierarchy.

To check if a directory is empty, we can use the -empty option with the find command. The -empty option lists only files and directories that are empty.

if [ $(find /path/to/directory -empty) ]; then echo "Directory is empty" else echo "Directory is not empty" fi 

In the above example, we use an if-then statement to check if the directory is empty or not using the -empty option with the find command. If the directory is empty, the script will display “Directory is empty”, and if it’s not, it will display “Directory is not empty”.

Creating a Directory Only If It Doesn’t Already Exist Using the Mkdir Command

The mkdir command is used to create directories. We can use the -p option with the mkdir command to create a directory only if it doesn’t already exist.

In the above example, we use the -p option to create a directory only if it doesn’t already exist. If the directory already exists, the mkdir command does not return any errors.

Using the Test Command to Check If a File Exists

We can also use the test command to check if a file exists, regardless of its type. To check if a file exists, we can use the -f option with the test command. The -f option checks if the file is a regular file. If it is, it returns true, and if it’s not, it returns false.

if [ -f /path/to/file ]; then echo "File exists" else echo "File does not exist" fi 

In the above example, we use an if-then statement to check if the file exists or not using the -f option with the test command. If the file exists, the script will display “File exists”, and if not, it will display “File does not exist”.

We can also use the -h option with the test command to check if a symbolic link to a directory exists.

if [ -h /path/to/symbolic/link ]; then echo "Symbolic link to directory exists" else echo "Symbolic link to directory does not exist" fi 

In this case, we use the -h option to check if a symbolic link to a directory exists. If the symbolic link exists, the script will display “Symbolic link to directory exists”, and if not, it will display “Symbolic link to directory does not exist”.

Читайте также:  Linux proton vs wine

Using an If-Then-Else Statement in a Bash Script to Check If a Directory Exists

We can perform the main work of checking if a directory exists within an if-then-else statement in a Bash script.

if [ -d /path/to/directory ]; then echo "Directory exists" else echo "Directory does not exist" mkdir /path/to/directory echo "Directory created" fi 

In the above example, we use an if-then-else statement to check if the directory exists or not using the -d option with the test command. If the directory exists, the script will display “Directory exists”, and if not, it will create the directory using the mkdir command and display “Directory created”.

Other helpful code examples for checking if a directory does not exist in Linux shell

In shell, linux shell check if directory does not exists code example

if [ ! -d directory ]; then mkdir directory fi

In shell, linux command if directory exists code example

DIR="/etc/httpd/" if [ -d "$DIR" ]; then ### Take action if $DIR exists ### echo "Installing config files in $. " else ### Control will jump here if $DIR does NOT exists ### echo "Error: $ not found. Can not continue." exit 1 fi

Conclusion

In this blog post, we have explored different ways to check if a directory exists or not in a Linux shell, and how to check if a directory is empty or not. We have also discussed how to create a directory only if it doesn’t already exist, how to check if a file exists, and how to use an if-then-else statement in a Bash script to check if a directory exists.

As a developer, system administrator or IT professional, it’s important to master these techniques to perform tasks efficiently and effectively on a Linux server. We encourage you to implement the techniques covered in this blog post in your own projects to increase your productivity and improve your skills.

Источник

How To Check If File or Directory Exists in Bash

Introductory image to check file or directory in Bash.

Note: You may encounter the term bash script. This is a sequence of several commands to the shell. A script can be saved as a file and is often used to automate multiple system commands into a single action.

How to Check if a File Exists

To test for the file /tmp/test.log, enter the following from the command line:

The first line executes the test to see if the file exists. The second command, echo, displays the results 0 meaning that the file exists, 1 means no file was found.

In our example, the result was 1.

The result 1 indicates that no file was found ehan using echo command.

Now try creating a file, then testing for it:

As we have created the file beforehand, the result now is 0:

The result 0 indicates that the file is found.

You can also use square brackets instead of the test command:

How to Check if a Directory Exists

To check if a directory exists, switch out the –f option on the test command for –d (for directory):

Create that directory, and rerun the test:

This command works the same as it does for files, so using brackets instead of the test command works here also.

Читайте также:  Посмотреть версию apache linux

Note: If you are searching for a file or directory because you need to delete it, refer to our guide on removing files and directories with Linux command line.

How to Check if a File Does not Exist

Typically, testing for a file returns 0 (true) if the file exists, and 1 (false) if the file does not exist. For some operations, you may want to reverse the logic. It is helpful if you write a script to create a particular file only if it doesn’t already exist.

To create a file if one doesn’t already exist, enter the following at a command line:

[ ! –f /tmp/test.txt ] && touch /tmp/test.txt

The exclamation point ! stands for not. This command makes sure there is not a file named test.txt in the /tmp directory. You won’t see anything happen.

To see if the system created the file, enter the following:

You should see test.txt listed. You can use a similar command for a directory – replace the –f option with –d:

[ ! –d /tmp/test ] && touch /tmp/test 

How to Check for Multiple Files

To test for two files at the same time use the && option to add a second file:

[ -f /tmp/test.txt && -f /tmp/sample.txt ] && echo “Both files were found”

To test for multiple files with a wildcard, like an asterisk * to stand for various characters:

[ -f /tmp/*.jpg ] && echo “Files were found”

As usual, changing the –f option to –d lets you run the same test for multiple directories.

File Test Operators to Find Prticular Types of Files

Here are several commands to test to find specific types of files:

There are many other options available. Please consult the main page (test ––help) for additional options.

Working with Code Snippets

The previous commands work well for a simple two-line command at a command prompt. You can also use bash with multiple commands. When several commands are strung together, they are called a script.

A script is usually saved as a file and executed. Scripting also uses logical operators to test for a condition, then takes action based on the results of the test.

To create a script file, use the Nano editor to open a new file:

Enter one of the snippets from below, including the #!/bin/bash identifier. Use Ctrl-o to save the file, then Ctrl-x to exit Nano. Then, run the script by entering:

The following code snippet tests for the presence of a particular file. If the file exists, the script displays File exists on the screen.

#!/bin/bash if [ -f /tmp/test.txt ] then echo “File exists” fi

This works the same if you’re checking for a directory. Just replace the –f option with –d:

#!/bin/bash if [ -d /tmp/test ] then echo “File exists” fi

This command checks for the directory /tmp/test. If it exists, the system displays File exists.

You can now use bash to check if a file and directory exist. You can also create simple test scripts as you now understand the functions of a basic bash script file. Next, you should check out our post on Bash Function.

Источник

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