LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices


Reply
  Search this Thread
Old 04-29-2007, 07:12 AM   #1
etechnophile
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:
nameofwebsite.com : somename.html
I would like to replace the : by a dash i.e - as shown below

Code:
nameofwebsite.com - 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 '-' ?

Code:
#!/bin/bash
# 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

Code:
rename : - nameofwebsite.com*
This works, but not recursively.

Thanks in advance.

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

Rep: Reputation: 38
With bash:

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

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

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

Last edited by radoulov; 04-29-2007 at 12:06 PM.
 
Old 04-29-2007, 09:50 AM   #3
ghostdog74
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
Code:
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(":" ,"-")))        
            os.rename(file,newfile)

Last edited by ghostdog74; 04-29-2007 at 09:53 AM.
 
Old 05-04-2007, 11:26 PM   #4
etechnophile
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.
 
  


Reply

Tags
rename



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 07:02 PM
Character encoding problem for filename in ubuntu xenthos Linux - General 1 03-22-2006 12:59 PM
change uploaded files from filename.avi to filename.avi.html like www.rapidshare.de latheesan Linux - Newbie 3 06-16-2005 04:33 AM
Renaming All Caps Filename Into Lower Case slpwkr Linux - General 3 06-03-2005 06:23 AM
Renaming Multiple Files with a "." on the end of the filename dbowles Programming 1 05-09-2005 04:28 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

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