[SOLVED] How to make several "renames" by inoking a single command? Make a shell script file?
Linux - NewbieThis 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.
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.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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?
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.
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
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.