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
Best Answer
Let us suppose that we have
file1
in the current directory. Then:Now, let's look at what the documentation states:
In my first example above, only the directory
.
is listed on the command line. Since.
does not have the namefile1
, nothing is listed in the output. In my second example above, both.
andfile1
are listed on the command line, and, becausefile1
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:Overlapping paths
Consider:
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 infile1 file1
.