I have seen a few similar questions, but nothing that shows how to remove any of a set of special characters from all file names recursively.

I want to keep uppercase and lowercase letters, numbers and underscores and get rid of all special characters in the filenames. I have seen colons and question mark chars and a couple file names with newlines.


If you want to do it from command-line , you can use rename command.

rename 's/[^a-zA-Z0-9_]//' *.<file_format>

The above command will retain Uppercase,lowercase letters ,underscore and numbers. It removes all other chars.

  • Thanks but that does not remove double quote chars from the file names. Also, you need to add \. so that it does not remove the dot before the file extension. Hope you can improve your answer. – MountainX Nov 3 '13 at 6:04
  • also this doesn't handle subdirectories recursively. – MountainX Nov 3 '13 at 6:29
  • @MountainX I have checked its man-page .It dont have such facility . – rɑːdʒɑ Nov 3 '13 at 6:44
  • 1
    You'll want to use the g flag, like so: 's/[^a-zA-Z0-9_]//g', otherwise this will only remove the first match for the pattern. I think that is the issue @MountainX is running into, since this command removes double quote characters for me perfectly well. I would also recommend looking up 'bash globstar' if you want recursiveness, MountainX. – evilsoup Nov 3 '13 at 19:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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