The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
shell script to delete directories... Stephan Shell Programming and Scripting 8 05-18-2008 08:00 AM
How to delete files in UNIX using shell script theguy16 Shell Programming and Scripting 7 04-09-2008 01:40 AM
shell script for delete old files krishnarao Shell Programming and Scripting 1 07-07-2006 07:21 AM
need some help with a script to delete directories. centrino Shell Programming and Scripting 1 12-12-2005 06:33 PM
Need Help: Delete a file by Shell Script r3edi Shell Programming and Scripting 5 07-11-2005 04:13 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 05-15-2008
Registered User
 

Join Date: Sep 2005
Posts: 16
shell script to delete directories...

Hi.

I'm trying to write a script that will delete all directories found, that are not named as a "number" (year)...

here is what i mean, let's say i have within /data/exports the following directories:

/data/exports/2000
/data/exports/2001
/data/exports/2002

/data/exports/daily/2000
/data/exports/monthly/2000

/data/exports/daily/2001
/data/exports/monthly/2001

/data/exports/daily/2002
/data/exports/monthly/2002

/data/exports/blahblah/something/another/etc

and so on...

I want to write a script, that will delete all directories within /data/exports/ but not the 2000,2001 and 2002 found at that first level. The 200x found within daily and etc i want gone though.

So i thought about writing up a script that would list all directories within /data/exports/ and those that are not numbers, do a rm -R on it...but i can't seem to get it right...

any thoughts, help appreciated.

Thanks.
Reply With Quote
Forum Sponsor
  #2  
Old 05-15-2008
 

Join Date: Nov 2007
Location: 45.48-73.63
Posts: 736
You want? ... and what did you do to resolve the problem?
man find can help you.
Reply With Quote
  #3  
Old 05-15-2008
Yogesh Sawant's Avatar
Part Time Moderator and Full Time Dad
 

Join Date: Sep 2006
Location: Rossem, Tazenda
Posts: 791
particularly check the -type, -name and -exec options of find command
Reply With Quote
  #4  
Old 05-16-2008
Registered User
 

Join Date: Sep 2005
Posts: 16
thanks danmero?

Yogesh, i've been using the find command along with the -type d option which works fine, but i need to figure out a way to only rmdir the directories that are not numerical years. Is there a way, to filter out the directories named 2000, 2001 etc and delete all the rest? I can't seem to get it working...
Technically the way i thought of doing it was using a if "not numerical" then rmdir -fR ? But how do i write this "if"?
Reply With Quote
  #5  
Old 05-16-2008
era era is offline
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,650
Code:
find . -type d |
grep -v '[0-9][^/*]$' |
xargs rm
The grep removes any line containing a number in the last component of the path name. Maybe you should run it with "xargs echo rm" first just to see that it does what you want.

You could also play around with find -type d \! -name '*[0-9]*'
Reply With Quote
  #6  
Old 05-16-2008
Registered User
 

Join Date: Sep 2005
Posts: 16
Thank you Era! that is what i was looking for! Really appreciate your contribution.

This should work just fine!
Reply With Quote
Google The UNIX and Linux Forums
Reply

Tags
find

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 05:45 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0