LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 01-17-2003, 04:55 PM   #1
fio
LQ Newbie
 
Registered: Dec 2001
Posts: 7

Rep: Reputation: 0
Simple BASH script


Hey.

I am trying to rename a couple of files containing the character "?" in their names. I want to replace "?" with "-".

I've tried -
for i in `find . -name *\?*' ; do mv "$i" "`echo $i | sed -e s/\?/\-/g`" ; done

DIDN'T WORK.

Can anyone offer a perl / sed / ANY script to accomplish the task?

Thanks.
 
Old 01-17-2003, 06:45 PM   #2
nxny
Member
 
Registered: May 2002
Location: AK - The last frontier.
Distribution: Red Hat 8.0, Slackware 8.1, Knoppix 3.7, Lunar 1.3, Sorcerer
Posts: 771

Rep: Reputation: 30
for i in $(find . -name '*\?*'); do mv $i $(echo $i | sed -e s/\?/-/g); done

Note:Wont work with whitespace in the filename.
 
Old 01-18-2003, 05:13 AM   #3
fio
LQ Newbie
 
Registered: Dec 2001
Posts: 7

Original Poster
Rep: Reputation: 0
Thanks, but...

Thanks for the reply!

But I need it to work WITH white spaces..

Can you offer additional help?
 
Old 01-18-2003, 07:15 AM   #4
unSpawn
Moderator
 
Registered: May 2001
Posts: 27,543
Blog Entries: 54

Rep: Reputation: 2924Reputation: 2924Reputation: 2924Reputation: 2924Reputation: 2924Reputation: 2924Reputation: 2924Reputation: 2924Reputation: 2924Reputation: 2924Reputation: 2924
find . -name "*\?*"|while read raw; do
mv -f "${raw}" "$(echo ${raw} | tr "?" "-")"; done

Just make it a habit to*always* quote variables + a double quote can't be mistaken by a backtick and the use of $(action) is easier to spot than using backticks. Since it's a simple one char replacement I'm using "tr" instead of sed.
 
Old 01-18-2003, 11:00 AM   #5
fio
LQ Newbie
 
Registered: Dec 2001
Posts: 7

Original Poster
Rep: Reputation: 0
Thanks

Thanks,

Worked perfectly.

Fio
 
Old 01-18-2003, 04:26 PM   #6
nxny
Member
 
Registered: May 2002
Location: AK - The last frontier.
Distribution: Red Hat 8.0, Slackware 8.1, Knoppix 3.7, Lunar 1.3, Sorcerer
Posts: 771

Rep: Reputation: 30
Good job, unspawn!!
 
  


Reply


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 On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Simple bash script lp449 Linux - Networking 3 08-02-2005 05:25 PM
Simple bash script Soulful93 Programming 1 06-04-2005 08:26 PM
Simple Bash Script Help njdownes Programming 2 03-05-2005 08:35 AM
Simple Bash Script dmedici Programming 9 12-31-2004 04:48 AM
Help with simple bash script - please tw001_tw Linux - Software 5 12-19-2004 10:02 PM


All times are GMT -5. The time now is 01:49 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration