Reference: File tests

  1. Comparison operators
  2. Reference
  3. Regular expressions

There are a number of very useful tests you can perform on files. Remember that you’ll usually want to determine if the file exists first, especially for the tests that return a value such as a file size or age.

Test Tests
-e the file exists
-r the file is readable by your script
-w the file is writable by your script
-x the file is executable by your script
-s the size of the file in bytes
-f the file is a plain file, not a directory or alias
-d the file is a directory
-M the time since the file was last modified, in days

Tests can be combined by using the filename for the first test, and the underscore for subsequent tests. For example, to test if a file exists and has been modified less than 24 hours ago:

if (-e $filepath && -M _ < 1) { … }

There are many more tests for more specialized purposes, but these should cover most of your needs.

  1. Comparison operators
  2. Reference
  3. Regular expressions