While working with Bash and shell scripts, you might need to check whether a directory or a file exists or not on your filesystem.
In order to check whether a file or a directory exists with Bash, you can use test command.
Following is the basic syntax for the test command:
test EXPRESSION
[ EXPRESSION ]
[[ EXPRESSION ]]
Check If File Exists
To check if file exists, you have to use -f
FILE operator. It will return true
only if the FILE is a regular file.
if [[ -f <file> ]]
then
echo "<file> exists on your filesystem."
fi
For example, you want to check if the file /etc/passwd
exists on your file system or not, you can check by any of the following stuff:
if test -f "/etc/passwd"; then
echo "File is exists on your system."
fi
if [ -f "/etc/passwd" ]; then
echo "File is exists on your system."
fi
if [[ -f "/etc/passwd" ]]; then
echo "File is exists on your system."
fi
If you want to perform a different action based on whether the file exists or not simply use the if/then construct:
if [ -f "/etc/passwd" ]; then
echo "File is exists on your system."
else
echo "File is NOT exists on your system."
fi
Check File Existence using shorter forms
You can also check file existence without using the if statement. If exist status true then after the && operator will be executed.
test -f /etc/passwd && echo "This file exists."
[ -f /etc/passwd ] && echo "This file exists."
[[ -f /etc/passwd ]] && echo "This file exists."
Check if Directory Exist
To check whether directory is exist or not, you have to use -d
operator.
Following is the basic syntax for check directory exist:
if [[ -d "$DIRECTORY" ]]
then
echo "$DIRECTORY exists on your filesystem."
fi
For instance, to check /etc
directory exist on your system, you would run:
if [[ -d /etc ]]
then
echo "/etc exists on your filesystem."
fi
Check if Multiple Files Exist
Sometimes, you have required to check multiple files exists on your system or not. You can do it using &&
operator.
if [ -f /etc/resolv.conf -a -f /etc/hosts ]; then
echo "Both files exist."
fi
if [[ -f /etc/resolv.conf && -f /etc/hosts ]]; then
echo "Both files exist."
fi
Equivalent variants without using the IF statement:
[ -f /etc/resolv.conf -a -f /etc/hosts ] && echo "Both files exist."
[[ -f /etc/resolv.conf && -f /etc/hosts ]] && echo "Both files exist."
Check If File Does Not Exist
You also can check reverse condition for does not exists using the !
(exclamation mark) logical not operator:
if [ ! -f "/etc/passwd" ]; then
echo "This file does not exist."
fi
In short form you can do as below:
[ ! -f /etc/passwd ] && echo "This file does not exist."
File test operators
Following are the FILE operators that allow you to test for particular types of files:
-b
FILE
– True if the FILE exists and is a special block file.-c
FILE
– True if the FILE exists and is a special character file.-d
FILE
– True if the FILE exists and is a directory.-e
FILE
– True if the FILE exists and is a file, regardless of type (node, directory, socket, etc.).-f
FILE
– True if the FILE exists and is a regular file (not a directory or device).-G
FILE
– True if the FILE exists and has the same group as the user running the command.-h
FILE
– True if the FILE exists and is a symbolic link.-g
FILE
– True if the FILE exists and has set-group-id (sgid) flag set.-k
FILE
– True if the FILE exists and has a sticky bit flag set.-L
FILE
– True if the FILE exists and is a symbolic link.-O
FILE
– True if the FILE exists and is owned by the user running the command.-p
FILE
– True if the FILE exists and is a pipe.-r
FILE
– True if the FILE exists and is readable.-S
FILE
– True if the FILE exists and is a socket.-s
FILE
– True if the FILE exists and has nonzero size.-u
FILE
– True if the FILE exists, and set-user-id (suid) flag is set.-w
FILE
– True if the FILE exists and is writable.-x
FILE
– True if the FILE exists and is executable.
Conclusion
You learned how to check if a file or directory exists in Bash.
If you have any questions or feedback, feel free to leave a comment.