I want to find all PHP scripts that are inside a directory named web. In particular, I'm interested in any files named index.php in any of these directories.

I know that I can use sudo find / -type d -name "web" to list all the directories, and then I can use sudo find <directory path> -type f -name "*.php" for each of the output paths, to see if it contains a PHP script.

But is there a way to do this in one line and two nested find commands: one find command used to get the paths to the web directories, and the second to use each of those directories in turn as the root for finding a file whose name matches the pattern *.php?


You don't need to call find twice if you use the -path option instead of -name:

find / -type f -path "*/web/*.php"

Specifically for every file index.php located under a directory named web:

find / -type f -path "*/web/*index.php"
  • +1, elegant and short :-) but it does not answer the question: 'But is there a way to do this in one line and two nested find commands' ;-) – sudodus Nov 3 '17 at 11:28
  • @sudodus Oh my, I must've read the question wrong. ;) – dessert Nov 3 '17 at 12:02
  • 2
    A one line answer without nesting is even better. Thanks to George and @sudodus for showing me how to use -exec. – James Newton Nov 3 '17 at 12:30
  • 1
    You are welcome @JamesNewton :-) – sudodus Nov 3 '17 at 12:53
  • 1
    @jschmitter Yes there is, with the globstar shell option explained e.g. in this answer – if this doesn’t do please ask a new question. – dessert Jun 26 '19 at 18:38

This command should achieve that:

find / -type d -name "web" -exec find {}/  -type f -name "*.php" \;

Tested on this tree:

├── index.php
├── kate
│   ├── jate
│   │   └── jate.php
│   └── kate.php
├── koko
│   └── read.php
└── web2
    └── index.php



Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.