Bash Scripting

How to Check if a File Exists in Bash – Simply Explained

Why Do We Check Files 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.

Ways to Check in Bash

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.

File Test Flags

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

Examples Explained

Check if any file or folder exists

if [ -e "$FILE" ]; then
  echo "It exists"
fi

This checks if something is there, whether it is a file or a folder.

Check if it is a regular file

if [ -f "$FILE" ]; then
  echo "It is a file"
fi

This makes sure the path is a normal file and not a folder.

Check if it is a directory

if [ -d "$DIR" ]; then
  echo "It is a directory"
fi

This checks only for folders.

Check file permissions

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.

Check if a file does not exist

if [ ! -f "$FILE" ]; then
  echo "The file is missing"
fi

The exclamation mark means “not”. This checks if the file is absent.

Check more than one file

if [ -f "$FILE1" ] && [ -f "$FILE2" ]; then
  echo "Both files exist"
fi

This makes sure that two files are present before running further commands.

Conclusion

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.

0xSnow

0xSnow is a cybersecurity researcher with a focus on both offensive and defensive security. Working with ethical hacking, threat detection, Linux tools, and adversary simulation, 0xSnow explores vulnerabilities, attack chains, and mitigation strategies. Passionate about OSINT, malware analysis, and red/blue team tactics, 0xSnow shares detailed research, technical walkthroughs, and security tool insights to support the infosec community.

Recent Posts

Bash Scripting Best Practices Every Beginner Should Know

Introduction Bash scripting is a powerful way to automate Linux tasks, but writing a script…

1 day ago

How To Create A Self-Signed SSL Certificate Using Bash And OpenSSL

Introduction A self-signed SSL certificate is a certificate that is created and signed by the…

1 day ago

How To Debug Bash Scripts Using bash -x And set Commands

Introduction Debugging is an important part of Bash scripting. When a script does not work…

1 day ago

How To Use Cron Jobs With Bash Scripts For Automation

Introduction Cron jobs are used in Linux to run commands or Bash scripts automatically at…

1 day ago

How To Use Pipes In Bash Scripts For Command Chaining

Introduction Pipes are an important feature in Linux and Bash scripting. A pipe allows you…

1 day ago

How To Use grep, awk, And sed In Bash Scripts

Introduction The grep, awk, and sed commands are powerful text-processing tools in Linux. They are…

1 day ago