I have 2 different recursive directories, in one directory have 200 .txt files in another have 210 .txt files, need a script to find the different file names and remove them from the directory.
我有2个不同的递归目录,在一个目录中有200个.txt文件,另一个有210个.txt文件,需要一个脚本来查找不同的文件名并从目录中删除它们。
2 个解决方案
#1
3
There are probably better ways, but I think about:
可能有更好的方法,但我想:
find directory1 directory2 -name \*.txt -printf '%f\n' |
sort | uniq -u |
xargs -I{} find directory1 directory2 -name {} -delete
find directory1 directory2 -name \*.txt -printf '%f\n'
: print basename of each file matching the glob *.txt
find directory1 directory2 -name \ * .txt -printf'%f \ n':打印与glob * .txt匹配的每个文件的basename
sort | uniq -u
: only print unique lines (if you wanted to delete duplicate, it would have been uniq -d
)
排序| uniq -u:只打印唯一的行(如果你想删除重复,那就是uniq -d)
xargs -I{} find directory1 directory2 -name {} -delete
: remove them (re-specify the path to narrow the search and avoid deleting files outside the initial search path)
xargs -I {}查找directory1 directory2 -name {} -delete:删除它们(重新指定缩小搜索范围的路径,避免删除初始搜索路径之外的文件)
Notes
Thank's to @KlausPrinoth for all the suggestions.
感谢@KlausPrinoth的所有建议。
Obviously I'm assuming a GNU userland, I suppose people running with the tools providing bare minimum POSIX compatibility will be able to adapt it.
显然我假设一个GNU用户空间,我想人们运行提供最低POSIX兼容性的工具将能够适应它。
#2
1
Yet another way is to use diff
which is more than capable in finding file differences in files in directories. For instance if you have d1
and d2
that contain your 200 and 210 files respectively (with the first 200 files being the same), you could use diff
and process substitution
to provide the names to remove to a while loop:
另一种方法是使用diff,它能够找到目录中文件的文件差异。例如,如果您的d1和d2分别包含200和210个文件(前200个文件相同),则可以使用diff和process substitute来提供要删除到while循环的名称:
( while read -r line; do printf "rm %s\n" ${line##*: }; done < <(diff -q d1 d2) )
Output (of d1 with 10 files, d2 with 12 files)
输出(d1有10个文件,d2有12个文件)
rm file11.txt
rm file12.txt
diff
will not fit all circumstances, but is does a great job finding directory differences and is quite flexible.
diff不适合所有情况,但是找到目录差异并且非常灵活。
#1
3
There are probably better ways, but I think about:
可能有更好的方法,但我想:
find directory1 directory2 -name \*.txt -printf '%f\n' |
sort | uniq -u |
xargs -I{} find directory1 directory2 -name {} -delete
find directory1 directory2 -name \*.txt -printf '%f\n'
: print basename of each file matching the glob *.txt
find directory1 directory2 -name \ * .txt -printf'%f \ n':打印与glob * .txt匹配的每个文件的basename
sort | uniq -u
: only print unique lines (if you wanted to delete duplicate, it would have been uniq -d
)
排序| uniq -u:只打印唯一的行(如果你想删除重复,那就是uniq -d)
xargs -I{} find directory1 directory2 -name {} -delete
: remove them (re-specify the path to narrow the search and avoid deleting files outside the initial search path)
xargs -I {}查找directory1 directory2 -name {} -delete:删除它们(重新指定缩小搜索范围的路径,避免删除初始搜索路径之外的文件)
Notes
Thank's to @KlausPrinoth for all the suggestions.
感谢@KlausPrinoth的所有建议。
Obviously I'm assuming a GNU userland, I suppose people running with the tools providing bare minimum POSIX compatibility will be able to adapt it.
显然我假设一个GNU用户空间,我想人们运行提供最低POSIX兼容性的工具将能够适应它。
#2
1
Yet another way is to use diff
which is more than capable in finding file differences in files in directories. For instance if you have d1
and d2
that contain your 200 and 210 files respectively (with the first 200 files being the same), you could use diff
and process substitution
to provide the names to remove to a while loop:
另一种方法是使用diff,它能够找到目录中文件的文件差异。例如,如果您的d1和d2分别包含200和210个文件(前200个文件相同),则可以使用diff和process substitute来提供要删除到while循环的名称:
( while read -r line; do printf "rm %s\n" ${line##*: }; done < <(diff -q d1 d2) )
Output (of d1 with 10 files, d2 with 12 files)
输出(d1有10个文件,d2有12个文件)
rm file11.txt
rm file12.txt
diff
will not fit all circumstances, but is does a great job finding directory differences and is quite flexible.
diff不适合所有情况,但是找到目录差异并且非常灵活。