简体   繁体   中英

How to find all files with a particular parent directory in linux?

How do you find all files with a particular parent directory in the linux command terminal?

I know to find all files using find like so:

find . -name filename.extension

But is it possible to find all filename.extension files with the parent directory of foldername ?

I have tried the following but this does not work:

find . -name foldername/filename.extension

And I cannot find any example of how to do this.

So some example results I would expect are as so:

./example/project/website/foldername/filename.extension
./folder/demo/foldername/filename.extension
./more/files/foldername/filename.extension
./business/assets/foldername/filename.extension
./steven/foldername/filename.extension

Is there anyway to do this?

-path开关与find 一起使用

find . -path \*/foldername/filename.extension

Just try this

locate "/*/foldername/filename.extension"

provided you have updated index with updatedb

You can run:

find ./ | grep 'foldername/filename.extension$'

"find" command will find all files, and "grep" will filter them by regular expression.

PS. Also you can use "-exec" parameter of "find".

Answering the more generic question, I was hoping this would work

find . -path '*/foldername/*'

But instead of all files with parent it gives all files with ancestor . This will show only children:

find . -path '*/foldername/*' | sed 's|.*foldername/\([^/]*\).*|\1|' | sort -u

or

find . | egrep -o 'foldername/[^/]*' | sed 's|[^/]*/||' | sort -u

But that doesn't give paths, so

find . -iname foldername -exec find {} -maxdepth 1 -mindepth 1 \;
  1. Find all directories named examples .
  2. Then omit the results who has any parent directory named node_modules .

find . -type d -name examples | grep -v node_modules

只在父目录中显示结果

Find -maxdepth 1 -type f -name 'file.extension'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM