使用排除列表中查找与find目录在bash [英] Finding directories with find in bash using a exclude list

查看:290
本文介绍了使用排除列表中查找与find目录在bash的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在才想你,请阅读在此之前已经做了。

now before you think, "this has been done before" please read on.

最喜欢的人试图做你结束了硬编码脚本单行命令查找bash脚本,但最终在最后编辑的事情在接下来的几个月/年,所以常常是你希望你这样做是正确的第一次。

Like most of the people trying to do a find bash script you end up hard-coding the script to a single line command, but end up editing the thing over the following months/years so often that you wish in the end you did it right the first time.

我写一个小的备份方案,现在要做的目录备份,需要找到他们,针对需要排除directorie的列表。谈何容易。让我设置了阶段:

I am writing a little backup program right now to do backups of directories and need to find them, against a list of directorie's that needs to be excluded. Easier said than done. Let me set the stage:

#!/bin/bash
BasePath="/home/adesso/baldar"
declare -a Iggy
Iggy=( "/cgi-bin" 
    "/tmp" 
    "/test" 
    "/html" 
    "/icons" )
IggySubdomains=$(printf ",%s" "${Iggy[@]}")
IggySubdomains=${IggySubdomains:1}
echo $IggySubdomains
exit 0

现在在本月底你会得到的 / cgi-bin目录,/ tmp目录,/测试/ HTML,/图标
这证明了概念的作品,但是现在把它​​远一点,我需要用find搜索在基本路径并搜索只有一层深的所有子目录和排除阵列中的子目录的列表。 ..

Now at the end of this you get /cgi-bin,/tmp,/test,/html,/icons This proves that the concept works, but now to take it a bit further I need to use find to search the BasePath and search only one level deep for all subdirectories and exclude the list of subdirectories in the array...

如果我用手工输入这个这将是:

If I type this by hand it would be:

find /var/www/* \( -path '*/cgi-bin' -o -path '*/tmp' -o -path '*/test' -o -path '*/html' -o -path '*/icons' \) -prune -type d

和我也许应该要循环到每个子目录,做同样的...我希望你明白我的意思。

And should I maybe want to loop into each subdirectory and do the same... I hope you get my point.

所以,我所要做的似乎可能的,但我有一个有点问题,的printf%S不使用我喜欢所有这些发现-path或-o选项。这是否意味着我必须再次使用eval?

So What I am trying to do seem possible, but I have a bit of a problem, printf ",%s" doesn't like me using all those find -path or -o options. Does this mean I have to use eval again?

我想在这里使用bash的力量,而不是一些for循环。任何建设性的意见将是AP preciated。

I am trying to use the power of bash here, and not some for loop. Any constructive input would be appreciated.

推荐答案

试着这么做

find /var/www/* \( -path "${Iggy[0]}" $(printf -- '-o -path "*%s" ' "${Iggy[@]:1}") \) -prune -type d

和看看会发生什么。

编辑:添加了领先的*每一个路径在你的例子

added the leading * to each path as in your example.

和这里是一个完整的解决方案根据您的描述。

And here's a complete solution based on your description.

#!/usr/bin/env bash
basepath="/home/adesso/baldar"
ignore=("/cgi-bin" "/tmp" "/test" "/html" "/icons")

find "${basepath}" -maxdepth 1 -not \( -path "*${ignore[0]}" $(printf -- '-o -path "*%s" ' "${ignore[@]:1}") \) -not -path "${basepath}" -type d

不包括在$ $上市的基本路径子目录忽略,presuming至少有两个在$忽略(固定不是硬的)。

Subdirectories of $basepath excluding those listed in $ignore, presuming at least two in $ignore (fixing that is not hard).

这篇关于使用排除列表中查找与find目录在bash的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆