Image for post
Image for post

Test if a file exists (command line)

A handy Unix command is test which can check for the existence of a file for example and 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 to test.txt.YYYMMDD-HHMMSS (appropriate date/time stamp).

Great for scripts that are working with say a log file and need to rename it before starting a new log.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store