简体   繁体   中英

linux-shell: renaming files to creation time

Good morning everybody,

for a website I'd like to rename files(pictures) in a folder from "1.jpg, 2.jpg, 3.jpg..." to "yyyymmdd_hhmmss.jpg" - so I'd like to read out the creation times an set this times as names for the pics. Does anybody have an idea how to do that for example with a linux-shell or with imagemagick?

Thank you!

Naming based on file system date

In the linux shell:

for f in *.jpg
do
    mv -n "$f" "$(date -r "$f" +"%Y%m%d_%H%M%S").jpg"
done

Explanation :

  • for f in *.jpg do

    This starts the loop over all jpeg files. A feature of this is that it will work with all file names, even ones with spaces, tabs or other difficult characters in the names.

  • mv -n "$f" "$(date -r "$f" +"%Y%m%d_%H%M%S").jpg"

    This renames the file. It uses the -r option which tells date to display the date of the file rather than the current date. The specification +"%Y%m%d_%H%M%S" tells date to format it as you specified.

    The file name, $f , is placed in double quotes where ever it is used. This assures that odd file names will not cause errors.

    The -n option to mv tells move never to overwrite an existing file.

  • done

    This completes the loop.

For interactive use, you may prefer that the command is all on one line. In that case, use:

for f in *.jpg; do mv -n "$f" "$(date -r "$f" +"%Y%m%d_%H%M%S").jpg"; done

Naming based on EXIF Create Date

To name the file based on the EXIF Create Date (instead of the file system date), we need exiftool or equivalent:

for f in *.jpg
do
    mv -n "$f" "$(exiftool -d "%Y%m%d_%H%M%S" -CreateDate "$f" | awk '{print $4".jpg"}')"
done

Explanation :

The above is quite similar to the commands for the file date but with the use of exiftool and awk to extract the EXIF image Create Date.

  • The exiftool command provides the date in a format like:

     $ exiftool -d "%Y%m%d_%H%M%S" -CreateDate sample.jpg Create Date : 20121027_181338

    The actual date that we want is the fourth field in the output.

  • We pass the exiftool output to awk so that it can extract the field that we want:

     awk '{print $4".jpg"}'

    This selects the date field and also adds on the .jpg extension.

Thanks to @John1024 !

I needed to rename files with different extensions in the same time, according to last modification date :

for f in *; do
  fn=$(basename "$f")
  mv "$fn" "$(date -r "$f" +"%Y-%m-%d_%H-%M-%S")_$fn"
done

"DSC_0189.JPG" ➜ "2016-02-21_18-22-15_DSC_0189.JPG"

"MOV_0131.avi" ➜ "2016-01-01_20-30-31_MOV_0131.avi"

If you don't want to keep original filename :

mv "$fn" "$(date -r "$pathAndFileName" +"%Y-%m-%d_%H-%M-%S")"

Hope it helps noobs as me !

Try this

for file in `ls -1 *.jpg`; do name=`stat -c %y $file | awk -F"." '{ print $1 }' | sed -e "s/\-//g" -e "s/\://g" -e "s/[ ]/_/g"`.jpg; mv $file $name; done

Though there might be an easier way.

I created a shell script; I think it's mac only, linux might need other arguments.

#!/bin/bash

BASEDIR=$1;
for file in `ls -1 $BASEDIR`; do 
    TIMESTAMP=`stat -f "%B" $BASEDIR/$file`;
    DATENAME=`date -r $TIMESTAMP +'%Y%m%d-%H%M%S'`-$file
    mv -v $BASEDIR/$file $BASEDIR/$DATENAME;
done

Change filename based on file creation time:

exiftool "-filename<FileCreateDate" -d %Y%m%d_%H%M%S%z%%-c.%%le input.jpg

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM