• Uncategorized

About linux : Bash-Rename-file-name-to-unix-timestamp-of-modification-date

Question Detail

Hello I need to rename files to their unix time stamp of their modification date and append it as a prefix.

In other words I need a script to mass rename
Files like





1649493072000 ABC.jpg

1649493072182 XYZ.png

I also like to append a u- in front of every file that is modified this way.

SO I like to turn





u-1649493072000 ABC.jpg

u-1649493072182 XYZ.png


To all mods please note that my question is different from other questions already asked since I’m asking about the UNIX Modification timestamp of the file not the ISO date like 2022-04-09.

Question Answer

find . -type f -exec \
sh -c '
for i do
    d=$(dirname "$i")
    [ "$d" = / ] && d=
    echo mv "$i" "$d/u-$(stat -c %Y "$i") $n"
done' _ {} +
  • This operates recursively in the current directory (.). It only targets regular files (not directories etc). Modify -type f and other flags if needed.

  • It just prints the mv commands, so you can review them. Remove the echo to run for real.

  • We use find to list the target files, and its -exec flag to pass this list to a shell loop where we can parse and modify the filenames, including stat to get the modification time.

  • I don’t know your use case, but a better solution may be to just save the output of: find . -type f -printf '%p u-%[email protected]\n' in a file, for later reference (this prints the file path and modification time on the same line). Also, maybe a snapshot (if possible).

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.