LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   bash - Delete Directories That Don't Match a Certain Name (https://www.linuxquestions.org/questions/linux-general-1/bash-delete-directories-that-dont-match-a-certain-name-920777/)

latimer 12-27-2011 04:03 PM

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!

AlucardZero 12-27-2011 04:06 PM

Use the find command

latimer 12-27-2011 04:13 PM

How do I find the opposite? I tried find /var/run/amplify/ -name ! temp and find !(/var/run/amplify/ -name temp) but neither worked.

Telengard 12-27-2011 07:29 PM

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. :(

Telengard 12-27-2011 07:57 PM

Quote:

Originally Posted by latimer (Post 4559581)
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.


All times are GMT -5. The time now is 01:12 AM.