29 C
Jaipur
Tuesday, April 13, 2021

How to Check if a File Exists in Bash? – Linux Hint

Must read

In Linux, everything is saved in a file. To check if a file exists and if it’s a standard one, use the test command with the -f option. Similarly, using the test command with the -d operator helps you to see if a file or directory exists.

In this guide, we will learn how to use a Bash script to check if a file or directory exists.

Create File

We need to create a file using the below-given command:

To create a file we use the touch command and “.sh” is the bash file extension.

Open the file and write the script, then save the text by pressing “save”.

One way is to find a file by asking for a filename from the user on a terminal.

Use “-f” to check the file.

Write the below script:

#!/bin/bash
echo “Enter filename”
read newfile1
if [ -f $newfile1 ]
then
echo “File exists”
else
   echo “File does not exist”
fi

Go back to the terminal and run the file to print output:

Permission denied message will be displayed in the terminal.

D:Aqsa18 marchhow to check in bash if file existshow to check in bash if file existsimagesimage6 final.png

Make it executable using the following command:

Enter the file name, and the output will be printed:

D:Aqsa18 marchhow to check in bash if file existshow to check in bash if file existsimagesimage8 final.png

One way is to find a file by telling the file name while writing code.

We have three ways to write a statement:

Test EXPRESSION

[ EXPRESSION ]

[[ EXPRESSION ]]

If [ -f “$filename” ]

If test –f “$fiename.”;

If [[ -Expression ]] ;

Output:

D:Aqsa18 marchhow to check in bash if file existshow to check in bash if file existsimagesimage12 final.png

Without using if

The test command can also be used without an if statement. Only if the test command’s exit status is valid, the command after the && operator will be executed.

Output:

D:Aqsa18 marchhow to check in bash if file existshow to check in bash if file existsimagesimage21 final.png

Check Multiple Files

Instead of complicated nested if/else constructs, use -a (or && with [[) to check if multiple files exist:

#!/bin/bash
if [ -f new_file.txt -a -f newfile.txt ]; then
    echo “Both files exist.”
fi

Another way is:

#!/bin/bash
if [[ -f new_file.txt && -f newfile.txt ]]; then
    echo “Both files exist.”
fi

Output:

D:Aqsa18 marchhow to check in bash if file existshow to check in bash if file existsimagesimage2 final.png

Without Using IF Statement

Use the following syntax to check file without using IF:

[ -f file1_name –a –f file2_name ] && echo “Print statement”

[ -f file1_name <strong> &&</strong> -f file2_name ] && echo “Print statement.”

Conclusion:

This article has shown how to use Bash to search and find if whether a file or directory exists.

If else is used to perform different actions based on the value of the expression used to evaluate whether or not a file or directory exists. Create a nested if-else statement and check the file or directory without using the IF statement.

Source link

- Advertisement -

More articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest article