6

I want to find the path for directory name bbb where the parent directory called aaa

For example

/aaa/bbb
/tmp/aaa/bbb
/usr/bin/aaa/bbb
/home/aaa/bbb
/home/aaa/xxx/bbb

So i wrote something like this:

find /*/aaa -name bbb

In some platforms it works and in some it doesn't, and in any case the /aaa/bbb is not found because there is no parent directory to aaa

I guess I could run find / -name bbb | grep \/aaa but I think if there is something smarter..?

Nir
  • 1,265
  • 7
  • 23
  • 34
  • 1
    many systems have the `locate` command implemented which nightly keeps a database of all filenames. you can then just `locate aaa/bbb`. – meuh Aug 24 '15 at 12:19

2 Answers2

10

Use the -path option for this case:

find / -type d  -path '*/aaa/bbb'

From the man page for find:

File name matches shell pattern pattern. The metacharacters do not treat / or . specially; so, for example,

find . -path "./sr*sc"

will print an entry for a directory called `./src/misc' (if one exists).

Cross-Platform Compatibility

Edit: I just noticed the and tags.

You don’t specify which version of find you’re using and the above information applies to GNU find. However, the man page also specifies that,

The predicate -path is also supported by HP-UX find and will be in a forthcoming version of the POSIX standard.

so it looks like -path can be used in HP-UX but not with AIX (at least not AIX 7.1, the latest release as of 2015-08-24).

Stéphane Chazelas
  • 522,931
  • 91
  • 1,010
  • 1,501
Anthony Geoghegan
  • 12,605
  • 7
  • 59
  • 62
  • Not working. Return nothing. – Nir Aug 24 '15 at 10:17
  • @nir There was a typo in my find command where it was missing its directory argument, `/`. I've fixed that now. I also added the ` -type d` argument to only provide the path to the directory itself and not files contained within it. – Anthony Geoghegan Aug 24 '15 at 10:26
  • I dont know which version of `find` I have. This code works in linux abut in AIX I get `-path is not a valid option.` – Nir Aug 24 '15 at 12:01
  • 1
    `-path` is POSIX but was recently (issue 7, susv4, 2008) added to the standard. – Stéphane Chazelas Aug 24 '15 at 12:46
  • 1
    I'll mark this as the answer for future searches, but as my main platform is AIX it's not very helpful and I don't see better solution than my `find` + `grep`. – Nir Aug 25 '15 at 09:05
0

/*/aaa does not match /aaa and this is the reason why find cannot find /aaa/bbb

If you don't like to start the find at / and if you don't have a recent find like the portable sfind that supports -path you need a better manual start list.

schily
  • 18,806
  • 5
  • 38
  • 60