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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting share a shell script which can replace weird characters in directory or file name
# 1  
Old 03-13-2012
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.

Last edited by begonia; 03-13-2012 at 10:31 PM.. Reason: delete one line which is useless
This User Gave Thanks to begonia For This Post:
# 2  
Old 03-13-2012
Code:
$ echo "joker's world" | sed 's/'$(echo -e "\047")'//'
jokers world
$
$  echo "joker's world" | sed "s/'//"
jokers world
$

# 3  
Old 03-13-2012
thanks
But I know how to replace apostrophe in text string,
It's not so easy to replace apostrophe in file name
# 4  
Old 03-13-2012
Something like this?

Code:
# 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
#

# 5  
Old 03-13-2012
Bug

Hmmm...
thanks for your response Smilie
What I mean is to rename its name permanently.
In your case,after your command
Code:
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.

Last edited by Franklin52; 03-14-2012 at 05:07 AM.. Reason: Please use code tags for code and data samples, thank you
# 6  
Old 03-13-2012
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script cannot create directory and move the file to that directory

I have a script, which is checking if file exists and move it to another directory if then mkdir -p ${LOCL_FILES_DIR}/cool_${Today}/monthly mv report_manual_alloc_rpt_A_I_ASSIGNMENT.${Today}*.csv ${LOCL_FILES_DIR}/cool_${Today}/monthly ... (9 Replies)
Discussion started by: digioleg54
9 Replies

2. Shell Programming and Scripting

Weird Error (: No such file or directory)

Hi Everyone, I am trying to make this script to use, to find out the DAHDI channel status. Every thing works fine, I even get proper results, however I have this weird error that comes along. Can someone please help me. Thanks a lot. #!/bin/bash # Color to set the test to when a channel is... (3 Replies)
Discussion started by: jeetz
3 Replies

3. Shell Programming and Scripting

Shell scripting-I need a script which should watch a directory for a file with specific directory

I need a script which should watch a directory for a file with specific directory. If it finds a file in directory, it should search for few specific keyword in the file. if the keyword exists, it should trim string from specific column. The file should be moved to another directory and the a... (8 Replies)
Discussion started by: akashdeepak
8 Replies

4. Shell Programming and Scripting

Shell script to find and replace contents of files in directory

Hi all This is my first post. Please bear with me with all my mistakes. I started learning shell since couple of days now and this might be quite basic for all, i want to search for files in a directory containing specific string and replace it with new string. The code i wrote is quite bulky... (2 Replies)
Discussion started by: theprogrammer
2 Replies

5. Shell Programming and Scripting

Using shell to replace characters in a text file

Can I just say, this is such a frustrating and yet enormously rewarding field of study. I'm in the middle of configuring GeekTool (Uh oh, stupid n00b) and I really only have one question. I'm using Automator to grab a RSS feed, having GeekTool continually run that application every 10 minutes,... (7 Replies)
Discussion started by: SomeTechGuy
7 Replies

6. UNIX for Dummies Questions & Answers

Watch for File on Windows share using Shell Script

I want to poll a particular file on a Windows shared drive using shell script and copy it to a Unix directory if present. Step wise execution: 1) Poll/watch for a file in the following location on Windows share: (SRC_DIR=\\mum\dharmesh\research\ST_DXR_20080821 to DXR.xls) 2) If present,... (1 Reply)
Discussion started by: dharam_v
1 Replies

7. Shell Programming and Scripting

Watch for File on Windows share using Shell Script

Watch for File on Windows share using Shell Script I want to poll a particular file on a Windows shared drive using shell script and copy it to a Unix directory if present. Step wise execution: 1) Poll/watch for a file in the following location on Windows share:... (1 Reply)
Discussion started by: dharam_v
1 Replies

8. Shell Programming and Scripting

Weird Ascii characters in file names

Hi. I have files in my OS that has weird file names with not-conventional ascii characters. I would like to run them but I can't refer them. I know the ascii # of the problematic characters. I can't change their name since it belongs to a 3rd party program... but I want to run it. is there... (2 Replies)
Discussion started by: yamsin789
2 Replies

9. Shell Programming and Scripting

Replace characters in all file names in a particular directory

Hi, I have searched the forum on how to mass replace the file names. We are doing the migration and I am trying to accomplish a task where I have to replace all UNIX scripts in a particular directory that start with bdw to fdm... For example: bdw0110137.sh should be fdm0110137.sh Keep the... (4 Replies)
Discussion started by: madhunk
4 Replies

10. UNIX for Dummies Questions & Answers

How to get rid of all the weird characters and color on bash shell

Does anyone of you know how to turn off color and weird characters on bash shell when using the command "script"? Everytime users on my server used that command to record their script, they either couldn't print it because lp kept giving the "unknown format character" messages or the print paper... (1 Reply)
Discussion started by: Micz
1 Replies
Login or Register to Ask a Question