• Home
  • Linux
  • Ubuntu
  • Debian
  • CentOS
  • Linux Commands
  • About Us
  • Donate
TecNStuff
Menu
  • Home
  • Linux
  • Ubuntu
  • Debian
  • CentOS
  • Linux Commands
  • About Us
  • Donate

How to Check if a File or Directory Exists in Bash

Written by Admin, Updated On June 18, 2020
bash, terminal
How to Check if a File or Directory Exists in Bash

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.

If our content helps you, please consider buying us a coffee

Thank you for your support.

Share On
Share on Facebook
Share on Twitter
Share on Reddit
Share on Tumblr
 Previous Article How to Delete (Remove) Symbolic Links in Linux
Next Article   Ls Command in Linux (List Files and Directories)

Related Posts

  • How to Install SSH Keys on Ubuntu 22.04

    How to Set up SSH Keys on Ubuntu 22.04

    January 7, 2023
  • How to Install Fail2ban on Ubuntu 22.04

    How to Install and Configure Fail2ban on Ubuntu 22.04

    December 5, 2022
  • How to Enable SSH on Ubuntu 22.04

    How to Enable SSH on Ubuntu 22.04

    December 1, 2022

Leave a Reply Cancel reply

DigitalOcean Referral Badge

Popular Posts

  • How to Install Microsoft Edge Browser on Ubuntu 22.04
    How to Install Microsoft Edge Browser on Ubuntu 22.04 March 14, 2023
  • How to Install Ruby on Ubuntu 22.04 LTS
    How to Install Ruby on Ubuntu 22.04 LTS February 27, 2023
  • How to Install LEMP Stack on Ubuntu 22.04
    How to Install LEMP Stack on Ubuntu 22.04 March 18, 2023
  • How to Install Set Up Apache Virtual Hosts on Ubuntu 22.04
    How to Set Up Apache Virtual Hosts on Ubuntu 22.04 March 2, 2023
  • How to Install MariaDB on Debian 11 Bullseye
    How to Install MariaDB on Debian 11 Bullseye March 8, 2023
© 2020 TecNStuff All rights reserved. This website is using and storing cookies on your browser. By using this website you agree our Privacy Policy.  Follow us -  Twitter | Facebook