LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-11-2010, 03:17 PM   #1
snowape
LQ Newbie
 
Registered: Sep 2010
Posts: 9

Rep: Reputation: 0
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
 
Old 10-11-2010, 03:53 PM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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.

Last edited by David the H.; 10-11-2010 at 03:59 PM. Reason: as stated
 
1 members found this post helpful.
Old 10-11-2010, 04:08 PM   #3
snowape
LQ Newbie
 
Registered: Sep 2010
Posts: 9

Original Poster
Rep: Reputation: 0
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
 
Old 10-11-2010, 04:51 PM   #4
Fred_mike
Member
 
Registered: Oct 2010
Posts: 95

Rep: Reputation: Disabled
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.

Last edited by Fred_mike; 10-11-2010 at 04:53 PM.
 
Old 10-11-2010, 04:59 PM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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.
 
Old 10-13-2010, 11:45 AM   #6
snowape
LQ Newbie
 
Registered: Sep 2010
Posts: 9

Original Poster
Rep: Reputation: 0
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash script problem with ftp session exiting the script early edomingox Programming 5 02-23-2010 05:39 AM
How to get full path to script file inside script itself? And in case of sym links? maggus Linux - Newbie 3 05-28-2009 08:40 AM
ssh - using variables in call to start remote script from local script babag Linux - Networking 2 06-03-2008 04:50 PM
set variables in a bash script; ansi PS1 color script donnied Programming 4 11-21-2007 11:33 AM
Iptables (with masq) troubleshooting, very simple script attached script and logs. xinu Linux - Networking 13 11-01-2007 04:19 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:42 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration