Home / Linux/Unix/BSD / Bash Check if File Exists

Bash Check if File Exists

Are you working with shell scripting and Bash? This article is for you. At some point in your work, you’ll need to check your file system. Is your directory still there? Not sure if your file exists anymore? These are the two essential questions you’ll need to know.

How do you know if your directory or file exists with Bash? The answer is simple, do a “Bash Test.” Today, we’ll teach you how to do just that. By the end of this article, you should check for yourself whether a file or directory still exists in your Bash script.

Test if a Regular File Exists

The first concern you should tackle is whether your regular file or directory still exists in Bash script. To answer this question, you’ll need to do the test command.

If you want to check if your file exists, you’ll need to use the “-f” file option. Make sure to indicate which file you’re trying to look for.

We will be using “/user/commrev” as a file example throughout the article.

So, for example, you’re trying to test if the “/user/commrev” file exists. On your bash script, you’ll have to write it down as:

if [[-f "/user/commrev"]].

If the file exists, then the next command prompt you’ll see should be,

else echo "<file> exists on your file system."

Using short terms

You can also directly test your bash shell by using and existing short terms. Use the same symbolic link “-f” option, and type the command you want to run. So you’ll need to open up your test command and write the following as

if [[ -f //user/commrev ]] && echo "This file exists!"

Checking if more then one file exists

In case you want to check if more than one file exists, you can still do so. Similar to the above situations, use “-f” and test if a file you’re trying to locate is still there. Separate the files by using the “&&” operator.

For example, you want to test if 1 file 1 and 2 file exists. You can type it on the test command as follows:

if [[ - f <file 1> ]] && [[-f <file> ]] then
echo "file exists!" fi.

Easy! Make sure to remember the “-f” command and file name you’re looking for.

Check if a File Does Not Exist

The next one you’ll need to learn is to check if a file exists no more. The command is easy to learn and remember.

Using Bash script, use the “!” symbol and follow it with the “-f” option, and the file you’re trying to check.

For example:

if [[ ! -f <file> ]] then echo "<file> does not exist on your file system. "

You can also use shorter forms, similar to the above example. You’ll be able to quickly test if a file exists or not on your terminal. So, you can type it on the test command as

[[! -f <file>]] && echo "This file does not exist!
"[! -f ] && echo "File does not exist!"

In the alternative, you can also use the ” | | ” operator. It’ll execute the right command in case the left command fails. You can type it as follows,

[[ - f <file> ]] | | echo "File does not exist!"

Easy! You’ll be able to check if a file exists on Bash or not!

Check if Directory Exists

How do you know if your directories still exist in Bash? Well, you’ll need to use and remember the symbolic link “-d” command option. Follow this command with the directory you’re trying to look for.

if [[-d "FILE DIRECTORY" ]]
then
echo " FILE DIRECTORY exist ." fi

For illustration, you want to test in Bash if /user/commrev still exists in the directory. It’ll look something like this:

#!/bin/bash if [[ -d /user/commrev ]] 
then
echo "/user/commrev exists on your file system."

When you type this command on Bash, the output should look something like this $ /etc. exists on your file system.

Using short terms

Similar to how you can use short terms to test if a file exists on Bash, you can also do the same for the directory. In the bash shell, you can use short terms to check out whether a directory still exists.

For you to test it, you’ll use the “-d” option. Make sure to put it in brackets, and follow it up with the command you want to run. It should look something like this:

[[ -d <directory> ]] && echo "FILE directory exists!" [ -d <directory> ] && echo "This FILE directory exists!"

You want to test and check if /user/commrev exists on your directory. You can use the short term, and you’d type it up as

" [ -d /user/commrev ] && echo "This directory exists!"

How to Create a Bash Script

Remembering all those commands and options may be challenging for some, especially if you’re the type of person who checks multiple times whether a particular file exists or directory still exists on the system. Well, it might be handy for you to learn how to create a script which can do the task for you, automatically!

We’ll teach you how to create a bash script that can check multiple filenames. You won’t have to keep typing the command anymore.

Using chmod, you can easily create a new bash script and execute it. It will look something like this:

$ mkdir -p ~/bin $ cd ~/bin && touch check_file && chmod u+x check_file && vi check_file

Generally, if you’re starting to check if your file exists, the script content will be like this:

#! /bin/bash #

Use the argument expansion to capture all files provided as arguments. 

for FILE in ${@,} do if [[ ! -f $FILE ]] 
then
echo "The file ${FILE} exist!" fi done

Before you exit Bash, make sure to save your script. Add the “bin” folder and add it to the PATH environment. It will look something like this:

$ export PATH="~/bin:$PATH" 

$ printrev PATH 

~/bin: /usr/local/sbin: /usr/local/bin: /usr/sbin: /usr/bin: /sbin: /bin

You can access it wherever on the system! You can even pull out your script and check if your file exists or not. Done! No need to keep opening your test command.

Final Words

Today, you learned how to check and test if your file exists or not on your Bash!

Using the bash test and bash shell test, for short terms, you’ll be able to find them! Find a file or two in no time! You also learned how to test whether a directory still exists!

And finally, we taught you how to make life a little better for you. We’ve taught you how to write a complete bash script to check if a file exists multiple times at the same time.

You won’t have to trouble yourself when you look for a file! You can wait for the program to run your file system and give you feedback on whether a file does not exist or not.

Whether you’re trying to check if file or directory exists, we hope we helped you!

Feel free to comment below what you learned. So go ahead and try this for yourself! It’s time to check whether your file does not exist anymore.