Check if File Exists in Bash
When writing a Bash script, you often work with files or folders. Before trying to read, copy, or edit them, you should first check if file exists in Bash. Doing this ensures the file or directory is really there and prevents your script from breaking or showing errors.
There are several ways to check if file exists in Bash. The most common options are:
test EXPRESSION[ EXPRESSION ][[ EXPRESSION ]]All of these can be used for file checks, but [[ ]] has more features and works only in Bash, while [ ] and test are more universal and portable.
Flag What It Checks
-e Any kind of file or directory
-f A regular file (not a folder or special file)
-d A directory
-r File is readable
-w File is writable
-x File is executable
-L A symbolic link (shortcut to another file)
-s A file that is not empty
if [ -e "$FILE" ]; then
echo "It exists"
fi This checks if something is there, whether it is a file or a folder.
if [ -f "$FILE" ]; then
echo "It is a file"
fi This makes sure the path is a normal file and not a folder.
if [ -d "$DIR" ]; then
echo "It is a directory"
fi This checks only for folders.
if [ -r "$FILE" ]; then
echo "You can read this file"
fi
if [ -w "$FILE" ]; then
echo "You can write to this file"
fi
if [ -x "$FILE" ]; then
echo "You can run this file"
fi These tests confirm whether the file can be read, edited, or run as a program.
if [ ! -f "$FILE" ]; then
echo "The file is missing"
fi The exclamation mark means “not”. This checks if the file is absent.
if [ -f "$FILE1" ] && [ -f "$FILE2" ]; then
echo "Both files exist"
fi
This makes sure that two files are present before running further commands.
In Bash, checking if a file or directory exists is simple once you know the flags. Use -e for anything, -f for regular files, and -d for folders. Add checks for read, write, or execute permissions when needed. Always put the file name inside quotes to avoid problems with spaces. If you want to test for something missing, use !. With these methods, your Bash scripts will be much safer and easier to manage.
VirtualBox is a free, open-source, cross-platform virtualization application maintained by Oracle. It lets you run multiple…
PostgreSQL (also called Postgres) is a free, open-source, object-relational database management system with a strong reputation…
VMware Workstation Player is a mature, stable virtualization platform that lets you run multiple isolated operating…
A properly configured firewall is one of the most important layers of security for any internet-facing server. UFW (Uncomplicated Firewall) is a user-friendly front-end for managing iptables rules that ships pre-installed on Ubuntu. Its defaults are sensible: block all incoming connections, allow all outgoing connections. No outside traffic reaches your server unless you explicitly open a port. This guide covers how to configure a UFW firewall on Ubuntu 18.04, from setting default policies and application profiles to writing allow and deny rules for specific ports, IPs, and subnets. <strong>Prerequisite:</strong> You need sudo access. Configure UFW Firewall on Ubuntu: Defaults, SSH, and Application Profiles If UFW is not installed, add it with: bashsudo…
UFW (Uncomplicated Firewall) is a user-friendly front-end for managing iptables rules on Ubuntu. It ships pre-installed…
Apache Cassandra is a free, open-source NoSQL database designed for high availability and linear scalability with…