31

I want to rename all files in a certain directory. Renaming them to their date of creation.
So if my file is Image1.jpg, it should rename into something like "Jan 16 12:09:42 2011.jpg"
I want to do this through command line. I've been trying:

stat -f %SB Image0100.jpg

But how can I combine this with mv command? And how will I iterate stat and mv through the whole files?
Or are there simple ways to rename all files with their date creation?

4
  • 2
    No simple way but easy with a shell script. However, consider that after you rename all files that way they will NOT sort correctly in a directory listing. April will be first, followed by August and December... is that what you really want? Or, do you want to use a filename like yyyymmdd.hhmmss,jpg? Jan 17, 2011 at 7:05
  • @jim: Either way jim, as long as it is renamed. Jan 17, 2011 at 7:11
  • 1
    I'd second Jim's suggestion; use an ISO 8601 type format (such as yyyymmdd.hhmmss.jpg) to make it easier to view things in time order. Jan 17, 2011 at 8:24
  • 3
    Not programming... Belongs on Superuser.
    – leppie
    Jan 17, 2011 at 8:31

11 Answers 11

63
jhead -n DSCN0382.JPG
DSCN0382.JPG --> 0408-150734.jpg

any strftime argument can be given as well:

jhead -n%Y%m%d-%H%M%S *.jpg 

This will rename files matched by *.jpg in the format YYYYMMDD-HHMMSS

jhead -n%Y%m%d-%H%M%S DSCN0382.JPG
DSCN0382.JPG --> 20120408-150734.jpg

see also the man page for lots of other cool options. You can for instance correct (shift) the EXIF date. This is very handy when merging files from different camera's when some camera's have an incorrect time set.

3
  • 5
    I quickly want to add from the man page: "If the target name already exists, the name will be appended with "a", "b", "c", etc, unless the name ends with a letter, in which case it will be appended with "0", "1", "2",etc." Mar 3, 2013 at 14:51
  • 1
    For mac users, jhead is available on homebrew and works the same Feb 28, 2015 at 11:35
  • 1
    Thanks. My prefered format string for jhead is the following: jhead -nIMG_%04i_%d-%m-%Y *.jpg which will generate IMG_0001_31-12-2014.jpg
    – rodvlopes
    May 2, 2015 at 15:03
15

If you're working with JPG that contains EXIF data (ie. from digital camera), then you can use following to get the creation date instead of stat.

exif -t 0x9003 -m Image0100.jpg

Per request, here's the command and output. A couple of points to note:

  • Since not every file has exif data, we want to check that dst is valid before doing the rest of commands.
  • The output from exif has a space, which is a PITA for filenames. Use sed to replace with '-'.
  • Note that I use 'echo' before the mv to test out my scripts. When you're confident that it's doing the right thing, then you can remove the 'echo'... you don't want to end up like the guy that got all the files blown away.

Command

for i in *.jpg; do
  dst=$(exif -t 0x9003 -m $i ) &&
  dst_esc=$(echo $dst | sed 's/ /-/g' ) &&
  echo mv $i $dst_esc.jpg
done

Output

'12379632.jpg' does not contain tag 'DateTimeOriginal'.
mv 15084688.jpg 2003:02:28-21:48:54.jpg
mv 15136312.jpg 2003:03:01-10:36:05.jpg
mv 15137960.jpg 2003:03:01-10:36:38.jpg
mv 15140744.jpg 2003:03:01-10:37:46.jpg
3
  • +1: Interesting...! Even better if you showed the sample output, and how to do the rename from it. Jan 17, 2011 at 8:25
  • 2
    I would add a second filter to remove ":" characters, a MUST if you plan to share the files with a Windows OS! dst_esc=$(echo $dst | sed 's/ /-/g' | sed 's/:/-/g' ) Nov 16, 2014 at 16:20
  • Brilliant! Been through a lot of tools, this is the first that actually worked...
    – Hornbech
    Dec 10, 2014 at 10:50
5
for file in *jpg
do
 newfile=$(date "+%b %d %H:%M:%S%Y $file" -d "$(stat -c "%y" $file)")
 mv "$file" "$newfile"
done$
2

There is also:

rename -v 's/.JPG/.jpeg/' *.JPG
rename -v 's/.jpeg/.jpg/' *.jpeg
exiv2 -r'RP_%Y%m%d_%H%M%S' rename *.jpg

to give RP_20120801_063021.jpg for example. This works for Debian Linuxes (e.g. Ubuntu).

The rename commands are for when some images are .JPG or .jpeg. Cannot convert .JPG directly to .jpg as its interpreted as being the same...

1
  • 1
    Many people on the internet have searched for an answer to the greatest question of them all - How do i sort all the photos I have which I'm never going to look at? Although mine can all go in the same folder thanks to their naming, I've grouped them into folders called like '2012_01_January_Shenanigans' and '2012_06_Scootland_holiday' of about 100 photos each.
    – xxjjnn
    Dec 23, 2012 at 17:41
1

Most POSIX filesystems do not record creation time (and the API certainly doesn't expose it even if it is recorded), so you're SOL.

1
  • 1
    +1: This is strictly correct. There's the 'inode change time', known as ctime, which records when the inode last changed - for permissions, or links, or owner, or ... and there's the 'modify time' or mtime when the content last changed. Those are your closest approximations, but they are neither of them very good unless there is some other constraint that dictates that the files are not changed after creation. Jan 17, 2011 at 8:22
1
$ for i in *; do mv "$i" "`stat -f %SB $i`"; done
3
  • How about if the file name has space character? I tried this code, but got problem on file names with space character. Jan 17, 2011 at 7:13
  • lol I just tried this and it blew away all my files except for one
    – James
    Jan 17, 2011 at 7:16
  • 1
    An easy problem to run into with this is if you cp a directory for testing purposes. The problem, then, is that unless you did cp -p ... then all of the files have the same times on them, so they end up aliasing each other for names, and only the last one is left standing. Hey, I just answer the questions; I try not to comment on whether it's really a good idea or not... Jan 17, 2011 at 9:54
1
find . -type f | xargs stat -f "mv %%N \"%%SB.jpg\"" | awk -F \; '{system $1;}'
1
  • You can alternatively pipe to "sh" rather than awk running "system" on each line. May 15, 2015 at 7:01
1

If you, like me, have to google this over and over again because you're getting older and can't remember things, you can also use gThumb which has a batch renamer included which works well and has a old name -> new name preview. Real nice.

1
  • 1
    So I actually googled it again and found my own answer. Thank you, three :) BTW, Select all pictures you want to rename and hit F2. That's it. My preferred format is this one: %M{%Y-%m-%d_%H-%M-%S}%E which translates to something like 2015-06-06_18-48-46.jpg
    – three
    Jun 7, 2015 at 12:03
0

On MacOS X 10.6.6 with Bash (and, I think, with Korn shell), this works on file names with spaces in them:

echo > "x y"
for file in *
do
    x=$(stat -f '%SB' -t '%Y%m%d.%H%M%S' "$file")
    mv "$file" "$x"
done

It also uses the notation derived from ISO 8601 for the date and time. Clearly, if two files have the same modification time, the last of those file in alphabetic order is the only one that will survice. This also loses any suffix. If you're dealing with '.jpg' files, you'd probably want to use:

echo > "x y,jpg"
for file in *.jpg
do
    x=$(stat -f '%SB' -t '%Y%m%d.%H%M%S.jpg' "$file")
    mv "$file" "$x"
done

Since the output name ($x) has no spaces in it, there's no need for the quotes around "$x" in the move command, but their presence is consistent.

0

Just use exiftool. Here’s an example from its documentation:

   exiftool -r '-FileName<CreateDate' -d %Y-%m-%d/%H%M_%%f.%%e dir
        Both the directory and the filename may be changed together via the
        "FileName" tag if the new "FileName" contains a '/'.  The example above
        recursively renames all images in a directory by adding a "CreateDate"
        timestamp to the start of the filename, then moves them into new
        directories named by date.
0

A perl alternative:

I really like the short pattern to add a fixed-string prefix with perl:

/bin/ls *.jpg | perl -pe 's/.*/mv "$&" "prefix_$&"/' | sh

To make a date prefix, it is a bit more complicated.

cat >t.pl

while(<*.jpg>) {
    $mtime = (stat($_))[9];
    ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($mtime);
    $year += 1900;
    ++ $mon;
    printf("mv \"%s\" \"%d-%.2d-%.2d-%s\"\n", $_, $year, $mon, $mday, $_)
}

perl t.pl | sh

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.