Having the file structure like that:
/root/abc/1.txt
/root/abc/2.txt
and sitting in /root directory.
1.) Why doesn't the command:
find . -path "abc/*.txt"
returns no results while
find . -path "./abc/*.txt"
and
find . -path "*/abc/*.txt"
do works?
2.) What's the difference between find . -path "./abc/*.txt"
and find . -path "*/abc/*.txt"
2 Answers 2
Question 1:
The -path pattern parameter matches the entire filename.
$ find
.
./abc
./abc/1.txt
./abc/2.txt
The pattern abc/*.txt does not match without the leading ./.
Question 2:
The wildcard * can match any string of any length (including / and . characters). The parameter
-path "*/abc/*.txt" has two wildcards, so it can match any of the following:
./abc/a.txt
* *
./abc/ANYTHING.txt
* ********
def/ghi/abc/SOMETHING.txt
******* *********
./abc/def_dsa/faf.txt
* ***********
-
I've made a typo in question 2: should be
find . -path "*/abc/*.txt"rather thenfind . -path "\*/abc/*.txt". So can You be so nice and answer such corrected question?Mulligan– Mulligan2016年12月07日 18:53:17 +00:00Commented Dec 7, 2016 at 18:53 -
@Mulligan Edited the answer. Does this answer your question?Steven– Steven2016年12月07日 19:24:30 +00:00Commented Dec 7, 2016 at 19:24
-
Yes, thx. for help:-)Mulligan– Mulligan2016年12月07日 19:39:02 +00:00Commented Dec 7, 2016 at 19:39
-
So Am i right that
-pathpattern is applied to the result of whatfind .produces and that's why my first attempt fails?Mulligan– Mulligan2016年12月07日 19:43:28 +00:00Commented Dec 7, 2016 at 19:43 -
1
The following sentence is from the -path option in the find manual:
Note that the pattern match test applies to the whole file name, starting from one of the start points named on the command line.
This explains why your alternative search patterns ("./abc/*.txt" and "*/abc/*.txt") work. Your original search pattern ("abc/*.txt") will work only if the search root is abc:
find abc/ -path "abc/*.txt"
As for the difference between "./abc/*.txt" and "*/abc/*.txt", neither will try to expand the string before calling find: the first will find files only in the top-level directory ./abc/, but the second will find files in any subdirectory abc, such as ./def/abc/, ./ghi/def/abc/, etc, as well as ./abc/.
-
I've made a typo in question 2: should be
find . -path "*/abc/*.txt"rather thenfind . -path "\*/abc/*.txt". So can You be so nice and answer such corrected question?Mulligan– Mulligan2016年12月07日 18:53:53 +00:00Commented Dec 7, 2016 at 18:53 -
1The difference between
"./abc/*.txt"and"*/abc/*.txt"is that the first will match text files in the top-level./abcdirectory only, while the second will match not only here, but also in./def/abcor in any other subdirectory further down the hierarchy.AFH– AFH2016年12月07日 19:12:21 +00:00Commented Dec 7, 2016 at 19:12