remove special characters from filename recursively


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting remove special characters from filename recursively
# 1  
Old 03-20-2009
CPU & Memory remove special characters from filename recursively

hi:

i have several thousand files from users and of course they use all kind of characters on filenames. I have things like:

My special report (1999 ) Lisa & Jack's work.doc

crazy.

How do I remove all this characters in the current dir and subdirs too?

Thanks.
# 2  
Old 03-20-2009
Code:
find . -type f -print | while read file
do
    file_clean=$( echo ${file} | tr " ()&'" "_____" )
    echo $file $file_clean
done

Note: that's 5 underscores in the second argument to tr
If it works as intended (I haven't tested it), change the echo in line 4 to mv.
# 3  
Old 03-20-2009
Quote:
Originally Posted by pludi
Code:
find . -type f -print | while read file
do
    file_clean=$( echo ${file} | tr " ()&'" "_____" )
    echo $file $file_clean
done

Note: that's 5 underscores in the second argument to tr
If it works as intended (I haven't tested it), change the echo in line 4 to mv.

That script will fail for many reasons.

First, you don't want to find all files, only those whose name contains a space:

Code:
find . -type f -name '* *' -print |

Second, your read loop will strip leading or trailing spaces from the filenames (probably not a problem, but you never know) and remove any backslashes (also probably not a problem, but why risk it?):

Code:
 while IFS= read -r file

Third, modern versions of tr do not require the replacement to be repeated:

Code:
file_clean=$( echo "$file" | tr " ()&'" "_" )

Fourth, the mv line will fail when a filename contains a space (which, given the problem you are trying to solve, is always).

Code:
echo "$file" "$file_clean"

In bash and ksh93, you don't need tr:

Code:
file_clean=${file//[ ()&\']/_}

# 4  
Old 03-20-2009
hi:

I ended up using this

Code:
 find . -print | while read file
do
  file_clean=${file//[ ()&\'\,]/_}
  mv "$file" "$file_clean"
done

since I wanted to rename folders too and there were some filenames with

my docs, photos, and more/year 1999, 2000.doc

crazy users!

thanks.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to remove special characters?

Hi Gurus, I have file which contains some unicode charachator like "ü". I want to replace it with some charactors. I searched in internet and got command sed "s/ü/-/g", but I don't know how to type ü in unix command line. Please help me for this one. Thanks in advance (7 Replies)
Discussion started by: ken6503
7 Replies

2. Shell Programming and Scripting

How best to remove certain characters from filenames and folders recursively

hello, I'm trying to figure out which tool is best for recursively renaming and files or folders using the characters \/*?”<>| in their name. I've tried many examples that use Bash, Python and Perl, but I'm not much of a programmer I seem to have hit a roadblock. Does anyone have any... (15 Replies)
Discussion started by: prometheon123
15 Replies

3. Shell Programming and Scripting

Remove string between two special characters

Hi All, I have a variable like AVAIL="\ BACK:bkpstg:testdb3.iad.expertcity.com:backtest|\ #AUTH:authstg:testdb3.iad.expertcity.com:authiapd|\ TEST:authstg:testdb3.iad.expertcity.com:authiapd|\ " What I want to do here is that If a find # before any entry, remove the entire string... (5 Replies)
Discussion started by: engineermayur
5 Replies

4. Shell Programming and Scripting

remove special characters

hello all I am writing a perl code and i wish to remove the special characters for text. I wish to remove all extended ascii characters. If the list of special characters is huge, how can i do this using substitute command s/specialcharacters/null/g I really want to code like... (3 Replies)
Discussion started by: vasuarjula
3 Replies

5. UNIX for Dummies Questions & Answers

Files with special characters - how to remove

Hi, I have a directory that has a file which contained special characters in the filename. Can someone please advise how to remove the file, preferably with a rm -i ? Thanks in advance. Listing is as below: {oracle}> ls -1b bplog.bkup.001 bplog.bkup.002 bplog.bkup.003 bplog.bkup.004... (1 Reply)
Discussion started by: newbie_01
1 Replies

6. UNIX for Dummies Questions & Answers

How to Remove Special Characters

Dear Members, We have a file which contains some special characters. I need to replace these special character by a new line character(\n). The Special character is \x85. I am not sure what this character means and how we can remove it. Any inputs are greatly appreciated. Thanks... (5 Replies)
Discussion started by: sandeep_1105
5 Replies

7. Shell Programming and Scripting

Remove special characters from string

Hi there, I'd like to write a script that removes any set of character from any string. The first argument would be the string, the second argument would be the characters to remove. For example: $ myscript "My name's Santiago. What's yours?" "atu" My nme's Snigo. Wh's yors? I wrote the... (11 Replies)
Discussion started by: chebarbudo
11 Replies

8. AIX

Removing a filename which has special characters passed from a pipe with xargs

Hi, On AIX 5200-07-00 I have a find command as following to delete files from a certain location that are more than 7 days old. I am being told that I cannot use -exec option to delete files from these directories. Having said that I am more curious to know how this can be done. an sample... (3 Replies)
Discussion started by: jerardfjay
3 Replies

9. UNIX for Dummies Questions & Answers

remove special and unicode characters

Hi, How do I remove the lines where special characters or Unicode characters appear? The following query does work but I wonder if there is a better way. cat test.txt | egrep -v '\)|#|,|&|-|\(|\\|\/|\.' The following lines show that my query is incomplete. Warning: The word "*Khan" is... (1 Reply)
Discussion started by: shantanuo
1 Replies

10. Shell Programming and Scripting

Open filename with special characters in Perl

Hi All, I am facing a weird problem. I have got a directory structure copied from windows to Linux. Some of the folders are named like /gfs/data/Dow Jones $5/ DJ FCBOT_O_tick_1998.zip /gfs/data/Dow Jones $5/ DJ FCBOT_O_tick_2000.CSV /gfs/data/Dow Jones... (6 Replies)
Discussion started by: mitrashatru
6 Replies
Login or Register to Ask a Question