2

I am using the find command of bash, and I am trying to understand it since it is part of a code that I am using. In the code, the command in question is -

find -L $RAW_DIR -mindepth 2 -maxdepth 2 -name "*.bam" -o -name "*.sam" | wc -l

I have been trying to understand this command by searching its components. In esence, I thinking it is trying to find the number of files that end with .bam or .sam. I think -maxdepth 2 means to search for these files in this folder and its immediate subfolder.

What I do not understand is what mindepth -2 does in this case. I looked up mindepth, and the explanation given everywhere is -

"Do not apply any tests or actions at levels less than levels (a non-negative integer). '-mindepth 1' means process all files except the command line arguments."

To me this explanation is not very clear. Just like maxdepth -2means search for subfolders upto a depth of 2, what does mindepth -2 correspondingly mean, in simple language?

Also, if mindepth is just the opposite of maxdepth in terms of the direction (which would make intuitive sense), then how do I understand the fact that executing the command above on a folder that does have a .bam file leads to the output 0, whereas omitting the mindepth part of the command leads to the output 1 ?

2 Answers 2

10

Depth 0 is the command line arguments, 1 the files contained within them, 2 the files contained within depth 1, etc.

-mindepth N tells to process only files that are at depth >= N, similar to how -maxdepth M tells to process only files are at depth <= M. So if you want the files that are at depth 2, exactly, you need to use both.

Your command would match $RAW_DIR/foo/bam.bam, but not $RAW_DIR/bar.bam.

Try, e.g.

$ mkdir -p a/b/c/d
$ find ./a -maxdepth 2
./a
./a/b
./a/b/c
$ find ./a -mindepth 2
./a/b/c
./a/b/c/d
$ find ./a -maxdepth 2 -mindepth 2
./a/b/c

maxdepth with a negative argument doesn't mean anything:

$ find ./a -maxdepth -2
find: Expected a positive decimal integer argument to -maxdepth, but got ‘-2’
4
  • thanks a lot. Although I was sort of confused by the words deeper and lower, but I got it eventually
    – user1995
    Jul 8, 2018 at 22:39
  • @user1993, yeah, that didn't come out right. I suppose the opposite of "deep" is "shallow", but that doesn't really sound fitting here. Edited, hopefully for the better.
    – ilkkachu
    Jul 8, 2018 at 22:43
  • Depth implies the hierarchy of a file system. Why are "command line arguments" considered part of the file system?? Perhaps you could clarify that this means the root/starting point of the find i.e. the directory passed as an argument.
    – Marc
    Nov 10, 2021 at 7:24
  • @Marc, fine, "for the purposes of the -mindepth and -maxdepth options, the files named on the command line are considered to be at depth 0; files contained within them at depth 1; and in general, files contained within files at depth N are considered to be at depth N+1." better?
    – ilkkachu
    Nov 10, 2021 at 11:03
1

The -mindepth 2 and -maxdepth 2 parameters together limits the scope of find to the directories at depth two from $RAW_DIR.

The find command is identical to the bash shell command

printf '%s\n' "$RAW_DIR"/*/*.{sam,bam} | wc -l

This would count the number of .sam and .bam files in subdirectories of $RAW_DIR. It would still give the count of 1 for no files though, but just like the find command, it would count a file with a embedded newline in its name as two files.

For an absolutely correct find command, one that counts filenames containing embedded newlines as one file:

find . "$RAW_DIR" -type f -mindepth 2 -maxdepth 2 \
    '(' -name '*.bam' -o -name '*.sam' ')' -exec echo x ';' | wc -l

This would output an x on a line on its own for every found file matching the criteria, and the wc -l would count these x-es.

You must log in to answer this question.

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