bash – 在目录层次结构中查找并删除重复的文件名

#!/bin/sh
LASTBASE=""  
find $1 -type f -print | rev | sort | rev | while read FILE
do
    BASE=$(basename "$FILE")
    if [ "$BASE" = "$LASTBASE" ]; then
        rm "$FILE"
    LASTBASE="$BASE"
done
如果将find的输出传输到while循环中,则可以逐行处理它们:
find nnn/ -type f -print | rev | sort | rev | while read FILE; do
    ...
done

编辑:因此,如果文件名包含双(连续)空格,则此方法会中断,因为read实际上根据$IFS将行拆分,然后在存储最后一个变量时再次连接它.要解决此问题,您可以暂时更改$IFS以禁用拆分:

OIFS="$IFS"
IFS=""
find | while read...
IFS="$OIFS"

编辑:test(与[相同]没有==运算符,你只需要=.

相关文章

用的openwrt路由器,家里宽带申请了动态公网ip,为了方便把2...
#!/bin/bashcommand1&command2&wait从Shell脚本并行...
1.先查出MAMP下面集成的PHP版本cd/Applications/MAMP/bin/ph...
1、先输入locale-a,查看一下现在已安装的语言2、若不存在如...
BashPerlTclsyntaxdiff1.进制数表示Languagebinaryoctalhexa...
正常安装了k8s后,使用kubect工具后接的命令不能直接tab补全...