[CentOS] delete directories with find and exclude other directories

Denniston, Todd A CIV NAVSURFWARCENDIV Crane todd.denniston at navy.mil
Thu Feb 4 14:33:36 UTC 2016


________________________________________
From: Valeri Galtsev 
Sent: Wednesday, February 03, 2016 12:58 PM

On Wed, February 3, 2016 11:37 am, Tim Dunphy wrote:
> Hi all,
>
> I'm attempting to delete some directories and I want to be able to exclude
> a directory called 'logs' from being deleted.
>
> This is my basic find operation (without the exclusion)
>
> # find . -type d  |tail -10
> ./d20160124-1120-df8mfb/deployments
> ./d20160124-1120-df8mfb/releases
> ./d20160131-16993-vazqg5
> ./d20160131-16993-vazqg5/metadata
> ./d20160131-16993-vazqg5/deployments
> ./d20160131-16993-vazqg5/releases
> ./logs
> ./d20160203-27735-1tqbjh6
> ./d20160125-1120-1yccr9p
> ./d20160131-16993-1yf9lnc

crude thing I would do is:

find . -type d  | grep -v logs

, but that will also exclude other names containing "logs" it is like:

Semilogs2
logs4me

<SNIP>

#just skip the local logs dir
find . -type d -not -wholename ./logs
#skip dirs that start with /logs any where in the search
find . -type d -not -wholename \*/logs\*
#skip dirs that have log anywhere in their name, like Valeri's
find . -type d -not -wholename \*logs\*

and to actually get rid of the found _empty_dirs_ that are not logs...
find . -type d -not -wholename \*logs\* -exec rmdir {} \;
note 1: as written would have to be ran multiple times to empty deeper directory trees.
note 2: just because a logs dir is not shown/passed by the above command, does not mean that there was not one deep within a tree, so recursive removals might do more than you want. See recent UEFI thread. :)
note 3: rmdir can be replaced with your favorite destruction command, choose wisely.
note 4: I recommend when using an rm command, use a specific directory _name_ to find instead of '.', so there is _less_ chance of using it where you don't want to.

Even when this disclaimer is not here:
I am not a contracting officer. I do not have authority to make or modify the terms of any contract.






More information about the CentOS mailing list