Skip to content
The Chat Archive
gpt-4-0125-preview· OpenAI WildChat-4.8M (AI2)

我需要一个 shell脚本,可以比较两个目录下的所有文件是否相同,并且把不同的文件的名字在屏幕上显示出来,这两个目录结构是一样的,而且都有多层子目录

  1. User

    我需要一个 shell脚本,可以比较两个目录下的所有文件是否相同,并且把不同的文件的名字在屏幕上显示出来,这两个目录结构是一样的,而且都有多层子目录

  2. Assistant

    你可以使用以下 shell 脚本来完成这项任务。该脚本使用 diff 命令比较两个目录结构(包含多层子目录)下的所有文件,并展示不同之处。请按照以下步骤创建和使用脚本:

    1. 打开终端。
    2. 使用文本编辑器(例如vim或nano)创建一个新文件叫 compare_dirs.sh:
    nano compare_dirs.sh
    
    1. 拷贝并粘贴以下脚本内容到 compare_dirs.sh 文件:
    #!/bin/bash
    
    # 检查参数数量
    if [ "$#" -ne 2 ]; then
      echo "Usage: $0 directory1 directory2"
      exit 1
    fi
    
    # 定义目录变量
    dir1=$1
    dir2=$2
    
    # 使用diff命令递归比较两个目录
    diff --brief -r "$dir1" "$dir2" | while read line; do
      if [[ $line == *"Only in"* ]]; then
        echo $line
      elif [[ $line == *"differ"* ]]; then
        echo $line
      fi
    done
    
    1. 给脚本执行权限:
    chmod +x compare_dirs.sh
    
    1. 运行脚本,用实际的目录路径替换 directory_path_1 和 directory_path_2:
    ./compare_dirs.sh directory_path_1 directory_path_2
    

    此脚本工作如下:

    • 检查是否提供了两个目录作为参数($1 和 $2)。
    • 使用 diff --brief -r 命令递归比较这两个目录。
    • 命令输出的信息经过处理,显示那些仅存在于其中一个目录或内容不同的文件名。

    注意: 确保替换 directory_path_1 和 directory_path_2 为你要比较的两个目录路径。

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.