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@ops-manager:/tmp/tmp# find . -type d -prune -o -name 'logs' -print root@ops-manager:/tmp#
What am I doing wrong?
Thanks, Tim
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
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@ops-manager:/tmp/tmp# find . -type d -prune -o -name 'logs' -print root@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@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 ++++++++++++++++++++++++++++++++++++++++
________________________________________ 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.
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@ops-manager:/tmp/tmp# find . -type d -prune -o -name 'logs' -print root@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 wrote:
On 02/03/2016 10:11 AM, m.roth@5-cent.us wrote:
find . -type d ! -name logs -prune
That will prune all of the directories whose name is not "logs", starting with "."
So... not terribly useful.
Right, but a) I think I tried using prune 20 years ago... and b) I thought the o/p wanted to not deal with any directory whose name was logs. leaving off prune would get everything, which is perhaps a bit more useful.
mark
On 02/03/2016 10:51 AM, m.roth@5-cent.us wrote:
Right, but a) I think I tried using prune 20 years ago... and b) I thought the o/p wanted to not deal with any directory whose name was logs. leaving off prune would get everything, which is perhaps a bit more useful.
I think you don't understand. I was pointing out that the command you specified would print the name '.' and that is all. It won't descend through '.' because you told it to prune all directories not named "logs". That is, I'm trying to point out that it's your *logic* that's flawed.
OP was right in his thinking. The correct way to approach the problem is to ignore (prune) the logs dir, and then to do something with the remaining 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@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.
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.