| by Arround The Web | No comments

Bash –F Test Condition

The test command is integral to conditional statements in Bash scripting that compare one element to another. Sometimes, we need to check whether the file is regular or not. By running a Bash script, you can do this easily using the -f flag and the test command. However, many new users don’t know about this amazing utility of Bash scripting. In this tutorial, we will use the Bash -f test condition to determine whether the file is regular.

Bash -F Test Condition

A regular file is a type of file which is stored in the file system. As a Linux user, you mostly use the regular files like text files, image files, etc. Here, we will test whether the file is regular through the Bash -f test condition. Let’s run the Bash script and the syntax to test the regularity of the file:

test -f <filename>

Let us take some random files and test the regularity of the file. Let’s run the following Bash script:

#!/bin/bash
test -f LinuxHint.txt
echo $?

Output:

Upon running the previous script, we see 0 in the output. Hence, our file is regular. If your file is not regular, it gives us 1 in the output.

Let’s take one more example to clarify everything better. For instance, we can use the Bash -f test condition with an if-else statement.

#!/bin/bash
test -f rufus-3.20.exe
If echo "rufus-3.20.exe"
then
        echo "File is regular"
else
       echo "File is not regular"
fi

Output:

After running the previous script, it tells you that your file is not regular.

Conclusion

In this tutorial, we used the Bash -f test condition to see how to check whether a file is regular or not using a Bash script. Regular files are those that the users easily execute. You can check if the file is regular by using the -f flag with the test command without opening the file. The test command contains various options to check the type of files in Linux.

Share Button

Source: linuxhint.com

Leave a Reply