How to Check if a File or Directory Exists in Bash?

Learn via video courses
Topics Covered

While using the Linux command-line interface, it is often necessary to check the existence of a file or directory before performing certain operations. Whether you are working on system administration tasks or writing shell scripts, being able to verify the presence of a file or directory is crucial for error handling and conditional execution. In this article, we will explore different methods to check if a file or directory exists in Bash, all within the command line itself.

Build an AI-First Career, Master the Complete Skillset

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program
NSDC Certified

AI Forward Deployed Engineer Program

Full-stack engineering, production AI and client-facing consulting

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program

How to Check if a File Or Directory Exists in Bash ?

Bash provides various commands and operators that allow us to determine the existence of a file or directory. By using these tools effectively, we can ensure that our commands and scripts execute correctly and handle different scenarios gracefully.

Checking If a File Exists

To check if a file exists in Linux, we can use the test command or its equivalent [] brackets along with the -f flag. The -f flag checks if the given file path exists and if it is a regular file. Here's an example:

In the above command, replace /path/to/file.txt with the actual file path you want to check. If the file exists, the command after the && operator i.e. the echo command echoing "File exists" is executed. If the file does not exist, the command after the || operator i.e. the echo command echoing "File does not exist." is executed.

An alternative way to check if a file exists in Linux is by using the if-else conditional statements. Here's an example:

In the above command replace path/to/file.txt with the actual path of the file. Here, we are using [] instead of test, and similar to the previous example the -f option is used to check if a file exist in Linux. The if-else conditional statement will execute any one of the echo statements based on the condition, i.e. the existence of the file.

Sharpen Your Fundamentals with Free Learning

Checking If a Directory Exists

To check if a directory exists in Linux, we can use the test command along with the -d flag. The -d flag is used to check if a given path exists and it is a directory. Here's an example:

In the above command, replace /path/to/directory with the actual path of the directory. If the directory exists, the command after the && operator will be executed, and "Directory exists" will be displayed. If the file does not exist, the command after the || operator will be executed, and "Directory does not exist." will be displayed.

An alternate method to check using if-else conditional statements and [] as an equivalent of the test command is as follows:

In the above command, replace path/to/directory with the actual directory path you want to check. The -d flag is used to check if the directory exists and the if-else conditional statement is used to display the respective output.

How Scaler Transformed Careers in Different Fields

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more

Checking If File Does Not Exist in Linux

In the examples provided above, we have learned how to check if a file exists in Linux using the test command with the else statement or with the || operator. To simply check if a file exists or not we can also use the ! operator to negate the condition. Here's an example:

In the above command, replace the path/to/file.txt with the actual path of the file that is to be checked. Using the ! operator the above command will display "There is no such file!" if the specified file path is not found.

An alternate method to check if a file does not exist using the if conditional statement is as follows:

Here, the command output will display "There is no such file!" if the file path specified in place of path/to/file.txt placeholder, does not exist.

Checking If Multiple Files Exist

To check the existence of multiple files altogether, we can use the -a flag or the && operator with the test command or []. Here's an example of checking the existence of multiple files using the test command along with the -a flag:

The above command will display "Both files exist" only if both the file paths mentioned in place of file1.txt and file2.txt exist.

An alternate way to check if multiple files exist is by using the && operator. Here's an example:

The above command will display "Both files exist" when the file path specified in place of file1.txt and file2.txt both exist.

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

File Test Operators

There are various file test operators in Linux that can be used with the test command or its equivalent [] to perform different checks on files. Here are some commonly used file test operators:

OperatorsDescription
-eThe -e flag checks if the file exists, regardless of its type.
-fThe -f flag checks if the file exists and is a regular file.
-dThe -d flag checks if the file exists and is a directory.
-sThe -s flag checks if the file exists and has non-zero size.
-rThe -r flag checks if the file exists and is readable.
-wThe -w flag checks if the file exists and is writable.
-xThe -x flag checks if the file exists and is executable.
-LThe -L flag checks if the file exists and is a symbolic link.
-cThe -c flag checks if the file exists and is a character special file.
-bThe -b flag checks if the file exists and is a block special file.
-uThe -u flag checks if the file exists and has the setuid permission.
-gThe -g flag checks if the file exists and has the setgid permission.
-kThe -k flag checks if the file exists and has the sticky bit permission.

Conclusion

  • For a system administrator or any regular Linux user, being able to check if a file exists in Linux, is a necessary step in understanding our system and managing its resources.
  • To check if a file exists in Linux, the test command or its equivalent [] can be used along with the -f option.
  • Similarly, to check if a directory exists in Linux the test command or its equivalent [] can be used along with the -d option.
  • To check if multiple files or directories exists in Linux we can use the -a flag or && operator with the test command.