Rename file names in directory to creation timestamp with second precision
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 themv
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 thebash
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 thebash
man page.
-
Note that the upper case character names are that which show in Character Viewer on a US English install of macOS.