Rename file names in directory to creation timestamp with second precision

automatorfilefilesystemrenameterminal

Is there an easy way to do this for files in a particular directory – to have their names be renamed to the file timestamp, with seconds level precision

Clarification:

For example, suppose a (Mac 10.14.15) directory has the following files, where the notation below shows original file name and file creation time. (But, to be clear, we are working from the Mac file directory, and not from the luxury of just parsing thru a text file.) The filenames are uuid 36 character names:

35eafb32-8005-11e9-bc42-526af7764f64.png 09:35:01
419f0eb4-8005-11e9-bc42-526af7764f64.png 09:35:11
43249984-8005-11e9-bc42-526af7764f64.png 21:02:03

Suppose I want to rename the files based on (original creation) timestamp, but the filename of course has to be in an acceptable format:

prepend-09_35_01.png
prepend-09_35_11.png
prepend-21_02_03.png

(In the actual case, the directory has 2000+ files. If it matters, they are transported from an iPhone X app I made – through iTunes file sharing. The file creation date and time shows up correctly on Mac when transferred.)

Best Answer

If all the files are in the same directory, then in Terminal, changed directory, e.g.
cd /path/to/directory and then use the following compound command:

for f in *.*; do echo mv -n "$f" "${f%.*}-$(stat -f'%SB' -t "%H_%M_%S" "$f").${f##*.}"; done

Note: There is an intensional echo command that is there for you to test with, and it will only print out the mv command for you to see that it's the outcome you wish. You can then run it again removing just echo from the compound command to actually rename the files as desired via the mv command.

Example:

$ touch 35eafb32-8005-11e9-bc42-526af7764f64.png 419f0eb4-8005-11e9-bc42-526af7764f64.png 43249984-8005-11e9-bc42-526af7764f64.png
$
$ ls -1
35eafb32-8005-11e9-bc42-526af7764f64.png
419f0eb4-8005-11e9-bc42-526af7764f64.png
43249984-8005-11e9-bc42-526af7764f64.png
$
$ for f in *.*; do echo mv -n "$f" "${f%.*}-$(stat -f'%SB' -t "%H_%M_%S" "$f").${f##*.}"; done
mv -n 35eafb32-8005-11e9-bc42-526af7764f64.png 35eafb32-8005-11e9-bc42-526af7764f64-19_56_35.png
mv -n 419f0eb4-8005-11e9-bc42-526af7764f64.png 419f0eb4-8005-11e9-bc42-526af7764f64-19_56_35.png
mv -n 43249984-8005-11e9-bc42-526af7764f64.png 43249984-8005-11e9-bc42-526af7764f64-19_56_35.png
$
$ for f in *.*; do mv -n "$f" "${f%.*}-$(stat -f'%SB' -t "%H_%M_%S" "$f").${f##*.}"; done
$
$ ls -1
35eafb32-8005-11e9-bc42-526af7764f64-19_56_35.png
419f0eb4-8005-11e9-bc42-526af7764f64-19_56_35.png
43249984-8005-11e9-bc42-526af7764f64-19_56_35.png
$ 


Understanding the bash compound command:

  • for f in *.*; do ...; done    For every file in the current dir do the following command(s) to them.
  • echo    Print what follows. (Must be removed for the mv command to actually work.)
  • mv -n source target    Move files, do not overwrite an existing file, filename, new filename.
    • "$f"    The source filename.
    • "${f%.*}-$(stat -f'%SB' -t "%H_%M_%S" "$f").${f##*.}"    The target filename.
    • ${f%.*}    Name portion of the filename without the extension. See Parameter Expansion in the bash man page.
    • -    The HYPHEN-MINUS character.
    • $(...)    Command Substitution, allows the output of a command to replace the command name.
    • stat -f'%SB' -t "%H_%M_%S" "$f"
    • stat    Display file status. See its man page for more details.
    • -f'%SB'    −f format    Display information using the specified format. See the FORMATS section for a description of valid formats. In this case %SB represents string output of the birth time of the inode. (The time the file was created.)
    • -t "%H_%M_%S"    −t timefmt    Display timestamps using the specified format. This format is passed directly to strftime(3). In this case it represents the hour, a LOW LINE character (AKA underscore), the minute, a LOW LINE character, the second.
    • %H    Is replaced by the hour as a decimal number (00-23).
    • %M    Is replaced by the minute as a decimal number (00-59).
    • %S    Is replaced by the second as a decimal number (00-60).
    • "$f"    The source filename.
    • .    The FULL STOP character. (AKA dot or period.)
    • ${f##*.}    The extension portion of the filename. See Parameter Expansion in the bash man page.

Note that the upper case character names are that which show in Character Viewer on a US English install of macOS.