Tech News Today
  • Hardware
    • Motherboards
    • CPUs
    • Graphic Cards
    • RAM
    • SSDs
    • Computer Cases
    • Monitors
    • Peripherals
    • Power Supply Unit
    • PC Builds
    • Computer Tips
  • Software
  • Operating System
    • Windows
    • Mac
    • Linux
  • Gaming
  • Mobile
  • Console
  • More
    • Internet
    • Networking
    • Security
    • Buyer’s Guide
    • Gadgets
    • Laptops
    • Reviews
    • How To
    • News
Facebook Twitter Instagram
Tech News Today
  • Hardware
    • Motherboards
    • CPUs
    • Graphic Cards
    • RAM
    • SSDs
    • Computer Cases
    • Monitors
    • Peripherals
    • Power Supply Unit
    • PC Builds
    • Computer Tips
  • Software
  • Operating System
    • Windows
    • Mac
    • Linux
  • Gaming
  • Mobile
  • Console
  • More
    • Internet
    • Networking
    • Security
    • Buyer’s Guide
    • Gadgets
    • Laptops
    • Reviews
    • How To
    • News
Tech News Today
Home»Linux»How To Check If A File Exists In Bash

How To Check If A File Exists In Bash

Anup ThapaBy Anup ThapaAugust 13, 2022
bash check if file exists

When scripting in Bash, users often come across situations where they need to check whether a file or directory exists or not.

The if and test commands are used in conjunction to check whether a file exists or not in Bash. You’ll typically use expressions like -e or -f to check a file’s existence, whereas -d is used in the case of directories.

In addition to further info on these, we’ve also detailed how to check if a file exists using wildcards and how to exclusively check if a file doesn’t exist in this article.

Table of Contents

  • Check If File or Directory Exists
  • Check If A File Doesn’t Exist
  • Check if Multiple Files Exist
  • Check If One of Two Files Exists

Check If File or Directory Exists

First, let’s talk about the primary expressions, i.e., primaries. You can use the man test command to check test’s man page for the full list of primaries, but here are the most commonly used ones:

ExpressionDescription
-a FILEFILE exists. 
-d FILEFILE exists and is a directory.
-e FILEFILE exists.
-f FILEFILE exists and is a regular file.
-r FILEFILE exists and is readable.
-x FILEFILE exists and is executable or searchable.

Let’s start with something basic. In the following example, we store the file’s path in $FILE. From the table above, -e means that if FILE exists is true, then the $FILE exists output will be echoed. If FILE doesn’t exist, then the $FILE doesn’t exist will instead be the output that’s echoed.

FILE=/file/path
if [[ -e "$FILE" ]]; then
echo "$FILE exists."
else
echo "$FILE doesn’t exist."
fi

if then file

You could do the same thing more efficiently as such:
test -e /file/path && echo “File exists.” || echo “File doesn’t exist.”

test e file

You could also use wildcard characters for this:
[ -e /file/path ] && echo “File exists.” || echo “File doesn’t exist.”

wildcard e file

You would use the -d expression to check if a directory exists or not:
[ -d /directory/path ] && echo “Directory exists.” || echo “Directory doesn’t exist.”

check if directory

Check If A File Doesn’t Exist

If you’re only trying to check if the file doesn’t exist, you can use the NOT operator (!) as such:
if [ ! -e /file/path ]; then
echo "File doesn’t exist."
fi

But once again, you can do the same thing more efficiently as such:
[ ! -e /file/path ] && echo "File doesn’t exist."

check if file doesnt

Check if Multiple Files Exist

If you’re trying to check whether multiple files exist or not at once, you can use the -a expression or the && operator.

Using -a

The first method is as such:
if [ -e /file/one -a -e /file/two ]; then
echo "Both files exist."
fi

Whereas a more efficient variant would look like this:
[ -e /file/one -a -e /file/two ] && echo "Both files exist."

check if multiple files

Using &&

The first method is as such:
if [[ -e /file/one && -e /file/two ]]; then
echo "Both files exist."
fi

An equivalent variant would look like this:
[[ -e /file/one && -e /file/two ]] && echo "Both files exist."

check if both files

Check If One of Two Files Exists

If you’re trying to check if one of two files exists, you could use the following command:

if [ -f /file/path ] || [ ! -f /file/path ] ; then echo Exists; fi

one of two files

If you’re trying to check if one of two files doesn’t exist, you could instead apply ! to one of the file paths as such:
if [ -f /file/path ] || [ ! -f /file/path ] ; then echo Missing; fi

one of two files is missing
how-to
Anup Thapa

Anup Thapa is a tech writer at TechNewsToday. He mostly writes informative articles, tutorials, and troubleshooting guides related to Windows systems, networking, and computer hardware. Anup has been writing professionally for almost 5 years, and tinkering with PCs for much longer. His love for all things tech started when he got his first PC over 15 years ago. It was a Pentium IV system running Windows XP on a single 256 MB stick. He spent his formative years glued to this PC, troubleshooting any hardware or software problems he encountered by himself. Professionally, Anup has had brief forays into a variety of fields like coding, hardware installation, writing, etc. In doing so, he's worked with people of different backgrounds and skill levels, from average joes to industry leaders and experts. This has given him not just a versatile skillset, but also a unique perspective for writing that enables him to concisely communicate complex information and solve his reader's problems efficiently.

Related Posts

how to change boot order on linux

How to Change Boot Order on Linux

December 16, 2022
make-command-not-found

How to Fix “Make: Command Not Found” Error

September 20, 2022
linux terminal for windows

How to Install Linux Terminal on Windows

September 19, 2022
How-to-force-Quit-an-app-on-Linux

How to Force Quit an App on Linux

August 30, 2022
best linux for virtualbox

8 Best Linux Distros to Try in VirtualBox

August 30, 2022
ifconfig command not found

How To Fix “ifconfig Command Not Found” Error?

December 18, 2022
Add A Comment

Leave A Reply Cancel Reply

Latest Posts
how long does a cmos battery last

How Long Does a CMOS Battery Last

January 25, 2023
thunderbolt vs usb c

Thunderbolt Vs USB C: What’s the Difference

January 25, 2023
how to find hidden folder in laptop

How to Find Hidden Folder in Laptop

January 24, 2023
You may also like
4k-tv-as-monitor

How to Use a 4K TV as a Monitor? (Step-by-Step Guide)

January 26, 2023
how-to-install-cinema-on-firestick

How to Install Cinema on Firestick (Step-By-Step Guide)

January 26, 2023
how to adblock on chrome

How to Block Ads on Google Chrome

January 26, 2023
Recommended
Cookie Clicker Garden Guide

Cookie Clicker Garden Guide to Unlocking Every Seed

September 26, 2021
monitor no signal

Computer Turns On But Monitor Says No Signal (9 Ways To Fix)

November 10, 2022
Facebook Twitter Pinterest
  • Home
  • About Us
  • Our Team
  • Editorial Guidelines
  • Privacy Policy
  • Affiliate Disclosure
© 2023 TechNewsToday.

Type above and press Enter to search. Press Esc to cancel.