Linux shell script ‐f file test operator

11:40 PM , , 0 Comments

If you're writing a linux shell script, or reading one, you might be wondering what the "-f" operator actually is, or why it doesn't work for you. This article describes about what the "-f" operator does in a linux shell script and also goes into detail about the other operators related to it.

Introduction to Linux shell script operators

There are three types of operators in shell script - file test operators, conditional operators and arithmetic operators.

File Test Operators - Check for the existence of a file (can be limited to a specific type of file such as directory, regular file, symbolic links, etc) or the properties/attributes of a file such as read/write permissions, file ownership information, etc. The "-f" operator is a file test operator.

What is "-f" operator?

The -f operator is used to check the existence of a regular file (Check out different file types of linux here). It does not check whether other types of files exist, such as directory, synbolic links. If you have to check whether the existence of such files, you can use "-b" (for block devices), "-c" (for character devices) and "-d" (for directories). Also, all these different kinds of files can be tested using the "-e" operator. The "-f" operator only checks whether the specified file is a regular file (neither a device file, nor a directory). So if you want to test the existence of a file, regardless of its type, then you can use "-e" operator. Bash shell also supports "-a" operator, which accomplishes the same as "-e" operator.

Lets look at the sample linux shell script, which uses different file operators such as "-f", "-e" and "-a" and the result of this script's execution under different shells:

Sample Script

Execution Result

Under bash shell

The file exists (-a operator)
Regular file exists (using -f operator)
Character special file exists (using -c operator)
Block special file exists (using -b operator)
Directory exists (using -d operator)
The file exists (-e operator)

As you can see, both the "-a" and the "-e" operators perform the same function under bash - check for the existence of any kind of file. The usage of "-f" in a bash shell script is deprecated.

Under dash shell

file-existence-check.sh: 1: [: -a: unexpected operator
The file does not exist (-a operator)
Regular file exists (using -f operator)
Character special file exists (using -c operator)
Block special file exists (using -b operator)
Directory exists (using -d operator)
The file exists (-e operator)

The dash shell does not support the usage of "-a" operator. So you can use "-e" operator in its place.

Under zsh shell

file-existence-check.sh:[:1: too many arguments
The file does not exist (-a operator)
Regular file exists (using -f operator)
Character special file exists (using -c operator)
Block special file exists (using -b operator)
Directory exists (using -d operator)
The file exists (-e operator)

Like dash, zsh too, does not support the usage of "-a" operator. Here too, only "-e" operator instead of "-a" operator

So, you can consider the "-e" operator to be the universal existence checker for all files. And "-f" operator just checks for the existence of a regular file. Thus, in your linux shell script, use "-f" to check whether a file exists and is just a regular file.

References

  1. General Overview of Linux Filesystem
  2. TLDP's Bash guide for Beginners

A LINUX enthusiast and tech passionate guy. Love to watch Anime and play cricket. Got extra bit of care in myself, guess that's what people call selfishness :-P Google+