LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices



Reply
 
Search this Thread
Old 11-25-2011, 05:58 AM   #1
Foxbat1155
Member
 
Registered: Oct 2011
Location: Portugal.
Distribution: Debian squeeze - Fluxbox.
Posts: 109

Rep: Reputation: 0
Question How to make several "renames" by inoking a single command? Make a shell script file?


I am using the rename command to change capital letters and changing accented letters, and spaces, from directory and filename names to avoid problems.

I am using the rename command in several ways such as:

rename -v 's//e/' *

and rename -v 's//a/' *

I wanted to use 6 or 7 types of renames all at once to change all these types of accented letters.

Should I and how can I make a file that when invoked will do this all at once? Should I write a shell script?

Thankyou.
 
Old 11-25-2011, 06:46 AM   #2
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6
Posts: 1,602

Rep: Reputation: 483Reputation: 483Reputation: 483Reputation: 483Reputation: 483
Yes, you should write a shell script, pick a shell of your choice and start your journey on learning scripting, personally I use bash, but it's a matter of personal preference.

At the simplest level you can just create a file with all your commands in it, chmod it to 755 and then execute it with ./scriptname

Personally I like to append .sh at the end of files to show that they are scripts.

Good luck!
 
1 members found this post helpful.
Old 11-25-2011, 07:13 AM   #3
Foxbat1155
Member
 
Registered: Oct 2011
Location: Portugal.
Distribution: Debian squeeze - Fluxbox.
Posts: 109

Original Poster
Rep: Reputation: 0
Exclamation thankyou

Thankyou Sir. So if I write a shell script for a command called "cleaname" for bash I can just write cleaname on terminal and it will operate?

By the way WHat is the difference between the s and y in

rename -v 's/ /_/' *

and

rename -v 'y/ /_/' *

Is it just that s will replace 1 time and y will repeat for all occurrences as I have noted upon usage of both?

Thankyou in advance.
 
Old 11-25-2011, 07:14 AM   #4
bnguyen
Member
 
Registered: Jul 2010
Distribution: Slackware
Posts: 125

Rep: Reputation: 32
You can use something like:

find | ( while read a; do b=$(echo $a | sed -e 's/ /_/g' -e 's/\|\|/e/g' -e 's/\|\|/a/g') ; mv -i "$a" "$b" ; done )

Add other letters to meet your need.
 
1 members found this post helpful.
Old 11-25-2011, 07:19 AM   #5
Foxbat1155
Member
 
Registered: Oct 2011
Location: Portugal.
Distribution: Debian squeeze - Fluxbox.
Posts: 109

Original Poster
Rep: Reputation: 0
Lightbulb Thankyouu

Thankyou Nguyen.

Very helpful.

I suppose I can use this line in a shell script also?
 
Old 11-25-2011, 07:26 AM   #6
bnguyen
Member
 
Registered: Jul 2010
Distribution: Slackware
Posts: 125

Rep: Reputation: 32
Of course you can put it in a script; or just use it directly from the command line.

You can use this on any distribution while the rename command you posted before is Debian-specific I believe.
 
1 members found this post helpful.
Old 11-25-2011, 07:31 AM   #7
jhwilliams
Senior Member
 
Registered: Apr 2007
Location: Portland, OR
Distribution: Debian, Android, LFS
Posts: 1,168

Rep: Reputation: 208Reputation: 208Reputation: 208
There's definitely got to be a better way to do this, but this does seem to work.

Assuming the below is saved to translate.sh, run it like ./translate.sh <filelist>, e.g. ./translate.sh *

Code:
foo=(                        e i  u )
bar=( c a e i o u a e i o u a e i o u y a e i o u a e i o u )

i=${#bar[@]}

while [ $i -gt 0 ]; do
    i=$((i-1))
    rename "s/${foo[$i]}/${bar[$i]}/g" "$@"
done
 
1 members found this post helpful.
Old 11-25-2011, 07:34 AM   #8
Foxbat1155
Member
 
Registered: Oct 2011
Location: Portugal.
Distribution: Debian squeeze - Fluxbox.
Posts: 109

Original Poster
Rep: Reputation: 0
Thankyou

Thankyou Sir Williams.

So foo and bar are just general purpose variable names?
 
Old 11-25-2011, 07:35 AM   #9
Foxbat1155
Member
 
Registered: Oct 2011
Location: Portugal.
Distribution: Debian squeeze - Fluxbox.
Posts: 109

Original Poster
Rep: Reputation: 0
Exclamation recursive

Sir Williams, is that a recursive while cycle?
 
Old 11-25-2011, 07:40 AM   #10
jhwilliams
Senior Member
 
Registered: Apr 2007
Location: Portland, OR
Distribution: Debian, Android, LFS
Posts: 1,168

Rep: Reputation: 208Reputation: 208Reputation: 208
Hi Foxbat, yea -- I should probably have named them "original" and "translated", instead of "foo" and "bar". That would make better sense to read. :-)

No, it's not recursive. I'm just calling the rename binary from the script, in a loop.
 
1 members found this post helpful.
Old 11-25-2011, 07:42 AM   #11
Foxbat1155
Member
 
Registered: Oct 2011
Location: Portugal.
Distribution: Debian squeeze - Fluxbox.
Posts: 109

Original Poster
Rep: Reputation: 0
Post Thankyou

Mr. Williams, why do you do i-1 instead of i+1? Is that being recursive, as in, going from the end to the beggining?

Thankyou
 
Old 11-25-2011, 07:45 AM   #12
Foxbat1155
Member
 
Registered: Oct 2011
Location: Portugal.
Distribution: Debian squeeze - Fluxbox.
Posts: 109

Original Poster
Rep: Reputation: 0
Question spaces

foo=( e i u )
bar=( c a e i o u a e i o u a e i o u y a e i o u a e i o u )

i=${#bar[@]}

while [ $i -gt 0 ]; do
i=$((i-1))
rename "s/${foo[$i]}/${bar[$i]}/g" "$@"
done

Mr Williams if I want to add the space to the foo and change it with a _ in the bar I just alter this?

foo=( e i u \ )
bar=( c a e i o u a e i o u a e i o u y a e i o u a e i o u _

i=${#bar[@]}

while [ $i -gt 0 ]; do
i=$((i-1))
rename "s/${foo[$i]}/${bar[$i]}/g" "$@"
done
 
Old 11-25-2011, 07:50 AM   #13
jhwilliams
Senior Member
 
Registered: Apr 2007
Location: Portland, OR
Distribution: Debian, Android, LFS
Posts: 1,168

Rep: Reputation: 208Reputation: 208Reputation: 208
Looks like you forgot the ending ), but otherwise, sure, that should work.

BTW, usually rename, sed, and tr are great for this sort of thing. I've only written that script the way I have because of the non ASCII characters.

Last edited by jhwilliams; 11-25-2011 at 07:51 AM.
 
1 members found this post helpful.
Old 11-25-2011, 07:54 AM   #14
Foxbat1155
Member
 
Registered: Oct 2011
Location: Portugal.
Distribution: Debian squeeze - Fluxbox.
Posts: 109

Original Poster
Rep: Reputation: 0
Cool thankyou

Yes I forgot, thankyou =)
 
Old 11-25-2011, 08:14 AM   #15
Foxbat1155
Member
 
Registered: Oct 2011
Location: Portugal.
Distribution: Debian squeeze - Fluxbox.
Posts: 109

Original Poster
Rep: Reputation: 0
Post file error

I have written a file with

foo=( e i u )
bar=( c a e i o u a e i o u a e i o u y a e i o u a e i o u )

i=${#bar[@]}

while [ $i -gt 0 ]; do
i=$((i-1))
rename "s/${foo[$i]}/${bar[$i]}/g" "$@"
done


but this has resulted that when I invoke it on the terminal it just stays on an infinite loop, apparently, as it does nothing and then I have to do ^C to stop it and then I do ls and it is all the same.

Any suggestions for modifications?
 
  


Reply

Tags
batch, rename, script, shell


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
[SOLVED] Errors executing shell script: "command not found" and "no such file or directory" eko000 Linux - Newbie 1 01-14-2011 08:54 AM
Shell script: I have string "abc____def____ghi", how to make "abc def ghi" vouser Programming 8 03-09-2010 11:01 PM
Problem in compile library file command "make" reallynewbie2009 Linux - Newbie 4 09-14-2009 11:04 AM
So many errors when I typed the "make" and "make install" command Niceman2005 Linux - Software 23 07-22-2009 03:33 PM
"make" command not working in shell unasriseth Linux - Newbie 6 05-25-2004 10:08 AM


All times are GMT -5. The time now is 11:19 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration