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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
|
11-25-2011, 04:58 AM
|
#1
|
|
Member
Registered: Oct 2011
Location: Portugal.
Distribution: Debian squeeze - Fluxbox.
Posts: 109
Rep:
|
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.
|
|
|
|
11-25-2011, 05:46 AM
|
#2
|
|
Member
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5
Posts: 537
Rep: 
|
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.
|
11-25-2011, 06:13 AM
|
#3
|
|
Member
Registered: Oct 2011
Location: Portugal.
Distribution: Debian squeeze - Fluxbox.
Posts: 109
Original Poster
Rep:
|
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.
|
|
|
|
11-25-2011, 06:14 AM
|
#4
|
|
Member
Registered: Jul 2010
Distribution: Slackware
Posts: 125
Rep:
|
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.
|
11-25-2011, 06:19 AM
|
#5
|
|
Member
Registered: Oct 2011
Location: Portugal.
Distribution: Debian squeeze - Fluxbox.
Posts: 109
Original Poster
Rep:
|
Thankyouu
Thankyou Nguyen.
Very helpful.
I suppose I can use this line in a shell script also?
|
|
|
|
11-25-2011, 06:26 AM
|
#6
|
|
Member
Registered: Jul 2010
Distribution: Slackware
Posts: 125
Rep:
|
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.
|
11-25-2011, 06:31 AM
|
#7
|
|
Senior Member
Registered: Apr 2007
Location: Portland, OR
Distribution: Debian, Android, LFS
Posts: 1,167
|
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.
|
11-25-2011, 06:34 AM
|
#8
|
|
Member
Registered: Oct 2011
Location: Portugal.
Distribution: Debian squeeze - Fluxbox.
Posts: 109
Original Poster
Rep:
|
Thankyou
Thankyou Sir Williams.
So foo and bar are just general purpose variable names?
|
|
|
|
11-25-2011, 06:35 AM
|
#9
|
|
Member
Registered: Oct 2011
Location: Portugal.
Distribution: Debian squeeze - Fluxbox.
Posts: 109
Original Poster
Rep:
|
recursive
Sir Williams, is that a recursive while cycle?
|
|
|
|
11-25-2011, 06:40 AM
|
#10
|
|
Senior Member
Registered: Apr 2007
Location: Portland, OR
Distribution: Debian, Android, LFS
Posts: 1,167
|
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.
|
11-25-2011, 06:42 AM
|
#11
|
|
Member
Registered: Oct 2011
Location: Portugal.
Distribution: Debian squeeze - Fluxbox.
Posts: 109
Original Poster
Rep:
|
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
|
|
|
|
11-25-2011, 06:45 AM
|
#12
|
|
Member
Registered: Oct 2011
Location: Portugal.
Distribution: Debian squeeze - Fluxbox.
Posts: 109
Original Poster
Rep:
|
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
|
|
|
|
11-25-2011, 06:50 AM
|
#13
|
|
Senior Member
Registered: Apr 2007
Location: Portland, OR
Distribution: Debian, Android, LFS
Posts: 1,167
|
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 06:51 AM.
|
|
|
1 members found this post helpful.
|
11-25-2011, 06:54 AM
|
#14
|
|
Member
Registered: Oct 2011
Location: Portugal.
Distribution: Debian squeeze - Fluxbox.
Posts: 109
Original Poster
Rep:
|
thankyou
Yes I forgot, thankyou =)
|
|
|
|
11-25-2011, 07:14 AM
|
#15
|
|
Member
Registered: Oct 2011
Location: Portugal.
Distribution: Debian squeeze - Fluxbox.
Posts: 109
Original Poster
Rep:
|
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?
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 08:40 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|