27

I am trying to understand how to use find -maxdepth 0 option.

I have the below directory structure.

--> file1
--> parent
          --> child1
                   --> file1
                   --> file2
          --> child2
                   --> file1
                   --> file2
          --> file1

Now, I execute my find command as below.

find ./parent -maxdepth 0 -name "file1"
find ./ -maxdepth 0 -name "file1"
find . -maxdepth 0 -name "file1"

With none of the above find commands, file1 gets returned.

From man page of find, I see the below information.

-maxdepth 0 means only apply the tests and actions to the command line arguments.

I searched for some examples with -maxdepth 0 option and couldn't find any proper example.

My find version is,

find --version
find (GNU findutils) 4.4.2

Can someone please provide me some pointers on which cases -maxdepth 0 option would be useful?

EDIT

When I execute the below command, I get the file1 getting listed twice. Is this intended to work this way?

find . file1 -maxdepth 1 -name "file1"
./file1
file1
34

Let us suppose that we have file1 in the current directory.  Then:

$ find . -maxdepth 0 -name "file1"
$ find . file1 -maxdepth 0 -name "file1"
file1

Now, let's look at what the documentation states:

-maxdepth 0 means only apply the tests and actions to the command line arguments.

In my first example above, only the directory . is listed on the command line.  Since . does not have the name file1, nothing is listed in the output.  In my second example above, both . and file1 are listed on the command line, and, because file1 matches -name "file1", it was returned in the output.

In other words, -maxdepth 0 means do not search directories or subdirectories. Instead, only look for a matching file among those explicitly listed on the command line.

In your examples, only directories were listed on the command line and none of them were named file1. Hence, no output.

In general, many files and directories can be named on the command line.  For example, here we try a find command with nine files and directories on the command line:

$ ls
d1  file1  file10  file2  file3  file4  file5  file6  file7
$ find d1 file1 file10 file2 file3 file4 file5 file6 file7 -maxdepth 0 -name "file1"
file1

Overlapping paths

Consider:

$ find . file1 -maxdepth 0 -iname file1
file1
$ find . file1 file1 -maxdepth 0 -iname file1
file1
file1
$ find . file1 file1 -maxdepth 1 -iname file1
./file1
file1
file1

find will follow each path specified on the command line and look for matches even if the paths lead to the same file, as in . file, or even if the paths are exact duplicates, as in file1 file1.

7
  • thanks. But how could I pass some file names on command line?
    – Ramesh
    Oct 16 '14 at 3:48
  • Good point. See my updated answer. You can put as many as you want before the first test. For example: find * -maxdepth 0 -name "file1". The * will list every file in the current directory on the command line.
    – John1024
    Oct 16 '14 at 3:51
  • 1
    @Ramesh Answer updated again with, I hope, better explanation and examples.
    – John1024
    Oct 16 '14 at 3:59
  • thanks again. I updated my question with more details from your latest update.
    – Ramesh
    Oct 16 '14 at 4:02
  • 1
    @tedly: Please look at context when you are editing. The above post said "For example, here we try a find command with 11 files and directories on the command line", and you changed the example command, but not the "11". May 4 '21 at 18:32
2

If you want to non-recursively find files (not directories) inside a directory use:

find . -maxdepth 1 -type f -name "file1"
# ./file1

-maxdepth 0 will not search. It will only try to match among the file/directory names that you have provided as arguments in find.

E.g. in the above statement, using a maxdepth value of 0, would try to match file1 to . which does not match.

However, passing * instead of . in combination with -maxdepth 0 would make bash replace * with list of files in current dir which would return a match.

0
Usage:

John1024 gave good examples, how to use -maxdepth 0, so I only add information of when to use it:

Well, what's the reason for -maxdepth 0? If you know the name, you may just issue the command

ls file-* 

Well, it might not be the name, what you're searching for.

Example:

touch file-{1..3}
echo "1" > file-1
touch -d @0 file-2

find * -maxdepth 0 -name "file-*" \( -size 1k -o -mtime +10000 \) -ls 
   669266     12 -rw-rw-r--   1 stefan   stefan          2 Mai  4 20:57 file-1
   669270      8 -rw-rw-r--   1 stefan   stefan          0 Jan  1  1970 file-2

I create 3 files with the name pattern file-*. Then I add some content to no. 1, reset the date for no. 2 and leave no. 3 as is.

Now I search for files of 1k (no.1) or age > roughly 30 years in maybe 1000s of files. For the example, just among 3 files.

But find * searches every file in the current directory, hence the somewhat alien man page jargon, of "starting point" instead of just speaking about directories. I guess the powerful filter features of find had led to the decision, to search for files, of which you know where they're located (the $PWD), but you can't easily filter for other attributes with standard shell tools.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.