使用FIND命令进行联合,交叉和排除?

时间:2022-07-25 15:05:48

I need to manage lists with find-command. Suppose the lists have random names in non-distinct lists (ie their intersection is not empty set). How can I do:

我需要使用find-command管理列表。假设列表在非不同列表中具有随机名称(即它们的交集不是空集)。我能怎么做:

A \ B

A \ B.

find files in the list A except the files in the list B

查找列表A中的文件,但列表B中的文件除外

A intersection B

一个十字路口B.

find files common to the lists A and B

查找列表A和B共有的文件

Please, consult here.

请在这里咨询。

A union B

工会B

find all files in the two lists

找到两个列表中的所有文件

EXAMPLES

$ find . | awk -F"/" '{ print $2 }'

.zcompdump
.zshrc
.bashrc
.emacs

$ find ~/bin/FilesDvorak/.* -maxdepth 0 | awk -F"/" '{ print $6 }'

.bashrc
.emacs
.gdbinit
.git

I want:

A \ B:

A \ B:

.zcompdump
.zshrc

A Intersection B:

交叉口B:

.bashrc
.emacs

A Union B:

联盟B:

.zcompdump
.zshrc
.bashrc
.emacs
.bashrc
.emacs
.gdbinit
.git

A try for the Intersection

尝试交叉路口

When I save the outputs to separate lists, I cannot understand why the command does not take the common things, ie the above intersection:

当我将输出保存到单独的列表时,我无法理解为什么命令不采用常见的东西,即上面的交集:

find -f all_files -and -f right_files .

Questions emerged from the question:

问题出现了问题:

  1. find ~/bin/FilesDvorak/.* -maxdepth 0 -and ~/.PAST_RC_files/.*

    找〜/ bin / FilesDvorak /.* -maxdepth 0 -and~ / .PAST_RC_files /.*

  2. Please, consult for recursive find Click here!

    请咨询递归查找点击这里!

  3. find ~/bin/FilesDvorak/.* -maxdepth 0 -and list

    找到〜/ bin / FilesDvorak /.* -maxdepth 0和列表

2 个解决方案

#1


Seriously, this is what comm(1) is for. I don't think the man page could be much clearer: http://linux.die.net/man/1/comm

说真的,这就是comm(1)的用途。我不认为手册页可能更清晰:http://linux.die.net/man/1/comm

#2


There are several tools that can help you find the intersection in file lists. 'find' isn't one of them. Find is for finding files that match a certain criteria on the filesystem.

有几种工具可以帮助您找到文件列表中的交集。 '发现'不是其中之一。 Find用于查找与文件系统上的特定条件匹配的文件。

Here are some ways of finding your answer.

以下是一些找到答案的方法。

To generate your two file lists

生成两个文件列表

find . -maxdepth 1 | sort > a
(cd ~/bin/FilesDvorak/; find . -maxdepth 1 | sort > b)

Now you have two files a and b that contain directory entries without recursing into sub directories. (To remove the leading ./ you can add a "sed -e 's/^.///'" or your first awk command between the find an sort)

现在,您有两个包含目录条目的文件a和b,而不会递归到子目录中。 (要删除前导./你可以在查找排序之间添加“sed -e's /^.///”或你的第一个awk命令)

To find the Union

找到联盟

cat a b | sort -u 

To find the A\B

找到A \ B

comm -23 a b 

To find the intersection

找到交集

comm -12 a b

'man comm' and 'man find' for more information.

'man comm'和'man find'获取更多信息。

#1


Seriously, this is what comm(1) is for. I don't think the man page could be much clearer: http://linux.die.net/man/1/comm

说真的,这就是comm(1)的用途。我不认为手册页可能更清晰:http://linux.die.net/man/1/comm

#2


There are several tools that can help you find the intersection in file lists. 'find' isn't one of them. Find is for finding files that match a certain criteria on the filesystem.

有几种工具可以帮助您找到文件列表中的交集。 '发现'不是其中之一。 Find用于查找与文件系统上的特定条件匹配的文件。

Here are some ways of finding your answer.

以下是一些找到答案的方法。

To generate your two file lists

生成两个文件列表

find . -maxdepth 1 | sort > a
(cd ~/bin/FilesDvorak/; find . -maxdepth 1 | sort > b)

Now you have two files a and b that contain directory entries without recursing into sub directories. (To remove the leading ./ you can add a "sed -e 's/^.///'" or your first awk command between the find an sort)

现在,您有两个包含目录条目的文件a和b,而不会递归到子目录中。 (要删除前导./你可以在查找排序之间添加“sed -e's /^.///”或你的第一个awk命令)

To find the Union

找到联盟

cat a b | sort -u 

To find the A\B

找到A \ B

comm -23 a b 

To find the intersection

找到交集

comm -12 a b

'man comm' and 'man find' for more information.

'man comm'和'man find'获取更多信息。