How to Determine If a File Exists in Bash
A common action that comes up frequently while working in Linux is determining whether or not a file exists before taking action.
In Bash, there are two ways this can be completed.
The first way to check if a file exists in Bash is with the test
command with the -f
option.
test
compares an element against another and returns true or false.
The test
command can be expressed in three different syntaxes:

Let's say we want to figure out if the file /tmp/cased.txt
exists.
By combining the test
command with an if
statement, this can be easily accomplished.

If the file exists, this command will return a File exists.
If the file does not exist, it will return File does not exist.
In this example we used the test
command in the [ ]
syntax; however we can accomplish the same goal with the other syntaxes as shown below.


You can save useful shell shortcuts like this, and a whole lot more, with Cased. If you're interested and want to learn more schedule a demo or visit our documentation.