我有一个大型目录树,这个嵌套目录重复(但不是全部):
如何仅使用此操作合并重复的目录:
我目前的代码:
#/bin/bash
for folder in $(find httpdocs -type d); do
n=$(echo $folder | tr "/" "\n" | wc -l)
nuniq=$(echo $folder | tr "/" "\n" | sort | uniq | wc -l)
[ $n -eq $nuniq ] || echo "Duplicated folder $folder"
done
但有问题,因为 data / home / es / home 是有效的文件夹,但检测为重复。
感谢。
答案 0 :(得分:3)
您可以使用uniq命令,如下所示;
#/bin/bash
for folder in $(find httpdocs -type d); do
nuniq=$(echo $folder | tr "/" "\n" | uniq -d | wc -l)
if [ "$nuniq" -gt "0" ]
then
echo "Duplicated folder $folder"
fi
done
man uniq;
-d, --repeated only print duplicate lines
您可以尝试以下脚本来复制和删除文件夹。我无法测试这个,所以在运行之前请备份你的httpdocs文件夹。
#/bin/bash
for folder in $(find httpdocs -type d); do
nuniq=$(echo $folder | tr "/" "\n" | uniq -d | wc -l)
if [ "$nuniq" -gt "0" ]
then
dest=$(echo $folder | tr '/' '\n' | awk '!a[$0]++' | tr '\n' '/')
mv -i $folder/* $dest
rmdir $folder
fi
done
例如;
user@host $ echo "data/home/es/home" | tr "/" "\n"
data
home
es
home
user@host $ echo "data/home/es/home" | tr "/" "\n" | uniq -d | wc -l
0
user@host $ echo "data/home/home" | tr "/" "\n"
data
home
home
user@host $ echo "data/home/home" | tr "/" "\n" | uniq -d
home
user@host $ echo "data/home/home" | tr "/" "\n" | uniq -d | wc -l
1