Tim Dunphy
2016-Feb-03 17:37 UTC
[CentOS] delete directories with find and exclude other directories
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 I'm just tailing the output so that you have an idea of what's going on without taking up the whole page. :) If I try to exlclude the logs directory with the prune command I get back no results. root at ops-manager:/tmp/tmp# find . -type d -prune -o -name 'logs' -print root at ops-manager:/tmp# What am I doing wrong? Thanks, Tim -- GPG me!! gpg --keyserver pool.sks-keyservers.net --recv-keys F186197B
Valeri Galtsev
2016-Feb-03 17:58 UTC
[CentOS] delete directories with find and exclude other directories
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-1yf9lnccrude 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 Thanks. Valeri> > I'm just tailing the output so that you have an idea of what's going on > without taking up the whole page. :) > > If I try to exlclude the logs directory with the prune command I get back > no results. > > root at ops-manager:/tmp/tmp# find . -type d -prune -o -name 'logs' -print > root at ops-manager:/tmp# > > What am I doing wrong? > > Thanks, > Tim > > -- > GPG me!! > > gpg --keyserver pool.sks-keyservers.net --recv-keys F186197B > _______________________________________________ > CentOS mailing list > CentOS at centos.org > https://lists.centos.org/mailman/listinfo/centos >++++++++++++++++++++++++++++++++++++++++ Valeri Galtsev Sr System Administrator Department of Astronomy and Astrophysics Kavli Institute for Cosmological Physics University of Chicago Phone: 773-702-4247 ++++++++++++++++++++++++++++++++++++++++
m.roth at 5-cent.us
2016-Feb-03 18:11 UTC
[CentOS] delete directories with find and exclude other directories
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 > > I'm just tailing the output so that you have an idea of what's going on > without taking up the whole page. :) > > If I try to exlclude the logs directory with the prune command I get back > no results. > > root at ops-manager:/tmp/tmp# find . -type d -prune -o -name 'logs' -print > root at ops-manager:/tmp# > > What am I doing wrong? >find . -type d ! -name logs -prune (and -print has been a default for a long time). mark
Gordon Messmer
2016-Feb-03 18:25 UTC
[CentOS] delete directories with find and exclude other directories
On 02/03/2016 09:37 AM, Tim Dunphy wrote:> If I try to exlclude the logs directory with the prune command I get back > no results. > > root at ops-manager:/tmp/tmp# find . -type d -prune -o -name 'logs' -print > > What am I doing wrong?You're not applying the prune command to items named logs, for one. :) find . -name logs -prune -o -type d -print find will crawl the directory. Items named logs will not be examined further. Otherwise, if the item is a directory, its name will be printed.
Gordon Messmer
2016-Feb-03 18:28 UTC
[CentOS] delete directories with find and exclude other directories
On 02/03/2016 10:11 AM, m.roth at 5-cent.us wrote:> find . -type d ! -name logs -pruneThat will prune all of the directories whose name is not "logs", starting with "." So... not terribly useful.
Chris Beattie
2016-Feb-03 19:11 UTC
[CentOS] delete directories with find and exclude other directories
On 2/3/2016 12:37 PM, Tim Dunphy wrote:> I'm attempting to delete some directories and I want to be able to exclude > a directory called 'logs' from being deleted.Since you can't have a file and a directory named "logs" in the same directory at the same time (that I know of), you could turn on bash's extended globbing. $ shopt -s extglob $ rm -rf !(logs) That will only preserve the top-level entity named logs, though. If there's a "logs" in a subdirectory, it'll get deleted. -- -Chris
Denniston, Todd A CIV NAVSURFWARCENDIV Crane
2016-Feb-04 14:33 UTC
[CentOS] delete directories with find and exclude other directories
________________________________________ 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-1yf9lnccrude 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.