How to delete .htaccess file recursive based on size or content

Issue

I am seeking a terminal command to recursively go through folder structures and either delete .htaccess files with permission of 0444 and/or if possible to match the first line in the file for a safety measure.

Had a few accounts compromised on a server, which cleared up the malware / rootkits but noticed it added .htaccess files inside every folder with the following content:

<FilesMatch ".(py|exe|phtml|php|PhP|php5|suspected)$">
  Order Allow,Deny
  Deny from all
</FilesMatch>

Solution

You can use find to go recursively through multiple directories, search for files and execute a command like rm on the result.

find . -type f -perm 0444 -name ".htaccess" -exec echo rm {} \;
  • . current diretory / can be other path e.g. /etc
  • -type f search for files
  • -perm 0444 permission 0444
  • -name ".htaccess" will only look for files named .htaccess
  • -exec CMD {} \; run command like rm on the result {}
  • verify output of find and remove echo to remove files

Answered By – dnltinney

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published