How to check if a file exists in bash

Last updated on February 9, 2021 by Dan Nanni

When you are working on a shell script in bash, there are cases where you want to check if a particular file exists (or does not exist) in a directory, and then perform an action based on the condition. In bash, there are several ways to check whether or not a file exists in bash. In the following I demonstrate several bash shell script examples for this use case.

Check if File Exists in bash

The easiest way to check if a file exists is to use the test command. With -f <file-name> option, the test command returns true if the specified file exists.

FILE=/etc/pam.conf
if test -f $FILE; then
    echo "$FILE exists"
fi

Alternatively, you can use the bash's built-in flag -f, which can check for existence of a file when used inside bash's conditional expressions.

FILE=/etc/pam.conf
if [ -f $FILE ]; then
    echo "$FILE does not exists"
fi

It is also possible to use the following shorthand one-liner, where the conditional check and a corresponding action are written in a single line.

test -f $FILE && echo "$FILE exists."
[ -f $FILE ] && echo "$FILE exists."

Check if File Does Not Exist in bash

If you want to check if a file does not exist in bash, you can simply add ! in the above conditional expressions.

FILE=/etc/xmodulo.com

# Option 1
if ! test -f $FILE; then
    echo "$FILE does not exist"
fi

# Option 2
if [ ! -f $FILE ]; then
    echo "$FILE does not exist"
fi

# Option 3
test ! -f $FILE && echo "$FILE does not exist."

# Option 4
[ ! -f $FILE ] && echo "$FILE does not exist."

Check if Any File with Extension Exists in bash

In all previous bash script examples, the name of the file to check is pre-determined and known. What if you want to check if any file with a pattern (e.g., with a particular extension) exists? In that case, the name of file(s) to check for is not known. Note that the test command and the bash's -f flag do not work with wildcard patterns.

The easiest way to check if any file with a wildcard expression exists is to use ls command. You simply check if ls (with a pattern) returns non-zero.

PATTERN="/etc/systemd/*.conf"

if ls $PATTERN 1> /dev/null 2>&1; then
    echo "Files exist"
fi

Another method is to use the compgen command. You can use the -G option to specify a glob pattern. Simlar to ls, compgen returns a list of files that are matched.

PATTERN="/etc/systemd/*.conf"

if compgen -G $PATTERN > /dev/null; then
    echo "Files exist"
fi

The following shorthand one-liners are also possible.

ls $PATTERN 1> /dev/null 2>&1 && echo "Files exist"
compgen -G $PATTERN > /dev/null && echo "Files exist"

If you find this tutorial helpful, I recommend you check out the series of bash shell scripting tutorials provided by Xmodulo.

Support Xmodulo

This website is made possible by minimal ads and your gracious donation via PayPal or credit card

Please note that this article is published by Xmodulo.com under a Creative Commons Attribution-ShareAlike 3.0 Unported License. If you would like to use the whole or any part of this article, you need to cite this web page at Xmodulo.com as the original source.

Xmodulo © 2021 ‒ AboutWrite for UsFeed ‒ Powered by DigitalOcean