Test if a file exists

Another really useful linux command is test, which can be used to test if a file exists and then rename it.

Here’s an example:

$ test -f test.txt && mv test.txt test.txt.`date +%Y%m%d-%H%M%S`

This first tests if the file test.txt exists. If it does, it renames the file test.txt.YYYMMDD-HHMMSS (appropriate date/time stamp). Great for scripts that are working with a log file and need to rename it before starting a new log.