2

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?

2

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
  • 1
    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
0

This command should achieve that:

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

Tested on this tree:

Documents/askubuntu/web
├── index.php
├── kate
│   ├── jate
│   │   └── jate.php
│   └── kate.php
├── koko
│   └── read.php
└── web2
    └── index.php

Result:

./Documents/askubuntu/web/koko/read.php
./Documents/askubuntu/web/kate/kate.php
./Documents/askubuntu/web/kate/jate/jate.php
./Documents/askubuntu/web/index.php
./Documents/askubuntu/web/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.