我正在使用 chroot 为我自己的操作系统构建软件包。我在存档文件中有 chroot,在构建过程开始时,它会下载两次并提取。我需要一个命令,该命令从构建包的 chroot 文件夹中删除文件,这些文件与第二个 chroot 文件夹中的文件相同(例如通过其散列)。我尝试了 Unix StackExchange 的几个命令,但没有一个起作用。
编辑:这必须是全自动的
这是通过比较所有文件的哈希值的方法。
感谢Kusalananda建议使用join
它简化了任务。您可以使用以下命令。请注意,如果任何文件的名称中有空格,这将不起作用。
# DIR1 is the main directory
# DIR2 is from where files will get deleted. Change the values accordingly
DIR1="$PWD"
DIR2="$HOME"
find $DIR1 -type f | xargs md5sum 2>/dev/null | sort > /tmp/m1
find $DIR2 -type f | xargs md5sum 2>/dev/null | sort > /tmp/m2
join /tmp/m1 /tmp/m2 > /tmp/m3
cat /tmp/m3 | cut -d ' ' -f3 | xargs rm -f
# To delete empty directories
find $DIR2 -type d -empty -delete
这里我们简单地重复计算两个目录中所有文件的哈希值,如果它们相同则删除该文件。
# DIR1 is the main directory
# DIR2 is from where files will get deleted.
DIR1="$PWD"
DIR2="$HOME"
# Take a file from $DIR1 and check for it in $DIR2
for i in $DIR1/*; do
HASH=$(md5sum $i 2>/dev/null | cut -d ' ' -f1 )
if [ "$HASH" ]; then
for j in $DIR2/*; do
HASH2=$(md5sum $j | cut -d ' ' -f1)
if [ "$HASH" = "$HASH2" ]; then
# Delete files from $DIR2
rm "$j"
fi
done
fi
done
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句