8

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>
1
  • 1
    Did you get hacked and backdoored in a wordpress installation by any chance?
    – monoco
    Aug 12 at 7:37

2 Answers 2

13

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
6
  • Thanks mate, before i run that... will that run through all child dirs finding .htaccess files with that perm of 0444.... seems they are within every dir so need to find and remove. just wanted to confirm before i run that command but many thanks for help!
    – James
    Apr 23, 2021 at 8:31
  • Yes, find will run through every child-directory. Notice the echo after -exec will print the results containing the path.
    – 0xdnL
    Apr 23, 2021 at 8:39
  • ok cool thanks, will run that then! thanks for the help and will make answer for sure! thanks again!
    – James
    Apr 23, 2021 at 8:48
  • oh just one thing, anyway to make "find . -type f -perm 0444 -exec echo rm {} \;" target just .htaccess files ?
    – James
    Apr 23, 2021 at 8:48
  • By adding -name flag. I've updated the answer to search for files named .htaccess
    – 0xdnL
    Apr 23, 2021 at 8:54
7

This command removes all the .htaccess files in all the sub folders:

find . -name ".htaccess" -exec rm -rf {} \;
1
  • Please do not roll back edits which objectively improve your post, in this case by adding formatting.
    – Adriaan
    Mar 28 at 10:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.