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
.
-path "/usr/local/connect/"
would match only on a file path that is exactly /usr/local/connect/
. That will never match because with find .
, all the paths will start with .
So you'd want:
find / -path '/usr/local/connect/*' -type d -prune -o \
-name '*.txt' -type f -mtime -1 -print
The -print
is also important. Without it, there would be an implicit -print
for files that match the whole expression (so both parts of the -o
).
Note that you can also use -xdev
to prevent crossing any file system boundary.
If you want to run it with find .
when the current directory is /usr/local
, that would have to be:
cd /usr/local &&
find . -path './connect/*' -type d -prune -o \
-name '*.txt' -type f -mtime -1 -print
Best Answer
I needed to put the -type f at the end of the search command, but before the printf. It became: