Testing files

There is quite a lot to say about testing and one of the most common tasks is checking files on the filesystem or if a directory is available or has some rights. So, imagine a script that has to write some data in a file inside a directory: first, we should check whether the directory exists, then if we can write into it, and finally if there is no name collision between the file we are going to open for writing and an already existing file. So let's have a look at which operators we use to execute some tests on files and devices and remember that they return true if the condition is met:

  • -e: Returns true if a file exists:
zarrelli:~$ ls test-files.sh test-files.sh  

We just verified that the file test-files.sh exists since ...

Get Mastering Bash now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.