-1

I gave two commands such as

find / -name "*.txt" -or -type f -print |wc -l

output: 1270

find / -name "*.txt" -or -type d -print |wc -l

output: 1305

why does it so? The output should be same, as I am using an OR operator.

but if 1st condition is true it will not go for the next one, right? so in that case both result will be same because both commands 1st condition is same and true.

2 Answers2

1

No, the output should NOT be the same.

-type f (FILES) is not the same as

-type d (DIRECTORIES)

so the count will be different. The OR-operator will not change that.

Your first command reports all files (.txt-file OR any file), your second command reports all text-files and all directories. Naturally those counts will be different.

What you probably wanted is

$ find / -type f -name '*.txt' | wc -l

Note: this will fail if your filenames have linebreaks in them.

If you have linebreaks in your filenames try...

$ find . -type f -name "*.txt" -printf '.' | wc -c

to print just the first character of each filename and count the characters instead of the lines. (See https://stackoverflow.com/a/15663760 )

markgraf
  • 2,860
  • but if 1st condition is true it will not go for the next one, right? so in that case both result will be same because both commands 1st condition is same and true. – Subir Makur Dec 17 '19 at 11:45
  • Yes, it will go for the next one. You will have matches for ALL your conditions. – markgraf Dec 17 '19 at 12:34
0

Because you use de OR operator you are adding to the result of the first expression the result of the second expression:

  • -type f : matches any file
  • -type d : matches any directory

the expressions are not equivalent so that the result are diferent.