$: cat tags > delete | extension | find | сервер | ssh

How to find all files by extension in Linux

To find all files by extension in Linux can be useful for cleaning the site from harmful files and files with extensions that should not be in a certain folder.

For example, if a WordPress site is infected with malware files and you know that /wp-content/uploads must not contain .php files, you can easily check if the .php files are hiding in that location.

You can check any other folder and search by any other extension in the same way.

Keep in mind, if you are doing this on a server, you need SSH access to the server.

Find all files in the given folder by extension

To find all files by extension in Linux, use this command if you want to find all .php files:

find /PATH/FOLDER -type f -iname "*.php"

Please replace /PATH/FOLDER with the exact path to the folder you want to search all files by .php extension.

.php is example, replace it by any other you are looking for.

List all files by requested extension into a text file

If you want to list all those files that were found in a list in a txt file (for later use, for example to show a client for whom you are cleaning the site), then use this command:

find /PATH/FOLDER -type f -iname "*.php" > /PATH/FOLDER/file-list.txt

Please replace /PATH/FOLDER with the exact path to the folder you want to search all files by .php extension.

Please replace /PATH/FOLDER/file-list.txt with the location and name of the file where you want to save the list of files found.

This way you can search any other file. In the command, just change it and instead of .php put some other extension. For example mp2, mp3, wmw… and the like which are often used to camouflage harmful files.

Find and delete all files by extension

Here you have to be careful not to delete all the files from a location that might contain certain files with a certain extension.

For example, WordPress contains .php files in the /wp-admin folder but should not contain .php files in the /wp-content/uploads folder. In this case, you want to delete all the files from the /wp-content/uploads folder, but leave all the others where they are.

To delete all files by extension in a specific folder, proceed as follows:

find /PATH/ -name "*.php" -delete

Please replace /PATH/ with the exact location to the folder where you want to delete all the files per the given extension.

This command will find and delete all files ending with .php extension. This process is irreversible. Think carefully and check the path before pressing Enter. Similar article: How to delete all .htaccess files with one command

Author, Ivan Blagojević

Engineering Lead at Adriahost. I spend most of my time on Linux operating systems with cPanel/WHM panels.

I help people solve problems and choose the right hosting for their site.

I write here in my spare time.

Time4VPS - VPS hosting in Europe