How to Check bash if file Directory exists

If you are working in a Bash script that interacts with files and directories, you may find a situation where you need to make sure that the file or directory exists. This helps to avoid possible errors when performing certain actions in a file that does not exist.

In this tutorial, I will show you a couple of ways to verify if the file or directory exists in the bash script or not. Let’s start with the file first.

Check if the file exists in the bash script

The idea here is to use the -f operator that returns true only when it is a regular file (not a directory).

Suppose you want to verify if the file/home/user/my_file exists or not. This is how you can verify with square brackets.

#!/bin/bash
 
if [ -f /home/user/my_file ]
then
    echo "My file exists"
f

But you will not always get the file name in advance. You can have it in a variable and, if that is the case, you can use it this way.

#!/bin/bash

FILE=/home/user/my_file
 
if [ -f "$FILE" ]
then
    echo "My file exists"
else 
    echo "My file doesn't exist"
fi

What is the condition you use in the if command. It is up to you how you want to use the if statement. For example, you can write it with two square brackets, keep “then” on the same line as if or put the whole statement together with a semicolon like this:

[ -f /home/user/my_file ] && echo "My file exists" || echo "My file doesn't exist"

Check file exists in bash with test

You can use test in bash to see if the file exists or not. It’s almost the same, don’t use the square brackets in the if statement:

#!/bin/bash

FILE=/home/user/my_file
 
if test -f "$FILE" 
then
    echo "My file exists"
else 
    echo "My file doesn't exist"
fi

You can also use the above code on a single line like this:

test -f /home/user/my_file && echo "My file exists" || echo "My file doesn't exist"

Check if the file does not exist in the bash script

What happens if it is the other way around, and you want to check if the file does not exist in bash? You can use virtually the same code as the previous one using the negation operator:

#!/bin/bash

FILE=/home/user/my_file
 
if [ ! -f "$FILE" ]
then
    echo "My file doesn't exist"
fi

Now that you know how to deal with files let’s move on to directories.

Verify if the directory exists in the bash script

The code to verify the directory is the same as you saw in the previous section. The only difference is that you will use -d instead of -f. -d returns true only for directories.

#!/bin/bash
 
if [ -d /home/user/my_dir ]
then
    echo "My directory exists"
fi

You can also use test here:

#!/bin/bash

DIR=/home/user/my_dir
 
if test -d "$DIR" 
then
    echo "My directory exists"
else 
    echo "My directory doesn't exist"
fi

Check if the directory does not exist in bash

You can use denial again to verify if the directory does not exist:

#!/bin/bash

DIR=/home/user/my_dir
 
if [ ! -d "$DIR" ]
then
    echo "My directory doesn't exist"
fi

That all you need to do to confirm if there is a file directory in the bash shell or not.

If you have any questions or suggestions, feel free to leave a comment below.

>>

Recent Articles

Cybercrime forensic Paid Course Free

What is Cybercrime forensic  Cybercrime Forensic computing is the application of investigation and analysis techniques to collect and preserve evidence of a particular computing device...

Android Keylogger Hidden Keyboard

However, if Android Keylogger falls into the hands of hackers, it can be dangerous. Critical information, such as usernames and passwords, is often entered...

Kali Linux 2019.4 Released New Features With New DE, Undercover Mode And More

What’s new in Latest Kali Linux 2019.4New Desktop Environment New GTK3 Theme Kali Undercover Kali Documentation Updates BTRFS during setup PowerShell NetHunter KexNew...

How To Install Latest Kali Linux 2019.4

What is Kali Linux? Kali Linux is a Debian-based Linux operating system for hacking, Advanced Penetration Testing, Security Auditing Security research, Computer Forensics, and Reverse...

Linux Terminal Shortcuts Keys Every Power Linux User

Shortcuts help you be more productive and efficient with any tool you use. Just think about it. If someone keeps the mouse down to...

1 Comment

Leave A Reply

Please enter your comment!
Please enter your name here

Stay on op - Ge the daily news in your inbox