1

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"

asked Dec 7, 2016 at 17:00

2 Answers 2

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
* ***********
answered Dec 7, 2016 at 17:26
6
  • I've made a typo in question 2: should be find . -path "*/abc/*.txt" rather then find . -path "\*/abc/*.txt". So can You be so nice and answer such corrected question? Commented Dec 7, 2016 at 18:53
  • @Mulligan Edited the answer. Does this answer your question? Commented Dec 7, 2016 at 19:24
  • Yes, thx. for help:-) Commented Dec 7, 2016 at 19:39
  • So Am i right that -path pattern is applied to the result of what find .produces and that's why my first attempt fails? Commented Dec 7, 2016 at 19:43
  • 1
    You are correct Commented Dec 7, 2016 at 22:49
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/.

answered Dec 7, 2016 at 17:36
2
  • I've made a typo in question 2: should be find . -path "*/abc/*.txt" rather then find . -path "\*/abc/*.txt". So can You be so nice and answer such corrected question? Commented Dec 7, 2016 at 18:53
  • 1
    The difference between "./abc/*.txt" and "*/abc/*.txt" is that the first will match text files in the top-level ./abc directory only, while the second will match not only here, but also in ./def/abc or in any other subdirectory further down the hierarchy. Commented Dec 7, 2016 at 19:12

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.