3

I have a some tree.

.
├── 1
│   └── a
│       └── script.a.sh
├── 2
│   └── a
│       └── script.b.sh
...
└── a
    └── script.sh

And I need to find script.*.sh. I execute ./a/script.sh:

#!/bin/bash
#

path="/tmp/test"

readarray files < <(find ${path} -path  "*/a/script.*.sh")

if [ ${#files[@]} -ne 0 ]
then
  for file in "${files[@]}"
  do
    echo ${file}
  done
fi

Output:

/tmp/test/1/a/script.a.sh
/tmp/test/2/a/script.b.sh

It's worked script!

But if I change find command to find ${path} -path "*/a" -name "script.*.sh" the script does output nothing.

My logic find all files with pattern /tmp/test/*/a/script.*.sh what I say with find ${path} -path "*/a" -name "script.*.sh".

My questions that remain at the moment:

  1. What does find ${path} -path "*/a" -name "script.*.sh" command do in real?
  2. There is debug mode in the find? I mean how does it understand what I say to it (example: path(s) where it will search, without find . -name "*te.st*", just ./*/*te.st*).
  3. Can -name, -path live on the same command line (need cases, examples, explanation) or they mutually exclusive? Or can we say -path extended version of -name?

Guys, yes I can use man and google. In these source I do not find full answers at all my questions either.

5
  • @JaromandaX It works! Thanks a lot for variation, but unfortunately the comment answer no one questions. It solve the problem but my questions do not find solve this problem. It's here above. But thanks again.
    – xUr
    Jul 11, 2023 at 3:26
  • @JaromandaX Unfortunately, you are wrong again, I will not explain why either. Please, read all questions carefully again.
    – xUr
    Jul 11, 2023 at 21:57
  • It works yet, I'm wrong? .... AGAIN? Sorry, what planet are you from? Jul 11, 2023 at 23:02
  • @JaromandaX :) Ok, It seems I have to give you more full explanation why your comment don't suit me. Understand, there are people who need everything to work. They aren't interesting how it work. And there are people who ask the question why it works and why it doesn't works. They are searching more global solve. So that there are no questions in the future. If simple this question about why, and you comment here is worked variant.
    – xUr
    Jul 12, 2023 at 0:00
  • On other words, I was not interesting worked variant, I have had it. I was interesting why it does not work. That is why I said: ...but unfortunately the comment answer no one questions... and ...Please, read all questions carefully again.... Unfortunately I will not be able to give you more understandable explanation, if you will not understand this explanation. Sorry. p.s. I'm from Earth and you? :)
    – xUr
    Jul 12, 2023 at 0:00

2 Answers 2

4

The -path argument in the GNU version of find specifies a pattern which is applied to the full path of the object being visited, including the starting directory.

The pattern is a pattern, but does not treat slashes as special. For instance the path a/b/c/d matches the pattern a*d.

It is similar to the pattern matching in the shell case statement:

$ case a/b/c/d in a*d ) echo match;; esac
match

rather than the pattern matching in pathname expansion.

$ echo a*d # will not match an a/b/c/d file accessible in this directory!

This for instance:

find /etc -path '*.conf'

will match and print /etc/foobar/foobar.conf. When it encounters that path, that path will match the pattern.

Thus:

  1. Q: What will find ${path} -path "*/a" -name "script.*.sh" do?

    A: It will not find anything. Because -path matches paths ending in /a, whereas script.*.sh matches directory entries that cannot possibly be a. The -path and -name predicates are joined by an implicit logical AND and so the result is empty.

  2. The starting places are optional in find; you can use find -name '*.txt' without find . -name '*.txt'. The shell syntax ./*/*te.st*, if used as an unquoted argument to find will be expanded by your shell using filename expansion. or possibly stay as-is, if it doesn't match anything.

  3. Q: Can -path and -name live on the same command line.

    A: Of course, but if they are combined with "and", and conflict, the results may be empty. find -path X -name Y means find -path X -a -name Y -a -print: if the visited path does not match X, and the directory entry being visited matches Y, the path will be printed. If no path which matches X ends in a name that matches Y, then the -print predicate (implicit or explicit) cannot be reached: nothing can be printed.

1

But if I change find command to find ${path} -path "*/a" -name "script.*.sh" the script does output nothing.

Indeed, it must not. A file matching the predicate -path "*/a" cannot also match the predicate -name "script.*.sh".

My logic find all files with pattern /tmp/test/*/a/script.*.sh what I say with find ${path} -path "*/a" -name "script.*.sh".

You seem to be assuming that the value of $path is /tmp/test. In that case, it's unclear to me why you are making this so complicated. Since you can exactly describe the paths you want via a glob, I would be inclined to leave find entirely out of the picture:

files=("${path}"/*/a/script.*.sh)

But if you want to do it with find, then I don't see the point of combining -name and -path for this case. As you already observed, your original find command does the job. And it's simpler and clearer.

Alternatively, you could move the partial-path matching to the starting-point list:

find ${path}/*/a -maxdepth 1 -name  "*script.*.sh"
  1. What does find ${path} -path "*/a" -name "script.*.sh" command do in real?

It searches for files in the subtree rooted at ${path} whose paths (starting with ${path}) match the pattern */a, and whose basenames match the pattern script.*.sh. Each in full. The paths of any files matching both criteria will be printed, but the conditions cannot both be satisfied for the same file, so nothing will be printed.

  1. There is debug mode in the find? I mean how does it understand what I say to it (example: path(s) where it will search, without find . -name "*te.st*", just ./*/*te.st*).

POSIX does not specify any debug facilities for find, but inasmuch as you have tagged linux and ubuntu, you are almost certainly using the GNU findutils version of find. This implementation does have debug facilities, which are documented on its manual page. You are looking for the -D option. It's unclear how helpful you would find it, but for maximum debug information you could add -D all to your find command, before the starting-point list.

  1. Can -name, -path live on the same command line (need cases, examples, explanation) or they mutually exclusive? Or can we say -path extended version of -name?

Yes, of course they can be used together, and occasionally it even makes sense to combine them. But the points that may have confused you are that

  • they are independent, and
  • -path tests the whole path, not just the directory part. Perhaps this is what you mean when you ask whether -path is an extended version of -name.

Your attempt at combining them failed because your -path predicate does not actually match the files you want to select. But this would work:

find ${path} -path  "*/a/*" -name "script.*.sh"

Also, combinations that use the -o connective (meaning "or") instead of default or explicit -a ("and") may find more use, or combinations in which one or both of the predicates is negated via -not.

7
  • On other word I said find to find directory and file - i.e. to find one entry which is a directory and a file at the same moment, but it's impossible, that is why result is empty. Am I right?
    – xUr
    Jul 11, 2023 at 22:41
  • I understand -path is extended version of -name. There are any cases where -name are useful then -path? On other words for what exist -name if exist -path?
    – xUr
    Jul 11, 2023 at 22:41
  • Not exactly. -path predicates can match any directory entry -- regular files, directories, special files. Same for -name predicates. In your example, each file considered by find is tested against both the -path predicate and the -name predicate. These cannot both match for any given file, not because they are inherently incompatible, but because of the specific path and name patterns you provide. This answer already provides an example of them working together. Jul 11, 2023 at 22:45
  • This answer also already discusses more plausible scenarios for using -name and -path together. Jul 11, 2023 at 22:48
  • It is not suitable for this question but anyway. I wanted to use something like this files=("${path}"/*/a/script.*.sh) at first. Can you advise me where I can read about this? Thanks in advance.
    – xUr
    Jul 12, 2023 at 0:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .