Home > Net >  How to mass remove files that contain special characters in file name
How to mass remove files that contain special characters in file name

Time:02-08

Google Drive went a little crazy and create multiple iterations of files by appending multiples of "(1)", "(1) (1)" to the file names. So far I've been able to get a decent list of them with their path by using:

tree -f -ifpugDs $PWD | grep -e '(1) (1)' | cut -d "]" -f 2 > output.txt

Now I'm having trouble on how to delete them with the rm command. I Don't mind deleting a few of them by hand that the filter didn't pick up ( like "(2) (1)" ), but there are around 45K of them that the filter matched with.

CodePudding user response:

This will delete every file whose name ends in (1), recursively:

find . -name '*(1)' -exec rm {}  
  • -name '*(1) (1)' to only delete files ending with a double 1.
  • -name '*([0-9])' will match any single digit.
  • find . -mindepth 1 -maxdepth 1 -name '*(1)' -exec rm {} for no recursion.
  • I would do find . -name '*(1)' -exec echo rm {} \; to print a neat list of the rm commands to be executed. Review it, then remove echo to run for real.
  • Changing \; back to is optional, is more efficient.
  •  Tags:  
  • Related