Re: Script to strip illegal characters from files and directories?



On Saturday 10 May 2008 11:56, Janis Papanagnou wrote:

No need to handle / in filenames; that's an illegal character in Unix
filenames. With ksh93 or bash you may want to try...

find . | while read -r f ; do mv -i "${f}" "${f//[:;*?\"<>|]}" ; done

where the characters are removed (as you seem to like) or try

find . | while read -r f ; do mv -i "${f}" "${f//[:;*?\"<>|]/_}" ; done

to replace the characters by an _ (which I think is better).

The script will try to do "mv . ." first, which of course will fail.
You should at least check that the new name differs from the old name, and
probably use "--" to indicate the end of the options to mv.

Furthermore, if a directory with strange characters is encountered first
(and find by default output directories first), then renaming the files
inside the directory will fail.

If the structire is as follows:

dir<>foo
|
+------file1**?
\------file:2:bar

Then "dir<>foo" will be renamed first, and subsequent attempts to
rename './dir<>foo/file1**?' and './dir<>foo/file:2:bar' to something else
will fail, since directory 'dir<>foo' does not exist anymore.

--
D.
.