Need bash script!!!
Hi!
Need a bash script that will be run with input parameters <directory>,<current_suffix>,<needed_suffix> and will change all filenames suffixes in this diretory and all subdirectories. |
So go a head an write it. :p
Check out the bash introduction and advance bash scripting tutorials on tldp.org. The one you want should not be that hard to do even if you don't have experience with bash scripting. I think the script you want is even in the bash intdocution tutorial. Greets zhjim |
Hi ! I'm sure many others will tell you that your question sounds very "homeworkish" :tisk: ;) The way to make the most out of the forum is to attempt to tackle the problem and post where you think you are going wrong. The folk here will always try to nudge you in the right direction but at the same time making you fathom out the problem yourself. That way you will actually find the solution yourself and understand how you got there thus giving you a better understanding of the OS. As the previous poster said, the solution is quite straightforward all you have to do is to read a simple bash tutorial.
Apologies for being so harsh ;) But as you can see non-homeworkish problems are very swiftly answered so people on the forum are not bad they just think in the way which would benefit us all. If your problem isn't homework related then please accept my profound apologies. Cheers, Uncle ps: Many many many moons ago I got told off on the Usenet for posting a similarly versed question ! When I sat down with some text the problem became quite straightforward to solve !! |
The "Parameter Expansion" section of the bash manual page will explain why this might be part of your solution:
Code:
nn="${on%$oe}$ne" # o and n mean old and new, you can guess the rest. |
Okay, guys..;) I understand your position in that question. I sniffed through advanced bash guide,a s was advised above, and found that way of solving my problem:
#!/usr/local/bin/bash ARGS=2 BAD_ARGS=65 if [ $# -ne "$ARGS" ] then echo "using order: `basename $0` old_suffix new_suffix" exit $BAD_ARGS fi for filename in *.$1 do mv $filename ${filename%$1}$2 done exit 0 But this works ony in the current directory, while I need some recursive work over subdirectories also. This time I faced the problem. Can I use such construction? : #!/bin/bash ARGS=2 BAD_ARGS=65 if [ $# -ne "$ARGS" ] then echo "using order: `basename $0` old_suffix new_suffix" exit $BAD_ARGS fi function change_suffix() { for filename in *.$1 do mv $filename ${filename%$1}$2 done } DIRS=$(find . -type d); for DIR in ${DIRS} do if [ -d "${DIR}" ]; then (cd $DIR;change_suffix; cd -;) fi done exit 0 |
Use [CODE] tags to format your script nicely in the forum.
You can use the find command to recurse down sub-directories. find is a wonderful tool, but it's utterly confusing to everyone for at least 6 months. :D find will print out a list of files / directories (depending on the options used), which you can then recurse over in the shell with "while read filename; do ... done" style loop. Anything which creates a list of stuff with one item per line can be piped into such a loop, e.g. Code:
locate ht-fonts/unicode |while read f; do echo "we found $f"; done |
Daleo your getting there. Not quite able to help your there but for testing just use some echo command inside your function.
Oh man matthew your are pulling trick that are even new to me *thumbs up* (or might it that i don't get your crytion), but I think its not very wise to say something about the find command and then give an example with the locate command. Just for the fun. Is there a way do to it without find. solely with ls -R. Greets zhjim |
Quote:
Quote:
|
Quote:
So you would need to pass the original commandline arguments as an argument to the function. Also your way of cd'ing to the directorys does not seem right to me. Try to build a function that takes a directory as an argument and have it call it self if it finds another dir else just change suffix. I have this function here somewhere. ... Somewhere. You would also need to build a complete path or cd somehow. Options are yours. Or just use find. Exclamationmark. Point. Point. Quote:
Beside i never realy got into find till I knew that it was part of lpi 102 exam and capabale of showing/finding acl's. Also while trying to get a grip on find I did not find it to hard to use it. But truly spoken it depend on the job you want to be done Best Regards zhjim |
All times are GMT -5. The time now is 07:40 AM. |