LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Script help (https://www.linuxquestions.org/questions/programming-9/script-help-837469/)

snowape 10-11-2010 03:17 PM

Script help
 
Hello all, I need your help once again....

I need to write a script which will delete all the previous directories I have created.

I have found out how to remove one directory with the following code:
find /media/CE40-EB9D -type d -name "week1" -exec rm -rf {} \;

but how can I get the same script to remove weeks 2-5? Thanks again ofr the help

David the H. 10-11-2010 03:53 PM

You can use ranges in your search patterns. Also, gnu find has a built-in delete option. No need to use -exec to call on the external rm.
Code:

find /media/CE40-EB9D -type d -name "week[1-5]" -delete
Be sure to test the output though, run it with -print instead before using -delete.

Learning how to really use the power of find is highly recommended when scripting: http://www.grymoire.com/Unix/Find.html

(Note that this guide isn't specific to gnu find, so some minor points may be slightly different, and some gnu features aren't mentioned, but it's a good general tutorial.)


Edit: One more friendly suggestion, this one about your post. Please try to make the title more specific, so that it communicates your actual problem, instead of just a general "help me". Also, please use [code][/code] around commands and code blocks, to preserve formatting and to improve readability.

snowape 10-11-2010 04:08 PM

thanks a lot, I think this is what I needed. So I typed up the script and saved it as a .scr, but how can I test this to run from terminal in Ubuntu? For example I saved it in my /media dir, but I tried to bash filename.scr and tried to sh filename.scr, both came back with no such file or dir

Fred_mike 10-11-2010 04:51 PM

you need to be in the same directory as the file you are trying to run. type " cd blah blah/media " , then run " filename.scr " or " sh filename.scr".
at least i think so, i am a noob.

David the H. 10-11-2010 04:59 PM

You didn't just place that command as-is into a file, did you? That probably won't do you much good. Executing it would only do the same thing as running the command itself. You'd have to build in a variable substitution or two at the least in order for it to accept external input.

You also have to change permissions to make the script executable. And the shell must know where to look when launching it. So the directory it's in must be either set in your PATH environment variable, or else the command name must include the path to the file (absolute or relative). ./command is the usual way to call executables from the current directory, for example.

Also, the traditional file extension for shell scripts is .sh, not .scr.

But it's not our job to teach you everything about scripting. If you need help on the basics, go to linuxcommand.org, or the bash guide for beginners, and start studying.

snowape 10-13-2010 11:45 AM

I understand its not your job to teach me.... I also understand its not "common" practice to name a script .scr and to name it .sh My instructor wanted us to name is .scr for what ever reason, I thought it odd as when I tried to write the script in Windows and saved the file as .scr it saved the file as a screenshot.

But I do thank you for the information, it was a great help. I come here because not all the books and online resources are up to date and I prefer using a resource which is up to date.


All times are GMT -5. The time now is 02:14 PM.