Delete files with same name at the end of the file

6,346

Solution 1

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

Solution 2

To match repeating extensions, with zsh:

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

Recursively:

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

Recursively:

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

With GNU find, recursively:

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

Solution 3

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

find . -name "*.php.php*" -exec rm '{}' \;
Share:
6,346

Related videos on Youtube

Danny
Author by

Danny

Updated on September 18, 2022

Comments

  • Danny
    Danny over 1 year

    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

    • debal
      debal about 10 years
      rm *.php.php* this will delete all files that have more than one php extension
  • Danny
    Danny about 10 years
    this command helps me remove the files in the directory ...I have those files in Sub directory too.....
  • Danny
    Danny about 10 years
    Thanks a lot....I tried this find . -name ".php.php" -print0 | xargs -0 rm -rf and it worked
  • Gilles 'SO- stop being evil'
    Gilles 'SO- stop being evil' about 10 years
    Or, under non-embedded Linux, FreeBSD, OSX or Cygwin: find . -name "*.php.php" -delete