Bash: Rename all files in current directory to include the time

I have a directory of files and I want to append the creation date (mtime) to the filename for archival purposes. I worked up a simple bash command to do it:

for i in *; do date_raw=$(stat --format %Y "$i"); date_format=$(date --date=@$date_raw +%Y-%m-%d); mv "$i" "$date_format-$i"; done;

Basically it stats the file to get the mtime, then uses date to format it, and finally renames the file.

Leave A Reply - 1 Reply
Replies
remke 2014-01-12 12:48pm - r.schuurmans@gmail.com - Logged IP: 83.83.74.116

Very nice, yust wat i was looking for (renaming downloads) Thx!

~remke

All content licensed under the Creative Commons License