Linux 根据创建日期重命名 JPG 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4710753/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 02:29:49  来源:igfitidea点击:

Rename JPG files according to date created

linuxunixcommand-line

提问by Neilvert Noval

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:

我想重命名某个目录中的所有文件。将它们重命名为它们的创建日期。
因此,如果我的文件是 Image1.jpg,它应该重命名为“Jan 16 12:09:42 2011.jpg”之类的内容,
我想通过命令行执行此操作。我一直在尝试:

stat -f %SB Image0100.jpg

But how can I combine this with mvcommand? And how will I iterate statand mvthrough the whole files?
Or are there simple ways to rename all files with their date creation?

但是我怎样才能将它与mv命令结合起来呢?我将如何迭代stat并遍历mv整个文件?
或者是否有简单的方法来重命名所有文件及其创建日期?

采纳答案by Theo Band

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

any strftime argument can be given as well:

也可以给出任何 strftime 参数:

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

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

这将以 YYYYMMDD-HHMMSS 格式重命名与 *.jpg 匹配的文件

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.

另请参阅手册页以了解许多其他很酷的选项。例如,您可以更正(移动)EXIF 日期。当某些相机的时间设置不正确时,这在合并来自不同相机的文件时非常方便。

回答by Ignacio Vazquez-Abrams

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.

大多数 POSIX 文件系统不记录创建时间(即使它被记录,API 当然也不会公开它),所以你是 SOL。

回答by DigitalRoss

$ for i in *; do mv "$i" "`stat -f %SB $i`"; done

回答by fseto

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

如果您正在使用包含EXIF数据(即来自数码相机)的JPG ,那么您可以使用以下内容来获取创建日期而不是 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 dstis 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.
  • 由于并非每个文件都有 exif 数据,我们希望dst在执行其余命令之前检查它是否有效。
  • exif 的输出有一个空格,它是文件名的 PITA。使用 sed 替换为“-”。
  • 请注意,我在 mv 之前使用了 'echo' 来测试我的脚本。当您确信它正在做正确的事情时,您就可以删除“回声”……您不希望最终成为将所有文件都炸掉的那个人。

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

回答by ghostdog74

for file in *jpg
do
 newfile=$(date "+%b %d %H:%M:%S%Y $file" -d "$(stat -c "%y" $file)")
 mv "$file" "$newfile"
done$

回答by Jonathan Leffler

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

在 MacOS X 10.6.6 上使用 Bash(我认为,使用 Korn shell),这适用于其中包含空格的文件名:

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:

它还使用源自 ISO 8601 的符号表示日期和时间。显然,如果两个文件具有相同的修改时间,那么按字母顺序排列的最后一个文件是唯一可以保留的文件。这也失去了任何后缀。如果您正在处理“.jpg”文件,您可能想要使用:

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.

由于输出名称 ($x) 中没有空格,因此在 move 命令中不需要将 "$x" 括起来,但它们的存在是一致的。

回答by tchrist

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

只需使用exiftool。这是其文档中的一个示例:

   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.

回答by akond

find . -type f | xargs stat -f "mv %%N \"%%SB.jpg\"" | awk -F \; '{system ;}'

回答by xxjjnn

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

以 RP_20120801_063021.jpg 为例。这适用于 Debian Linuxes(例如 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...

重命名命令适用于某些图像为 .JPG 或 .jpeg 的情况。无法将 .JPG 直接转换为 .jpg,因为它被解释为相同...

回答by three

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.

如果你像我一样,因为年纪大了,记不住东西而不得不一遍又一遍地谷歌搜索,你也可以使用 gThumb,它包含一个批处理重命名器,它运行良好,并有一个旧名称 -> 新名称预览。真不错。