Skip to content

testing for the existence of a file

In Ubuntu, the test command can be used to test for the existence of a file. For example, the following script uses test and an if-construct to test for the existence of a file named foo.bar:

FNAME="foo.bar" ; if test ! -s "$FNAME" ; then echo "$FNAME does not exist or is empty." ; else echo "$FNAME exists." ; fi

explanation

Note

This is an incomplete explanation.

  • The test ! -s "$FNAME" command tests for the existence of a file with a name stored in the $FNAME variable, returning true if the filename exists. This directs the script's control flow to either show a message indicating that the file does not exist or is empty, or show a message indicating that the file exists.

prior work

licensing

Some rights reserved: CC BY-SA 3.0. Includes significant content from test (Unix)§Examples on Wikipedia, with changes made.