Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

ext4 allows certain chars in filenames which ntfs doesn't. Is there a script to replace those chars in filenames?

share|improve this question
pathchk may help. It doesn't do the conversion, but should tell you if a file needs converting. – Mikel May 7 '11 at 6:48
up vote 2 down vote accepted

The following should replace all disallowed characters in filenames with underscores. Note that this may cause multiple files to be renamed to the same thing. For example, if you had two files called file>/txt and file<.txt, both will be renamed to file_.txt, and one will overwrite the other, deleting it.

find /path/to/ntfs/mount/ -print0 | xargs -0 rename 's{[\\:*?"<>|]}{_}g'

This command is valid for the perl version of rename, which is what Debian provides. Your distro may provide a version of rename with a slightly different syntax.

share|improve this answer
This syntax is only valid for perl-rename, which ships as rename on Debian. Other distros come with a simpler rename from util-linux. – grawity May 7 '11 at 23:31
Also, 1) do not replace /, because this would affect legitimate path separators; 2) the g option is needed to match all characters, not just the first occurence. – grawity May 7 '11 at 23:34
Thanks for the comments. – Ryan Thompson May 7 '11 at 23:38
I think it's much better to convert some characters to the nearest/most logical ones instead of _. For example a pair of < and > should be converted to ( and ) respectively, or both to % or ~, while ":" may be better replaced with " - " – Lưu Vĩnh Phúc Sep 11 '13 at 8:15

You must log in to answer this question.

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