Check for the Existence of a File or Folder in Bash: A Complete Guide

Introduce

There are several functions in Linux that only work when a specific file or directory exists. Bash is a shell that interprets commands. You can use one

How does Bash check if a file or directory exists? In this guide, learn how to use the bash command to check if a file or directory exists.

Prerequisite

  • A Linux operating system
  • Accessing the Terminal Window/Command Line (Ctrl-Alt-T / Ctrl-Alt-F2)

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

How to check if a file exists

Bash checks for the existence of a file or directory: To test the file /tmp/test.log, enter the following from the command line:

test –f /tmp/test.txt

The first line performs a test to see if the file exists. The second command echo shows a result of 0 indicating that the file exists and 1 indicating that the file is not found.

echo $?

In our example, the result is 1.

Bash checks if a file or directory exists Example: now try to create a file, and then test it:

touch /tmp/test.txt
test –f /tmp/test.txt
echo $?

Since we created the file beforehand, the result is now 0:

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

[ –f /tmp/test.txt ]
echo $?

How to check if a directory exists

How does Bash check if a file or directory exists? To check if a directory exists, turn off the –f option in the test command for –d (for directories):

test –d /tmp/test
echo $?

Create the directory and re-run the test:

touch /tmp/test
test –d /tmp/test
echo $?

This command works in the same way as a file, so using square brackets instead of the test command will work here as well.

Note: If you’re searching for a file or directory because you need to delete it, see our guide to deleting files and directories using the Linux command line.

How to check if a file doesn’t exist

Bash checks for the existence of a file or directory: Typically, a test for a file returns 0 (true) if the file exists, and 1 (false) if the file does not exist. For some operations, you may need to reverse the logic. It would be helpful if you wrote a script to create a particular file only if it didn’t exist.

To create a file that doesn’t exist, enter the following on the command line:

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

Exclamation markRepresentatives do not. This command ensures that there is no file named test.txt in the /tmp directory directory. You won’t see anything happening.

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

ls /tmp

Bash checks if a file or directory exists for examples: you should see test.txt listed. You can use a similar command for directories – replace the -f option with -d:

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

How to check multiple files

How does Bash check if a file or directory exists? To test two files at the same time, add a second file using the &&&> option:

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

To test multiple files with wildcards, e.g. asterisks * for various characters:

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

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

File test operators to find specific types of files

Here are a few commands for testing to find a specific type of file:

  • -f – file
  • -d – directory
  •  L – Symbolic link
  • -r – readable
  • -x – executable file
  • – w^ – writes

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

Use code snippets

Bash checks for the existence of a file or directory: The previous command works with a simple two-line command in the command prompt. You can also use bash with multiple commands. When multiple commands are strung together, they are called scripts.

script is usually saved as a file and executed. The script also uses logical operators to test the condition and then act on the test results.

To create a script file, open a new file using the Nano Editor:

sudo nano bashtest.sh

Enter one of the snippets below, including the #!/bin/bash identifier. Use Ctrl-o to save the file, and then use Ctrl-x to exit Nano. Then, run the script with the following command:

bash bashtest.sh

How does Bash check if a file or directory exists? The following code snippet tests the existence of a specific file. If the file exists, the script will show the file on the screen.

#!/bin/bash

if [ -f /tmp/test.txt ]

then

echo “File exists”

fi

Bash checks for the presence of a file or directory example: This is the same if you’re checking a directory. Just replace the -f option with -d:

#!/bin/bash

if [ -d /tmp/test ]

then 

echo “File exists”

fi

This command checks the directory /tmp/test. If it exists, the system shows that the file exists.

Conclusion

You can now use bash to check if files and directories exist. You can also create simple test scripts, as you now understand the functionality of a basic bash script file.