[英]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.对于一个网站,我想将文件夹中的文件(图片)从“1.jpg,2.jpg,3.jpg ...”重命名为“yyyymmdd_hhmmss.jpg” - 所以我想读出创作times 一组 times 作为图片的名称。 Does anybody have an idea how to do that for example with a linux-shell or with imagemagick?有人知道如何使用 linux-shell 或 imagemagick 做到这一点吗?
Thank you!谢谢!
In the linux shell:在 Linux 外壳中:
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.这将启动所有 jpeg 文件的循环。 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.它使用-r
选项告诉date
显示文件的日期而不是当前日期。 The specification +"%Y%m%d_%H%M%S"
tells date
to format it as you specified.规范+"%Y%m%d_%H%M%S"
告诉date
按照您指定的格式对其进行格式化。
The file name, $f
, is placed in double quotes where ever it is used.文件名$f
放在使用双引号的地方。 This assures that odd file names will not cause errors.这可以确保奇怪的文件名不会导致错误。
The -n
option to mv
tells move never to overwrite an existing file. mv
的-n
选项告诉 move 永远不要覆盖现有文件。
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
To name the file based on the EXIF Create Date (instead of the file system date), we need exiftool
or equivalent:要根据 EXIF 创建日期(而不是文件系统日期) exiftool
文件,我们需要exiftool
或等效项:
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.以上与文件日期的命令非常相似,但使用exiftool
和awk
来提取EXIF图像创建日期。
The exiftool
command provides the date in a format like: exiftool
命令以如下格式提供日期:
$ 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:我们将exiftool
输出传递给awk
以便它可以提取我们想要的字段:
awk '{print $4".jpg"}'
This selects the date field and also adds on the .jpg
extension.这将选择日期字段并添加.jpg
扩展名。
Thanks to @John1024 !感谢@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" "DSC_0189.JPG" ➜ "2016-02-21_18-22-15_DSC_0189.JPG"
"MOV_0131.avi" ➜ "2016-01-01_20-30-31_MOV_0131.avi" "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;我创建了一个 shell 脚本; I think it's mac only, linux might need other arguments.我认为它只是mac,linux可能需要其他参数。
#!/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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.