Score:0

finding directories and files

kw flag

I understand that I can find directories with a specific pattern using:

find . -type d -name "tmp_*"

I understand that I can find files with a directory using:

find . -type f -name "tmp.conf"

How would I list all directories starting with tmp_ containing a file called tmp.conf and list the path to that file?

Thanks!

Score:0
cn flag

There are different ways to Rome. I probably would search specifically for files named tmp.conf files and have the full path printed for each occurrence, as in

find . -type f -name 'tmp.conf' -exec readlink -f {} \;
Score:0
hr flag

You could use

find . -path '*/tmp_*/tmp.conf'

or

find . -regex '.*/tmp_[^/]*/tmp\.conf'

The difference is that the first (which uses ordinary shell wildcards) will match things like ./dir/tmp_foo/subdir/tmp.conf because / is not treated specially by *. The second (which uses a regular expression) excludes intervening / characters explicitly.

If you want the path to the containing directory only (without the tmp.conf basename, you can use the find command's -printf with the %h specifier:

find . -path '*/tmp_*/tmp.conf' -printf '%h\n'

Alternatively you could do something like

find . -type d -name 'tmp_*' -execdir test -e {}/tmp.conf \; -print

or

find . -type d -name 'tmp_*' -exec sh -c '
  for f do [ -e "$f/tmp.conf" ] && echo "$f"; done
' find-sh {} +
Jason H avatar
kw flag
I think I did manage to figure this out. I did the following: find ./tmp_* -type f -name "tmp.conf" | sort -f
hr flag
@JasonH if you're only interested in a set of top-level directories, then you don't need `find` at all - you could just use `echo tmp_*/tmp.conf` or `printf '%s\n' tmp_*/tmp.conf` (you'd probably want to set `shopt -s nullglob` first).
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.