Add date to files in Ubuntu
Even though I have been running Ubuntu as my main OS for more than a year now, I am still trying to figure out a good workflow. One thing I have been missing from my former OSX setup was the ability to quickly and easily prepend the date to a number of files. Having moved my files between many different OSes, hard drives, network drives, etc. over many years, I know that the files’ creation dates will break at some point. For that reason, I prefer to prepend the date to filenames of photos, random text files, etc. That way I am able to quickly search through the files easily.
On OSX I have made a small Automator script called add-date that does exactly this: prepends a file’s creation date to the filename. It has worked like a charm for many years, and still works, so feel free to try it out if you are on OSX.
In Ubuntu there are, of course, numerous terminal based methods to do something like this. But I haven’t really been that eager to mess around with mv, rename and regexp just to rename a bunch of files once in a while. In the quietness of the Norwegian summer I finally got around to look a little more into how to do this in Ubuntu, and it turned out to be very easy. Nautilus, the file manager on Ubuntu, actually sports the ability to run scripts from the GUI. The scripts, of any programming flavor (python, perl, etc.) just need to be put in the folder:
There are many examples of adding today’s date to the files, such as this python example, but it took a little while to figure out how to do it using the modification date of the file. I finally got this to work as a little bash script:
#!/bin/sh for f in "$@"; do mv "$f" "$(date -d@$(stat --printf='%Y' "$f") +%Y-%m-%d)-$f"; done
Save this as a file in the scripts folder, make it executable (chmod a+x), and restart nautilus. Then I can easily add the date to all files I want directly from the GUI.