45

On a Linux server, I need to find all files with a certain file extension in the current directory and all sub-directories.

Previously, I have always used the following command:

find . -type f | grep -i *.php

However, it doesn't find hidden files, for example .myhiddenphpfile.php. The following finds the hidden php files, but not the non-hidden ones:

find . -type f | grep -i \.*.php

How can I find both the hidden and non-hidden php files in the same command?

1
  • 2
    You know that the "re" in "grep" stands for "regular expression", right? I have no clue how either of those command lines are supposed to work... Commented May 6, 2010 at 7:56

3 Answers 3

35

...

find . -type f -name '*.php'
4
  • Any recursive option? This doesn't seem to be going into subdirectories where I ran the command. I don't know what directory the file is in. Commented Feb 5, 2020 at 22:39
  • 4
    What if I want both files and directories that may be hidden or not?
    – Rodrigo
    Commented Mar 29, 2020 at 17:34
  • @Rodrigo, just leave out -type f, like so: find . -name '*.php'. Commented Nov 30, 2022 at 16:21
  • For anyone who wanna find all hidden files, you could try find . -name ".[^.]*" Commented Aug 5, 2023 at 10:07
10

It's better to use iname (case insensitive).

I use this find command to search hidden files:

find /path -type f -iname ".*" -ls

Extracted from: http://www.sysadmit.com/2016/03/linux-ver-archivos-ocultos.html

1

The issue is grep, not the find (try just find . -type f to see what I mean).

If you don't quote the * then the shell will expand it - before grep even sees its command line arguments; since the shell doesn't find hidden files by default, you'll have issues.

The reason it's only finding the hidden file is because the shell has already expanded the * and so grep is only matching that one file.

You must log in to answer this question.

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