【问题标题】:Find directories that doesn't contain "*.sql" files查找不包含“*.sql”文件的目录
【发布时间】:2015-10-27 13:53:08
【问题描述】:

我有几个目录,如下所示。我想列出没有 SQL 文件的目录。例如下面示例中的“dev.mysite.com”。我使用的是 ubuntu 14.04 LTS

orange.com/
    orange.com.sql
    10.10.10.1/public_html/...

apple.edu.us/
    apple.edu.sql
    10.10.10.2/public_html/...

dev.mysite.com/
    10.10.10.3/public_html/

example.com/
    mysql_dbdump20150911.sql
    10.10.11.11/public_html/...

我尝试使用带有“cut”和“xargs”的“find”来实现这一点,并将这些目录移动到“dirwithsql”目录,并将剩余的目录作为没有 sql 文件的目录手动。

find . -maxdepth 2 -iname "*.sql" | cut -d'/' -f 2 | xargs -n 1 -I {} mv {} /backup/dirwithsql/{}

我试过了

find -maxdepth 2 ! -iname "*.sql" -exec dirname {} \;

但上面的命令显示所有目录

有没有更好的方法? 谢谢

【问题讨论】:

    标签: linux bash find


    【解决方案1】:

    我最近遇到了类似的问题,这对我有用

    for f in $(find . -type d -maxdepth 2); do if [[ $(ls -1 $f | grep '.sql$'|wc -l) == 0 ]] ; then echo $f; fi; done
    

    【讨论】:

    • 谢谢 :) 它有效,但我需要掌握这些表达方式
    【解决方案2】:
    find . -type d -print0 | while read -d $'\0' n; do [ -f "$n"/*.sql ] || echo $n; done
    

    即使使用奇异的目录名称(空格、换行符...)也可以工作

    【讨论】:

      猜你喜欢
      • 2015-04-06
      • 1970-01-01
      • 2016-07-12
      • 1970-01-01
      • 2015-03-22
      • 2019-12-09
      • 2012-09-24
      • 2014-09-04
      • 2013-07-06
      相关资源
      最近更新 更多