Bash脚本,用于搜索目录并将文件移动到新目录,删除所有副本 [英] Bash script for searching directory and moving files to a new directory, deleting any copies

查看:71
本文介绍了Bash脚本,用于搜索目录并将文件移动到新目录,删除所有副本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个充满备份数据的2TB硬盘,其中包括数百部电影,由于相同HD的连续备份,其中大多数已被复制了几次.硬盘驱动器按备份文件夹列表进行组织,每个备份都包含一个电影文件夹,其中包含备份时笔记本电脑HD上的所有电影.

I have a 2TB hard drive full of backed up data, including hundreds of movies, most of which have been copied several times due to sequential back-ups of the same HD. The hard drive is organized as a list of back-up folders, and every back-up contains a movies folder that has all the movies that were on my laptop HD at the time of the back-up.

我想创建一个新的电影文件夹,并将所有电影从每个电影"子文件夹移至新的文件夹,并确保不要将同一部电影两次移动.如果我想通过Bash进行所有操作,该怎么办?

I'd like to create a new movies folder and move all movies from every "movies" subfolder into the new one, making sure not to move the same movie twice. How can I go about this if I want to do everything via Bash?

推荐答案

假定电影在各个文件夹中的每个副本都具有相同的名称,并且都具有相同的扩展名(比如说.divx),则可以使用find找到它们并将它们复制到其他文件夹,然后删除旧文件夹.

Assuming that each copy of a movie in various folders has the same name and all have the same extension, let's say .divx, you can use find to find them and copy them to a different folder and then delete the old folders.

find / -iname "*.divx" -type f -print0 | xargs -0 -I '{}' mv "{}" /path/to/new_folder/

或者,您可以循环浏览所有文件并将其复制到new_folder(仅当它们不存在时).如果已经存在,请删除其他副本.像这样:

Or you can loop through all the files and copy them to new_folder only if they are not already present. If already present, delete other copies. Something like this:

for file in $(find . -iname "*.divx" -type f)
do
filename=$(basename ${file})
if [ ! -f ./movie/${filename} ]; then
    mv ${file} ./movie/${filename}
else
    rm ${file}
fi
done

这篇关于Bash脚本,用于搜索目录并将文件移动到新目录,删除所有副本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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