我有一个安装在/ run / media / jeremy / MONSTER的驱动器,它有多年的光盘库备份,iPhoto图库和多年来主目录的常规备份。
我想跟踪每个gif,jpg,jpeg和cannon原始文件并将它们复制到/run/media/jeremy/1.4t_Video/photos并最佳地根据文件给它们一个唯一的名称创立日期。我认为bash可以胜任这项任务,但不确定如何去做。
答案 0 :(得分:1)
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 *。
答案 1 :(得分:1)
你可以使用这样的脚本:
#!/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