Visit Jeremy's Blog.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 04-29-2007, 08:12 AM   #1
LQ Newbie
Registered: Jun 2006
Location: India
Distribution: OpenSUSE 12.1
Posts: 29

Rep: Reputation: 15
Smile renaming files recursively by replacing a character in filename

I have many files which have filenames of the format

Code: : somename.html
I would like to replace the : by a dash i.e - as shown below

Code: - somename.html
I searched LQ and found a script that does the job recursively, but it's for replacing spaces by underscores. Since I don't know sed at all, can someone help me modify this script for replacing ':' by '-' ?

# Recursive replacement of spaces with "_" in files/dirs
[ $# -ne "1" ] && echo "Usage: $0 <dir-path>" && exit 1
find $1 -name "* *" | sed 's/^.*$/mv \"&\"/g' > t1$$
find $1 -name "* *"|sed -e 's/ /_/g' -e 's/^.*$/\"&\"/g' >t2$$
paste t1$$ t2$$ > t3$$; sh t3$$; #rm -f t?$$
#[t?$$ files will help you in an accidental action]
Any other way to do this will be appreciated too. I initially tried

rename : -*
This works, but not recursively.

Thanks in advance.

Last edited by etechnophile; 04-29-2007 at 08:52 AM.
Old 04-29-2007, 09:53 AM   #2
Registered: Apr 2007
Location: Milano, Italia/Варна, България
Distribution: Ubuntu, Open SUSE
Posts: 212

Rep: Reputation: 37
With bash:

find . -name "* : *.html"|
	while IFS= read -r;do 
		mv "${REPLY}" "${REPLY/ : / - }" 
With zsh:

autoload -U zmv
zmv '(**/)(* : *).html' '$1${2/ : / - }.html'

P.S. The bash solution, of course, will fail, if you have the same pattern somewhere in the path or more than once in the filename and you want to change not the first one (in the latter case both zsh and bash solutions will fail).
In the first case something like this will be more appropriate:

find . -name "* : *.html"|
	while IFS= read -r;do 
		mv "$REPLY" "${fpath}${fname/ : / - }" 

Last edited by radoulov; 04-29-2007 at 01:06 PM.
Old 04-29-2007, 10:50 AM   #3
Senior Member
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
If you have Python, here's an alternative. It recurses the current directory
for r,d,f in os.walk("/startdir"):    
    for file in glob.glob(os.path.join(r,"*.html")):
        filename = os.path.split(file)[-1]  
        if ":" in filename:      
            newfile = "%s" % (os.path.join(r,filename.replace(":" ,"-")))        

Last edited by ghostdog74; 04-29-2007 at 10:53 AM.
Old 05-05-2007, 12:26 AM   #4
LQ Newbie
Registered: Jun 2006
Location: India
Distribution: OpenSUSE 12.1
Posts: 29

Original Poster
Rep: Reputation: 15
Thumbs up Thanks

I can't try out any of these solutions right now since I'm away from my PC at the moment, but will try them out as soon as possible. Thank you radoulov and ghostdog74 for the fast replies.



Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Replacing a image filename with a regex pattern in a script jonaster Linux - General 1 09-05-2006 08:02 PM
Character encoding problem for filename in ubuntu xenthos Linux - General 1 03-22-2006 01:59 PM
change uploaded files from filename.avi to filename.avi.html like latheesan Linux - Newbie 3 06-16-2005 05:33 AM
Renaming All Caps Filename Into Lower Case slpwkr Linux - General 3 06-03-2005 07:23 AM
Renaming Multiple Files with a "." on the end of the filename dbowles Programming 1 05-09-2005 05:28 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:51 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration