LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 12-27-2011, 04:03 PM   #1
latimer
LQ Newbie
 
Registered: Dec 2011
Posts: 2

Rep: Reputation: Disabled
bash - Delete Directories That Don't Match a Certain Name


I am trying to making a cron job that deletes all directories and their contents in a certain directory which don't have a specific name. I am using a binary which generates a bunch of timestamped folders in /var/run/amplify/ and there is no way to turn it off. There is a folder called "temp" in /var/run/amplify/ that needs to be kept, but all the other directories and their contents I want to be wiped by the cron daily. How would I go about doing this? Thanks!
 
Old 12-27-2011, 04:06 PM   #2
AlucardZero
Senior Member
 
Registered: May 2006
Location: USA
Distribution: Debian
Posts: 4,824

Rep: Reputation: 615Reputation: 615Reputation: 615Reputation: 615Reputation: 615Reputation: 615
Use the find command
 
Old 12-27-2011, 04:13 PM   #3
latimer
LQ Newbie
 
Registered: Dec 2011
Posts: 2

Original Poster
Rep: Reputation: Disabled
How do I find the opposite? I tried find /var/run/amplify/ -name ! temp and find !(/var/run/amplify/ -name temp) but neither worked.
 
Old 12-27-2011, 07:29 PM   #4
Telengard
Member
 
Registered: Apr 2007
Location: USA
Distribution: Kubuntu 8.04
Posts: 579
Blog Entries: 8

Rep: Reputation: 148Reputation: 148
This works in Bash if extglob is enabled.

Code:
blah$ ls
apples  grapes  oranges  peaches  penguins
blah$ rm -R !(penguins) #protect the penguins
blah$ ls
penguins
blah$
http://www.gnu.org/software/bash/man...ttern-Matching

Quote:
Originally Posted by Pattern Matching - Bash Reference Manual
!(pattern-list)
Matches anything except one of the given patterns.
From your post it looks like you're treating the symptom, and not the problem. Hope you can find some way to tame that bad binary.
 
Old 12-27-2011, 07:57 PM   #5
Telengard
Member
 
Registered: Apr 2007
Location: USA
Distribution: Kubuntu 8.04
Posts: 579
Blog Entries: 8

Rep: Reputation: 148Reputation: 148
Quote:
Originally Posted by latimer View Post
How do I find the opposite? I tried find /var/run/amplify/ -name ! temp and find !(/var/run/amplify/ -name temp) but neither worked.
Try this:

Code:
find /var/run/amplify/ \! -name temp
This will find all files in and below the /var/run/amplify/ directory which are not named temp. If you only want directories, then try this:

Code:
find /var/run/amplify/ -type d \! -name temp
By default, find will recurse into all sub-directories of /var/run/amplify/. If you want to limit find to only act on names in /var/run/amplify/, then try this:

Code:
find /var/run/amplify/ -maxdepth 1 -type d \! -name temp
For the record, I would not want something like the running automatically. I don't recommend putting this in cron.
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
delete lines between match csegau Programming 15 05-16-2011 11:25 AM
Match all files except directories ? Vilius Linux - Software 1 02-09-2009 02:49 AM
How to delete the file using pattern match? nishanthhampali Programming 3 04-16-2008 12:50 PM
delete file that match the content packets Programming 5 04-03-2007 02:47 PM
Tried to delete file as root but it says I don't have permission to delete it! beejayzed Mandriva 23 03-12-2004 02:46 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 11:01 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