ddb ddb - 4 months ago 18
Bash Question

How to rename a list of files in directory based on creation time on mac

I'm on a Mac bash console.

I googled al lot (even here in stack overflow) and tried a lot of scripts in order to rename each file in a directory with its creation timestamp via a bash command, but none worked, because

date
and
stat
commands are different in mac rather than in linux. Also I do not have
exif
command.

I'd like to rename with format like this (or anyway similar): YYYYmmDD-HHMM

Can anybody help me? Thanks a lot in advance

EDIT

I tried this, this and this

EDIT 2

I'd like to rename with format like this (or anyway similar): YYYYmmDD-HHMM.jpg (jpg is an example of file extension, I'd like to not to lose file extension)

EDIT 3

Currently this command partially works (always apply jpg extension)

for f in `ls`; do mv "$f" "$(stat -t %Y%m%d-%H%M $f | awk -F"\"" '{print $8}').jpg"; done

Answer
mv fileName `stat -f %SB -t %Y%m%d-%H%M fileName`

EDIT

here final solution, looping among whole file list

for f in `ls`;
    do mv "$f" "`stat -f %SB -t %Y%m%d-%H%M $f`.`echo $f | cut -d '.' -f 2`";
done
Comments