Delete files with same name at the end of the file

i have a lot of files in my folder and sub directories like

ajax_hostel_room_master.php.php          class_hostel_registration.php.php
ajax_hostel_room_master.php.php.php      class_hostel_registration.php.php.php
ajax_hostel_room_shifting.php            class_hostel_room_allocation.php
ajax_hostel_room_shifting.php.php        class_hostel_room_allocation.php.php
ajax_hostel_room_shifting.php.php.php    class_hostel_room_allocation.php.php.php

I want to keep the filename.php and delete the other files like filename.php.php

  • 1
    rm *.php.php* this will delete all files that have more than one php extension
    – debal
    Mar 20, 2014 at 8:58

3 Answers 3


rm -- *.php.php this will delete all files that have more than one php extension

for all sub directories you need

find /scripts/tmp -name "*.php.php" -exec rm {} +

/scripts/tmp is the directory under which my files and subdirectories existed

  • this command helps me remove the files in the directory ...I have those files in Sub directory too.....
    – Danny
    Mar 20, 2014 at 9:04
  • Thanks a lot....I tried this find . -name ".php.php" -print0 | xargs -0 rm -rf and it worked
    – Danny
    Mar 20, 2014 at 9:16

To match repeating extensions, with zsh:

rm -- *.*.*(e{'[[ $REPLY:t =~ "(\..*)\1$" ]]'})


rm -- **/*.*.*(e{'[[ $REPLY:t =~ "(\..*)\1$" ]]'})

That would match a.php.php and b.x.x and c.x.y.x.y (and .php.php).

With ksh93:

rm -- *@(.*)\1


set -o globstar
rm -- **/*@(.*)\1

With GNU find, recursively:

find . -regex '.*\(\.[^/]*\)\1' -exec rm {} +

Try this at the root of your directory where you want to delete the files:

find . -name "*.php.php*" -exec rm '{}' \;
  • 1
    Or, under non-embedded Linux, FreeBSD, OSX or Cygwin: find . -name "*.php.php" -delete Mar 20, 2014 at 23:03

You must log in to answer this question.

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