There are several functions in Linux that only work if a particular file or directory exists. Bash is a shell that interprets commands. You can use a
In this guide, learn how to use a bash command to check if a file or directory exists.
- A Linux operating system
- Access to a terminal window / command line (Ctrl-Alt-T / Ctrl-Alt-F2)
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:
test –f /tmp/test.txt
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.
Now try creating a file, then testing for it:
test –f /tmp/test.txt
As we have created the file beforehand, the result now is 0:
You can also use square brackets instead of the test command:
[ –f /tmp/test.txt ]
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):
test –d /tmp/test
Create that directory, and rerun the test:
test –d /tmp/test
This command works the same as it does for files, so using brackets instead of the test command works here also.
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:
- -f – file
- -d – directory
- –L – symbolic link
- -r – readable
- -x – executable
- –w – writable
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:
sudo nano bashtest.sh
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.