Table of Contents
Today, In this article we will show you how to Check File Exists in Bash Scripts on Linux. An extremely powerful tool for system administration and development is bash scripting. System administrators, network engineers, programmers, scientists, and everyone else who uses the Linux/Unix operating system use it. Bash is used for system management, data processing, web application deployment, automated backups, and writing customized scripts for particular pages, among other things.
An array of numerous commands that we often type at the command line are included in a plain text file called a Bash Shell Script. On the Linux filesystem, it is used to automate repeated processes. It may comprise one command, a series of commands, or the essential elements of imperative programming, such as loops, functions, conditional structures, etc.
A computer programme created in the Bash programming language is, in essence, what a Bash script is. This serves as merely a brief introduction. It’s not necessary to comprehend this in order to develop scripts, but if you start writing more complicated scripts, it can be helpful to know (and scripts that call and rely on other scripts once you start getting really fancy). We mentioned below are the steps to check If a File Exists in Linux Bash Scripts.
Steps to check File Exists in Linux Bash Scripts
Check If File Exists
Step 1: In order to check if a file exists in Bash, you have to use the “-f” option (for file) and specify the file that you want to check.
if [[ -f <file> ]]thenecho “<file> exists on your filesystem.”fi
Step 2: In a script, you would write the following if statement.
#!/bin/bash
if [[ -f “/etc/passwd” ]]thenecho “This file exists on your filesystem.”fi
Check File Existence using shorter forms
In some cases, you may be interested in checking if a file exists or not directly in your Bash shell.
Step 1: In order to check if a file exists in Bash using shorter forms, specify the “-f” option in brackets and append the command that you want to run if it succeeds.
[[ -f <file> ]] && echo “This file exists!”
[ -f <file> ] && echo “This file exists!”
Step 2: Using the example used before, if you want to check if the “/etc/passwd” file exists using shorter forms, you write the following command
[[ -f /etc/passwd ]] && echo “This file exists!”
Step 3: Shorter forms are closely related to exit statuses.
Step 4: When you run a command on Bash, it always exits with an error status : 0 for error and numbers greater than 0 for errors (1, 2.. 6 and so on)
Step 5: In this case, the “&&” syntax will check if the exit status of the command on the left is equal to zero : if this is the case, it will execute the command on the right, otherwise it won’t execute it.
Checking multiple files
In some cases, you may want to check if multiple files exist on your filesystem or not.
Step 1: In order to check if multiple files exist in Bash, use the “-f” flag and specify the files to be checked separated by the “&&” operator.
if [[ -f <file1> ]] && [[ -f <file2> ]]thenecho “They exist!”fi
Check If File Does Not Exist
On the other hand, you may want to check if a file does not exist on your filesystem.
Step 1: In order to check if a file does not exist using Bash, you have to use the “!” symbol followed by the “-f” option and the file that you want to check.
if [[ ! -f <file> ]]thenecho “<file> does not exist on your filesystem.”fi
Step 2: Similarly, you can use shorter forms if you want to quickly check if a file does not exist directly in your terminal.
[[ ! -f <file> ]] && echo “This file does not exist!”
[ ! -f <file> ] && echo “This file does not exist!”
Step 3: Note that it is also possible to check if a file does not exist using the “||” operator.
Step 4: The “||” operator will execute the command on the right if and only if the command on the left fails (i.e exits with a status greater than zero).
Step 5: To test if a file does not exist using the “||” operator, simply check if it exists using the “-f” flag and specify the command to run if it fails.
[[ -f <file> ]] || echo “This file does not exist!”
Check If Directory Exists
Step 1: In order to check if a directory exists in Bash, you have to use the “-d” option and specify the directory name to be checked.
if [[ -d “$DIRECTORY” ]]thenecho “$DIRECTORY exists on your filesystem.”fi
Step 2: In order to check its existence, you would write the following Bash script
#!/bin/bash
if [[ -d /etc ]]thenecho “/etc exists on your filesystem.”fi
Check Directory Existence using shorter forms
In some cases, you may be interested in checking if a directory exists or not directly in your Bash shell.
Step 1: In order to check if a directory exists in Bash using shorter forms, specify the “-d” option in brackets and append the command that you want to run if it succeeds.
[[ -d <directory> ]] && echo “This directory exists!”
[ -d <directory> ] && echo “This directory exists!”
Step 2: Let’s say that you want to check if the “/etc” directory exists for example.
Step 3: Using the shorter syntax, you would write the following command.
[ -d /etc ] && echo “This directory exists!”
Creating a complete Bash script
If you find yourself checking multiple times per day whether a file (or multiple) exists or not on your filesystem, it might be handy to have a script that can automate this task.
Step 1: In this section, you are going to create a Bash script that can take multiple filenames and return if they exist or not.
Step 2: Create a new Bash script and make it executable using chmod.
$ mkdir -p ~/bin
$ cd ~/bin && touch check_file && chmod u+x check_file && vi check_file
Step 3: Here is the content of the script to be used to dynamically check if files exist.
#!/bin/bash
# Using argument expansion to capture all files provided as arguments.
for FILE in ${@}doif [[ ! -f $FILE ]]thenecho “The file ${FILE} does not exist!”fidone
Step 4: Save your script and add the “bin” folder you just created to your PATH environment variable.
$ export PATH=”~/bin:$PATH”
$ printenv PATH
~/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
Step 5: Now that your script is accessible wherever you are on the system, you can call your script and start checking if files exist or not.
$ check_file /etc/passwd /etc/pass /etc/file
The file /etc/pass does not exist!The file /etc/file does not exist!
Final Words
We hope you like our article on how to check If a File Exists in Linux Bash Scripts. In Linux, there are numerous techniques to determine whether a file is accessible. One of the most important methods for verifying a file’s existence in bash scripting is the “test” command. The longer your admins use Linux, the more likely it is that they will start using shell scripts. In fact, you want to encourage them to begin with shell scripting to handle simpler tasks before switching to conventional development once they hit the inescapable brick wall.