在 bash 中复制整个驱动器上的所有照片?

Copy all photos on an entire drive in bash?

我有一个安装在 /run/media/jeremy/MONSTER 的驱动器,它有大量光圈库、iPhoto 库的旧备份,以及多年来主目录的一般备份。

我想追踪每一个 gif、jpg、jpeg 和 cannon 原始文件并将它们复制到 /run/media/jeremy/1.4t_Video/photos 并最好根据文件创建日期。我认为 bash 可以胜任这项任务,但不确定如何去做。

find /run/media/jeremy/MONSTER \( -iname \*.gif -o -iname \*.jpg -o -iname \*.jpeg \) -exec cp {} /run/media/jeremy/1.4t_Video/photos/ \;

这应该会找到所有以这些扩展名结尾的文件并将它们复制到 /run/media/jeremy/1。4t_Video/photos/

如果你想添加更多扩展,只需使用 -o -iname *.

您可以使用这样的脚本:

#!/bin/bash
serial=0
find /run/media/jeremy/MONSTER \( -iname \*.gif -o -iname \*.jpg -o -iname \*.jpeg -o -iname \*.cr2 \) -print0 |
   while read -r -d '' f; do

      # Get its extension, like "jpg" or "cr2"
      ext=${f##*.}

      # Get its date of creation like "2012-01-07 11:06:45"
      datetime=$(stat --printf="%w" "$f" | sed 's|\..*||')

      # Formulate a new name
      new="/output/dir/${datetime}-${serial}.${ext}"

      # Show what we came up with
      echo Would copy $f to $new
      # cp "$f" "$new"

      ((serial++))
   done

目前它什么都不做,它只是告诉你它会做什么。取消注释倒数第 3 行,如果它看起来正确,则删除 cp 前面的 #。请先备份,然后用少量文件测试...

示例输出

Would copy ./b.jpg to /output/dir/2016-02-11 10:40:58-0.jpg
Would copy ./mosaic/0.jpg to /output/dir/2016-02-08 12:36:06-1.jpg
Would copy ./mosaic/1.jpg to /output/dir/2016-02-08 12:36:07-2.jpg
Would copy ./mosaic/10.jpg to /output/dir/2016-02-08 12:36:12-3.jpg
Would copy ./mosaic/100.jpg to /output/dir/2016-02-08 12:36:41-4.jpg
Would copy ./mosaic/101.jpg to /output/dir/2016-02-08 12:36:42-5.jpg
Would copy ./mosaic/102.jpg to /output/dir/2016-02-08 12:36:42-6.jpg
Would copy ./mosaic/103.jpg to /output/dir/2016-02-08 12:36:42-7.jpg