1

A) This will output all the *.JPG and *.flac files recursively:

$ find . -type f -name "*.JPG" -o -name "*.flac"

Output includes: 1 JPG document and 254 flac documents

B) This will output only *.flac files recursively:

$ find . -type f -name "*.flac"

Output includes: 218 flac documents

I would assume all *.flac files listed with those two commands would match the exact same amount of files for each of those. The only difference would be that the first command would include *.JPG on top of all *.flac files, right? Not exactly. In case you are an iTunes user, you may be familiar with @eadir directories. Surprisingly, the first command will include even *.flac files within those @eadir directories (36 *.flac files), but the second command will not.

Why do those two commands return different results for the same file type?

1 Answer 1

4

find . -type f -name "*.JPG" -o -name "*.flac" looks for files that match at least one (-o) of the following conditions:

  • regular files (-type f) whose name ends with .JPG (-name "*.JPG");
  • files (of any type) whose name ends with .flac (-name "*.flac").

find . -type f -name "*.JPG" -o -name "*.flac" is equivalent to find . \( -type f -name "*.JPG" \) -o \( -name "*.flac" \). In more formal terms, juxtaposition (which means “and”) binds more tightly than the -o operator (which means “or”).

The @eadir directories apparently contain non-regular files, perhaps symbolic links. The .flac files that the first command returns but not the second one are not regular files. You can see what they are with the following command:

find . ! -type f -name "*.flac" -exec ls -ld {} +
5
  • Thanks for your answer. Well, "ea" stands for "extended attributes". Those @eadir -directories would contain files (usually thumbnails) corresponding to those in the "normal" directory tree. I'm still puzzled what's the fundamental difference whilst using OR-operator in find. Imo both find commands should return or not return the @eadir content. I'm sorry if I missed something obvious, but I'm still under strong impression that this may be non-consistent and it confuses a lot.
    – madjoe
    Commented Jun 15, 2020 at 19:56
  • @madjoe It's not related to whether they're under a directory called @eadir. It's about the file type. For example, if you create a symbolic link called foo.flac outside of @eadir, the first command will list it, but not the second one. Commented Jun 15, 2020 at 20:01
  • I understand it's related to the file type. I mentioned @eadir only as a reference. I just don't see a good reason why the first command will list it and the second one will not. It seems like a flaw in find, don't you think?
    – madjoe
    Commented Jun 15, 2020 at 20:08
  • @madjoe No, it's a flaw in your understanding of find. You told find to list all *.flac files regardless of their type, and *.JPG files but only if they're regular files. So that's what it does. If you want the constraint on the file type to apply to both constraints on the file name, you need parentheses. Remember that computers do what you tell them to do, they don't care about what you meant to do. Commented Jun 15, 2020 at 20:16
  • I read your answer more carefully and I've noticed "files (of any type)" part. I understand now. Thanks so much for your patience and an in-depth explanation.
    – madjoe
    Commented Jun 15, 2020 at 20:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .