如果两个文件夹中都存在扩展名为 *.c 的文件,则 Bash 脚本会删除该文件

如果两个文件夹中都存在扩展名为 *.c 的文件,则 Bash 脚本会删除该文件

编写一个 shell 脚本,如果一个目录中的所有 C 源文件位于另一个目录的结构中,则删除它们。命令行上的第一个参数是 C 源文件所在的目录,第二个参数是搜索开始的目录。


#!/bin/bash
if [ $# -ne 2 ]
    then
        echo "for execution complete the line of command: $0 dir1 dir2"
        exit 1
fi
if [ ! d ~/$1 ]
        then
        echo "write a directory in order to do the search"
        exit 1
fi
if [ ! d ~/$2 ]
        then
        echo "write a directory in order to do the search"
        exit 1
fi
find $2 -type f -name*.c > listafis.txt
for f in listafis.txt
    do
    if grep f $1
    then rm f
    fi
done

这是我尝试过的,但没有效果

答案1

比较两个目录中的文件,如果两个目录中都存在该文件,则删除其中一个。

$ comm -z -12 \
  <(find folder1/ -type f -name '*.c' -printf %P\\0 | sort -z) \
  <(find folder2/ -type f -name '*.c' -printf %P\\0 | sort -z) | xargs -0 -I {} rm -i folder2/{}

相关内容