LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to make several "renames" by inoking a single command? Make a shell script file? (http://www.linuxquestions.org/questions/linux-newbie-8/how-to-make-several-renames-by-inoking-a-single-command-make-a-shell-script-file-915465/)

Foxbat1155 11-25-2011 05:58 AM

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.

TenTenths 11-25-2011 06:46 AM

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! :)

Foxbat1155 11-25-2011 07:13 AM

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.

bnguyen 11-25-2011 07:14 AM

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.

Foxbat1155 11-25-2011 07:19 AM

Thankyouu
 
Thankyou Nguyen.

Very helpful.

I suppose I can use this line in a shell script also?

bnguyen 11-25-2011 07:26 AM

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.

jhwilliams 11-25-2011 07:31 AM

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


Foxbat1155 11-25-2011 07:34 AM

Thankyou
 
Thankyou Sir Williams.

So foo and bar are just general purpose variable names?

Foxbat1155 11-25-2011 07:35 AM

recursive
 
Sir Williams, is that a recursive while cycle?

jhwilliams 11-25-2011 07:40 AM

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.

Foxbat1155 11-25-2011 07:42 AM

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

Foxbat1155 11-25-2011 07:45 AM

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

jhwilliams 11-25-2011 07:50 AM

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.

Foxbat1155 11-25-2011 07:54 AM

thankyou
 
Yes I forgot, thankyou =)

Foxbat1155 11-25-2011 08:14 AM

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?


All times are GMT -5. The time now is 04:14 AM.