Logo
  • Ubuntu
  • CentOS
  • Debian
  • Fedora
  • RedHat

How to Check if a File or Directory Exists in Bash - DesignLinux

Jun 18 2020
designlinux 0 Comments
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.

Related

Tags: bash, terminal

How to Cache Content in NGINX

Prev Post

How to Install Anaconda on Ubuntu 20.04

Next Post
Archives
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • July 2022
  • June 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021
  • May 2021
  • April 2021
  • March 2021
  • February 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • July 2020
  • June 2020
  • May 2020
Categories
  • AlmaLinux
  • Android
  • Ansible
  • Apache
  • Arch Linux
  • AWS
  • Backups
  • Bash Shell
  • Bodhi Linux
  • CentOS
  • CentOS Stream
  • Chef
  • Cloud Software
  • CMS
  • Commandline Tools
  • Control Panels
  • CouchDB
  • Data Recovery Tools
  • Databases
  • Debian
  • Deepin Linux
  • Desktops
  • Development Tools
  • Docker
  • Download Managers
  • Drupal
  • Editors
  • Elementary OS
  • Encryption Tools
  • Fedora
  • Firewalls
  • FreeBSD
  • FTP
  • GIMP
  • Git
  • Hadoop
  • HAProxy
  • Java
  • Jenkins
  • Joomla
  • Kali Linux
  • KDE
  • Kubernetes
  • KVM
  • Laravel
  • Let's Encrypt
  • LFCA
  • Linux Certifications
  • Linux Commands
  • Linux Desktop
  • Linux Distros
  • Linux IDE
  • Linux Mint
  • Linux Talks
  • Lubuntu
  • LXC
  • Mail Server
  • Manjaro
  • MariaDB
  • MongoDB
  • Monitoring Tools
  • MySQL
  • Network
  • Networking Commands
  • NFS
  • Nginx
  • Nodejs
  • NTP
  • Open Source
  • OpenSUSE
  • Oracle Linux
  • Package Managers
  • Pentoo
  • PHP
  • Podman
  • Postfix Mail Server
  • PostgreSQL
  • Python
  • Questions
  • RedHat
  • Redis Server
  • Rocky Linux
  • Security
  • Shell Scripting
  • SQLite
  • SSH
  • Storage
  • Suse
  • Terminals
  • Text Editors
  • Top Tools
  • Torrent Clients
  • Tutorial
  • Ubuntu
  • Udemy Courses
  • Uncategorized
  • VirtualBox
  • Virtualization
  • VMware
  • VPN
  • VSCode Editor
  • Web Browsers
  • Web Design
  • Web Hosting
  • Web Servers
  • Webmin
  • Windows
  • Windows Subsystem
  • WordPress
  • Zabbix
  • Zentyal
  • Zorin OS
Visits
  • 1
  • 502
  • 604,464

DesignLinux.com © All rights reserved

Go to mobile version