AND运算符如何在FIND命令中工作?

时间:2022-05-05 15:05:52

My last question expanded, so let's analyse things separately.

我的最后一个问题扩大了,所以让我们分别分析一下。

AND-operater, Intersection?

I will give an example:

我举个例子:

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

.zcompdump
.zshrc
.zshrc_copy
.zshrc_somequy
.bashrc
.emacs

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

.bashrc
.emacs
.gdbinit
.git
.profile

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

当我将输出保存到单独的列表时,我无法理解为什么命令不采用常见的事情:

find -f all_files -and -f right_files .

I want only:

我只想要:

.bashrc
.emacs

3 个解决方案

#1


To slightly expand on Dave's answer:

略微扩展戴夫的答案:

You appear to want the intersection of two find commands. Find doesn't do set manipulation. Find traverses down (or up depending on how you look at it) a set of paths and applies an expression to each item it encounters. The default action it takes is to print the path of the items it finds that evaluate to true against the expressions. (I believe some old versions of find required you to explicitly add a -print expression.) It doesn't collate the results. For intersection analysis you can use tools like diff, sdiff, comm.

您似乎想要两个查找命令的交集。查找不做集合操作。查找遍历(或向上取决于您如何看待它)一组路径并将表达式应用于它遇到的每个项目。它所采取的默认操作是打印它找到的项目的路径,该路径根据表达式求值为true。 (我相信一些旧版本的find需要你明确添加一个-print表达式。)它不会整理结果。对于交叉点分析,您可以使用diff,sdiff,comm等工具。

I assume you are trying to find the items with the same name in two separate directories, not in sub directories.

我假设你试图在两个单独的目录中找到具有相同名称的项目,而不是在子目录中。

Assuming bash you can do something like

假设bash你可以做类似的事情

comm -12 <(find .  -maxdepth 1 | sort) <(cd ~/bin/FilesDvorak/; find . -maxdepth 1 | sort)

I believe the -and in find commands is almost always superfluous. Ex.

我相信-and in find命令几乎总是多余的。防爆。

find . -type f ! -type d 

Is the same as

是相同的

find . -type f -and ! -type d

Also the -f flag is an option to add to the paths to traverse. I don't believe it is an expression. Please 'man find' for clarification.

此外,-f标志是一个添加到遍历路径的选项。我不相信这是一种表达。请'找到'澄清。

#2


-and only works on tests. If you want to find the common elements of the two lists you should do something like

- 仅适用于测试。如果你想找到两个列表的共同元素,你应该做类似的事情

sort all_files > all_files.sorted
sort right_files > right_files.sorted
comm -12 all_files right_files > common_files

#3


The AND -option in the man

人中的AND -option

The -and operator is the logical AND operator. As it is implied by the juxtaposition of two expressions it does not have to be specified. The expression evaluates to true if both expressions are true. The second expression is not evaluated if the first expression is false.

-and运算符是逻辑AND运算符。正如两个表达式的并置所暗示的那样,它不必被指定。如果两个表达式都为真,则表达式的计算结果为true。如果第一个表达式为false,则不计算第二个表达式。

This part of the manual is rather cryptic for me. However, let's analyse it.

本手册的这一部分对我来说相当神秘。但是,让我们分析一下。

1st sentence: like AND operator in Math. Two two 1s only!

第一句:与数学中的AND运算符一样。只有两个1 1!

2nd sentente: This suggests me that you do not need to use the command, as you can see in Dave's answer. Great! - - It seems to juxpose the 1st expression with -AND to and Expression without -AND. Please, clarify this Greek to me.

第2个判刑:这表明你不需要使用命令,正如你在Dave的回答中所看到的那样。大! - - 似乎将第一个表达式与-AND并置于和表达式而不是-AND。请把这个希腊语告诉我。

3rd sentence: repeation: It says the same in the 1st sentence by the word "logical".

第3句:重复:在第1句中用“逻辑”一词表示相同。

4th sentence: repeation: It says that 01 implies false

第四句:重复:它说01意味着错误

The same in English:

同样的英文:

Let p and q be two expressions with the following truth valuas

设p和q为具有以下真值的两个表达式

p q pANDq
1 1 1
1 0 0
0 1 0
0 0 0

where 1 implies true, while 0 false.

其中1表示为true,而0表示为false。

Note the manuals contain often superfluous description and sometimes mistakes.

请注意,手册通常包含多余的描述,有时还包含错误。

#1


To slightly expand on Dave's answer:

略微扩展戴夫的答案:

You appear to want the intersection of two find commands. Find doesn't do set manipulation. Find traverses down (or up depending on how you look at it) a set of paths and applies an expression to each item it encounters. The default action it takes is to print the path of the items it finds that evaluate to true against the expressions. (I believe some old versions of find required you to explicitly add a -print expression.) It doesn't collate the results. For intersection analysis you can use tools like diff, sdiff, comm.

您似乎想要两个查找命令的交集。查找不做集合操作。查找遍历(或向上取决于您如何看待它)一组路径并将表达式应用于它遇到的每个项目。它所采取的默认操作是打印它找到的项目的路径,该路径根据表达式求值为true。 (我相信一些旧版本的find需要你明确添加一个-print表达式。)它不会整理结果。对于交叉点分析,您可以使用diff,sdiff,comm等工具。

I assume you are trying to find the items with the same name in two separate directories, not in sub directories.

我假设你试图在两个单独的目录中找到具有相同名称的项目,而不是在子目录中。

Assuming bash you can do something like

假设bash你可以做类似的事情

comm -12 <(find .  -maxdepth 1 | sort) <(cd ~/bin/FilesDvorak/; find . -maxdepth 1 | sort)

I believe the -and in find commands is almost always superfluous. Ex.

我相信-and in find命令几乎总是多余的。防爆。

find . -type f ! -type d 

Is the same as

是相同的

find . -type f -and ! -type d

Also the -f flag is an option to add to the paths to traverse. I don't believe it is an expression. Please 'man find' for clarification.

此外,-f标志是一个添加到遍历路径的选项。我不相信这是一种表达。请'找到'澄清。

#2


-and only works on tests. If you want to find the common elements of the two lists you should do something like

- 仅适用于测试。如果你想找到两个列表的共同元素,你应该做类似的事情

sort all_files > all_files.sorted
sort right_files > right_files.sorted
comm -12 all_files right_files > common_files

#3


The AND -option in the man

人中的AND -option

The -and operator is the logical AND operator. As it is implied by the juxtaposition of two expressions it does not have to be specified. The expression evaluates to true if both expressions are true. The second expression is not evaluated if the first expression is false.

-and运算符是逻辑AND运算符。正如两个表达式的并置所暗示的那样,它不必被指定。如果两个表达式都为真,则表达式的计算结果为true。如果第一个表达式为false,则不计算第二个表达式。

This part of the manual is rather cryptic for me. However, let's analyse it.

本手册的这一部分对我来说相当神秘。但是,让我们分析一下。

1st sentence: like AND operator in Math. Two two 1s only!

第一句:与数学中的AND运算符一样。只有两个1 1!

2nd sentente: This suggests me that you do not need to use the command, as you can see in Dave's answer. Great! - - It seems to juxpose the 1st expression with -AND to and Expression without -AND. Please, clarify this Greek to me.

第2个判刑:这表明你不需要使用命令,正如你在Dave的回答中所看到的那样。大! - - 似乎将第一个表达式与-AND并置于和表达式而不是-AND。请把这个希腊语告诉我。

3rd sentence: repeation: It says the same in the 1st sentence by the word "logical".

第3句:重复:在第1句中用“逻辑”一词表示相同。

4th sentence: repeation: It says that 01 implies false

第四句:重复:它说01意味着错误

The same in English:

同样的英文:

Let p and q be two expressions with the following truth valuas

设p和q为具有以下真值的两个表达式

p q pANDq
1 1 1
1 0 0
0 1 0
0 0 0

where 1 implies true, while 0 false.

其中1表示为true,而0表示为false。

Note the manuals contain often superfluous description and sometimes mistakes.

请注意,手册通常包含多余的描述,有时还包含错误。