如何在不比较文件内容的情况下列出彼此不存在的文件和文件夹?

问题描述 投票:0回答:2

我目前正在使用

diff -rq /dir1 /dir2
来检查彼此不存在的内容。但这比较了如果使用光盘和硬盘驱动器则需要很长时间的文件内容。我只想通过检查名称来列出差异。如何在 mac 中做到这一点?有什么工具可以做到这一点吗?有什么漂亮的自定义脚本吗?谢谢。

macos command-line diff
2个回答
0
投票

我可以用

find
comp
进行检查。

#!/bin/bash

if [ $# -ne 2 ]; then
    echo "Usage: $0 <folder1> <folder2>"
    exit 1
fi

folder1_path="$1"
folder2_path="$2"

# List all files in first folder
find "$folder1_path" -type f | sed "s#${folder1_path}/##" | sort > files_in_folder1.txt

# List all files in second folder
find "$folder2_path" -type f | sed "s#${folder2_path}/##" | sort > files_in_folder2.txt

echo 'Files in folder1 that are not in folder2:'
comm -23 files_in_folder1.txt files_in_folder2.txt

echo ''
echo 'Files in folder2 that are not in folder1:'
comm -23 files_in_folder2.txt files_in_folder1.txt

# Clean up
rm files_in_folder1.txt files_in_folder2.txt

0
投票

我只想通过检查名称来列出差异。如何在 mac 中做到这一点?

如果只有目录结构和文件名很重要……

您可以使用 list 目录内容 命令 (

ls
) 生成两个子 shell 中每个目录的递归结构,然后使用
diff
比较这些输出:

% diff <(cd a; ls -AFR1) <(cd b; ls -AFR1)

给定两个具有相同结构的目录

a
b

% ls -AFR1 a
hello.txt
sub/

a/sub:
hello.txt

运行上述命令会产生以下输出:

% diff <(cd a; ls -AFR1) <(cd b; ls -AFR1)

% echo $?
0

修改一个将显示所需的差异:

% rm b/sub/hello.txt

% diff <(cd a; ls -AFR1) <(cd b; ls -AFR1)
5d4
< hello.txt

% echo $?                                 
1

手册页中

ls
参数的描述:

-A      Include directory entries whose names begin with a dot (‘.’)
        except for . and ...  Automatically set for the super-user unless
        -I is specified.

-F      Display a slash (‘/’) immediately after each pathname that is a
        directory, an asterisk (‘*’) after each that is executable, an at
        sign (‘@’) after each symbolic link, an equals sign (‘=’) after
        each socket, a percent sign (‘%’) after each whiteout, and a
        vertical bar (‘|’) after each that is a FIFO.

-R      Recursively list subdirectories encountered.

-1      (The numeric digit “one”.) Force output to be one entry per line.
        This is the default when output is not to a terminal.  (-l)
        output, and don't materialize dataless directories when listing
        them.

但是,两个文件共享相同的名称并不意味着它们的内容没有不同,因此仅使用基于名称的方法作为快速早期步骤来检测目录根之间的整体更改,并且不排除缺少改变。

© www.soinside.com 2019 - 2024. All rights reserved.