linux-shell:将文件重命名为创建时间 [英] linux-shell: renaming files to creation time

查看:525
本文介绍了linux-shell:将文件重命名为创建时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家早上好,

一个网站我想在1.jpg,2.jpg,3的文件夹中重命名文件(图片)。 jpg ...toyyyymmdd_hhmmss.jpg - 所以我想把这一次的创作时间读出来作为照片的名字。有没有人知道如何使用linux-shell或imagemagick来做到这一点?

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?

谢谢!

推荐答案

根据文件系统日期命名



在linux shell中:

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

解释


  • for f in * .jpg
    do

这将启动所有jpeg文件的循环。这样做的一个特点是它可以使用所有文件名,甚至名称中包含空格,制表符或其他困难字符的文件名。

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$(日期-r$ f+%Y%m%d_%H%M%S)。jpg

这将重命名文件。它使用 -r 选项告诉 date 显示文件的日期而不是当前日期。规范 +%Y%m%d_%H%M%S告诉 date 将其格式化为你指定。

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.

文件名 $ f 放在双引号中,无论何时使用。这可以确保奇数文件名不会导致错误。

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

-n 选项mv 告诉move永远不要覆盖现有文件。

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

已完成

这样就完成了循环。

对于交互式使用,您可能更喜欢命令全部在一行上。在这种情况下,使用:

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



根据EXIF创建日期命名



要根据EXIF创建日期(而不是文件系统日期)命名文件,我们需要 exiftool 或等价物:

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

解释

以上内容非常类似于文件日期的命令,但使用的是 exiftool awk 提取EXIF图像创建日期。

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 命令以如下格式提供日期: / p>

  • 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.

我们将 exiftool 输出传递给 awk 这样它就可以提取我们想要的字段:

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

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

这会选择日期字段并添加 .jpg 扩展。

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

这篇关于linux-shell:将文件重命名为创建时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆