How does the AND-operator work in the FIND-command

2019-09-05 14:26发布

问题:

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

回答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.

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

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.

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.



回答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

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.

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!

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.

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

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

The same in English:

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

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

where 1 implies true, while 0 false.

Note the manuals contain often superfluous description and sometimes mistakes.



标签: unix shell find