Welcome to Linux Forums!

With a comprehensive Linux Forum, information on various types of Linux software and many Linux Reviews articles, we have all the knowledge you need a click away, or accessible via our knowledgeable members.

Linux Forum ArticlesLinux ForumsLinux Forum DownloadsLinux HostsFree MagazinesJobs
Home|Register|FAQ|Member List|Calendar|Unanswered Posts|Forum Rules|Today's Posts|Advanced Search|
SEARCH FOR IN
Go Back   Linux Forums > GNU Linux Zone > Linux Programming & Scripting
Reload this Page Bash script to rename files
Linux Forums
Linux Forums
Welcome To The Linux Forums!
Welcome to Linux Forums. We pride ourselves in being one of the largest Linux communities on the web, we encourage you to REGISTER on our forums and participate in the community. There are over 150,000 members ready to answer your questions. JOINING US today will allow you to make new posts, get support, send messages to other members and submit downloads to our downloads directory and many other great features!

Linux Programming & Scripting C, Perl, PHP, Bash Scripts, anything programming or script related post in here!

Reply
 
Thread Tools Display Modes
Old 07-03-2007   #11 (permalink)
Linux Newbie
 
Join Date: Jun 2007
Posts: 224
OK, I think I've got it. Change the mv command to:

/bin/mv "${str}.mp3" "${str2}.mp3"
vsemaska is offline   Reply With Quote
Old 07-03-2007   #12 (permalink)
Just Joined!
 
Join Date: Aug 2006
Posts: 39
opps, didn't realize this thread was two pages.

/bin/cp "${str}.mp3" "${str2}.mp3"
+ /bin/cp 'Tool\ -\ 10\,000\ days\ \[01\]\ vicarious.mp3' Tool_-_10-000_days_01_vicarious.mp3
/bin/cp: cannot stat `Tool\\ -\\ 10\\,000\\ days\\ \\[01\\]\\ vicarious.mp3': No such file or directory

Still not working for me.
LinuxDummie is offline   Reply With Quote
Old 07-03-2007   #13 (permalink)
Linux Newbie
 
Join Date: Jun 2007
Posts: 224
Those backslashes are messing things up. My tests on my Linux system without them seems to work. Last thing I can suggest is try it without them as follows:

str="$1"
/bin/cp "${str}.mp3" "${str2}.mp3"
vsemaska is offline   Reply With Quote
Old 07-03-2007   #14 (permalink)
Just Joined!
 
Join Date: Aug 2006
Posts: 39
Wow, that seems to work lovely

I tried that before but I think the difference was, I wasn't using quotes around ${str}.mp3

Thank you very much for your help, I appreciate it.
LinuxDummie is offline   Reply With Quote
Old 07-03-2007   #15 (permalink)
Linux Newbie
 
Join Date: Jun 2007
Posts: 224
Glad to hear it worked. Now if you want to get rid of that loop to build str2 and let the Linux utilities do the work for you use this:

let len=${#1}-4
tmp="`echo "$1:$len" | awk -F: '{print substr($1,1,$2)}'`"
str2="`echo "$tmp" | sed -e 's/ /_/g' -e 's/\[//g' -e 's/\]//g' -e 's/,/-/g'`"

Your assignment is to figure out what those commands do.
vsemaska is offline   Reply With Quote
Old 07-03-2007   #16 (permalink)
Just Joined!
 
Join Date: Aug 2006
Posts: 39
That code is pretty scary
LinuxDummie is offline   Reply With Quote
Old 07-04-2007   #17 (permalink)
Linux Newbie
 
Join Date: Jun 2007
Posts: 224
Basically:

var="`<pgm>`" means execute <pgm> and assign its output to var.

var="`<pgm1> | <pgm2>`" means execute <pgm1> sending its output (pipe) to <pgm2> as input. The output of <pgm2> is assigned to var.

So:
tmp="`echo "$1:$len" | awk -F: '{print substr($1,1,$2)}'`"
uses utility awk to extract the left part of the filename, minus the .rpm

str2="`echo "$tmp" | sed -e 's/ /_/g' -e 's/\[//g' -e 's/\]//g' -e 's/,/-/g'`"
uses utility sed (stream editor) to substitute one character for another. The -e's means the following are sed scripts which in this case we're using the substitute command 's' where:
' ' is replaced with '_', '[' is replaced with nothing (removed), ']' is replaced with nothing, and ',' is replaced with '-'. The 'g' at the end of each substitute command means global, replace all occurrences.

Typical way to feed utilities input is to use 'echo' piping (|) its output to the utility.

Hope that helps.
vsemaska is offline   Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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
Trackbacks are Off
Pingbacks are Off
Refbacks are Off
 

Similar Threads
Thread Thread Starter Forum Replies Last Post
Need a Script to compare 2 files and >> diff to file3 Cormac Redhat / Fedora Linux Help 2 09-07-2006 05:25 PM
bash script to perfom actions based on extension john_551 Linux Programming & Scripting 7 08-25-2006 02:35 AM
Unique files BASH SCRIPT RooNui Linux Programming & Scripting 7 08-21-2006 04:56 AM
how to execute sql statements withi unix bash script ancy Redhat / Fedora Linux Help 0 07-24-2006 11:59 AM
Bash script call /bin/mv, spaces handling in parameters codism Linux Programming & Scripting 11 09-21-2004 04:24 PM

Free Magazines
Cisco News
Receive a free quarterly e-newsletter with exclusive articles on how Cisco IT uses its own products and solutions to enable the business.
subscribe
Systems Management News, the newspaper for IT systems administration and data center managers!
Each issue of Systems Management News is chock-full of news and analysis to help you understand what's happening in your field.
subscribe
The Enterprise Newsweekly
eWeek is the essential technology information source for builders of e-business.
subscribe
Oracle Magazine
Oracle Magazine contains technology strategy articles, sample code, tips, Oracle and partner news, how to articles for developers and DBAs, and more. Oracle (NASDAQ: ORCL) is the world's largest enterprise software company.
subscribe
Total Telecom
Total Telecom is "The Economist of the communications industry".
subscribe
More free magazines »



All times are GMT. The time now is 02:35 AM.




© 2000 - 2008 - All Rights Reserved - Property of  MAS Media

Content Relevant URLs by vBSEO 3.2.0