How to Find All Files in a Directory With Extension


I needed to find all files in a directory on Linux that ended with the file extension .php.

How to find all files in a directory with extension

You can do this as follows:

cd <your directory path>

find . -type f -name "*.php"

How to find other file extensions in a directory

Just swap out the .php value for another file extension that you would like to search for instead.

If you wanted to find all files in directory with extension that are Java Jar files:

find . -type f -name "*.jar"

If you wanted to find all files in directory with extension that are text files:

find . -type f -name "*.txt"

Why did I need to do this?

I operated a site that some malicious actors had uploaded PHP scripts to, in order to perform various activities. I had realised this and needed to find all occurrences of these and other files. I knew that PHP was being used to execute these scripts, so I used the above find tool to locate and remove the files in question.

How to use the find tool for other purposes

Linux ships with a very useful tool called find that allows you to locate files in a directory hierarchy.

find [-H] [-L] [-P] [-D debugopts] [-Olevel] [starting-point...] [expression]