11

so I'm in the main folder for my web hosts, trying to find a file using find. I couldn't find it - it was listed as no such file or directory - and I thought maybe it isn't anywhere.

However the following command doesn't work either:

find index.php

which is wrong cause there are a gazillion of them. Why is find not working? Is there a better command to use?

4
  • 3
    find /some/dir -name index.php
    – thrig
    Commented Sep 7, 2015 at 16:06
  • 1
    It is generally important to include any output or error message produced. In your case it would have been either index.php or find: `index.php': No such file or directory depending on whether or not there was an index.php in your current directory. Commented Sep 7, 2015 at 19:51
  • I think it would really help if you could provide some output when you type a command that is failing.
    – VaTo
    Commented Sep 7, 2015 at 20:55
  • @Victor, I had a similar problem, FWIW even passing debug flags like -D stat to find produces no output if find works correctly, finding no matches, but in an unexpected way.
    – pbhj
    Commented Oct 17, 2016 at 13:44

3 Answers 3

20

The syntax of find is not like what you have written, please read the manual page man find to get detailed idea.

For example if you want to find files named index.php on the current directory and all the sub directories under it, you can use:

find . -name index.php -type f 

If you want to search for files having names say findex.php, index.phpfoo, index.php you need to use:

find . -name '*index.php*' -type f 

* is a glob pattern meaning zero or more characters.

On the other hand if you want to look in the current directory only :

find . -maxdepth 1 -name '*index.php*' -type f 
5
  • 1
    find index.php works on my debian jessie. Commented Sep 7, 2015 at 20:05
  • 4
    @OrtomalaLokni That only lists index.php in the current directory if present, it doesn't look in subdirectories. Commented Sep 7, 2015 at 21:40
  • yours is the most concise explanation and example combo. Thanks Commented Sep 8, 2015 at 15:07
  • The man page for find (on my current Ubuntu install "find (GNU findutils) 4.7.0-git") says that putting the -name first will reduce resource usage as otherwise it checks every object to see if it is a file, then checks the name. So, minor modification find . -name index.php -type f to speed things up a little.
    – pbhj
    Commented Oct 17, 2016 at 13:31
  • I needed the quotes '' and it worked
    – coretechie
    Commented Jul 24, 2023 at 4:16
19

I had the same problem using find from findutils 4.7.0-git. If I do find . -maxdepth 12 -name '*' -type f then despite having a million files in the below tree no files are returned except those in the current dir. The problem for me was that the dirs below were links, so I needed to do:

find -L . -maxdepth 12 -name '*' -type f

The -L ensures that any links are followed (used to be --follow but that was deprecated).

1
  • 1
    My find command was missing dirs that I knew existed and it turned out they were links. Thanks!
    – emhohensee
    Commented Jan 17, 2019 at 15:27
0

Look into the manage of find. There are multiple usage. For example: Find .| grep "Index.php" Or find . -name Xyz.php

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .