share a shell script which can replace weird characters in directory or file name

I just finish the shell script .
This shell can replace weird characters (such as #$%^@!'"...) in file or directory name by "_"
I spent long time on replacing apostrophe in file/directory name

added: 2012-03-14
the 124th line (/usr/bin/perl -i -e "s#\'#\\'#g" /tmp/rpdir_level$i.tmp) is useless , it's for test and forget to delete it.

1 Like
$ echo "joker's world" | sed 's/'$(echo -e "\047")'//'
jokers world
$
$  echo "joker's world" | sed "s/'//"
jokers world
$

thanks
But I know how to replace apostrophe in text string,
It's not so easy to replace apostrophe in file name

Something like this?

# ls -l
total 32
-rw-r--r-- 1 root root   38 Feb 29 03:45 file1
-rw-r--r-- 1 root root   38 Feb 28 07:25 file2
-rw-r--r-- 1 root root  519 Mar 13 10:38 input
-rw-r--r-- 1 root root   12 Mar 13 11:57 joker's
-rw-r--r-- 1 root root  542 Mar 13 10:36 output
drwxr-xr-x 2 root root 4096 Feb 29 12:24 test
-rwxr-xr-x 1 root root  183 Mar 13 09:41 test.pl
-rwxr--r-- 1 root root  237 Mar 13 06:38 test.sh
-rwxr--r-- 1 root root    0 Mar  9 06:47 test.sh~
#
# find ./ -maxdepth 1 -name "*\'*"
./joker's
#
# find ./ -maxdepth 1 -name "*\'*" | sed "s/'//g"
./jokers
#

Hmmm...
thanks for your response :b:
What I mean is to rename its name permanently.
In your case,after your command

find ./ -maxdepth 1 -name "*\'*" | sed "s/'//g"

If you use "ls", then it still show file name with apostrophe

What I mean is that after you execute the shell, and then use "ls"
It should show corrected name.

Thank you for sharing this script. It is an interesting approach to the problem.

Personally I would avoid letting the Shell see the funny filenames by avoiding the use of Shell Environment Variables and using temporary files instead. Similarly if I really need to use "sed" (not "tr") then I'd compose the "sed" commands in a "sed" file where Shell can't see them.

I have a local problem with commercial software which can create files with any name - including screen control escape sequences and absolutely anything a user can type on a keyboard.
The script to deal with this rationalises the filename before any "old files" cleanup sees the file. It also "corrects" any filenames starting with a hyphen character (for obvious reasons) and deletes any file consisting totally of garbage characters and/or space characters.
The reasoning behind the script is to stop the "old files" cleanup failing or executing dubious "rm" statements by renaming or removing files with dodgy names before running the cleanup.