【问题标题】:Tail command -f on all files excluding a file对除文件以外的所有文件执行尾部命令 -f
【发布时间】:2013-09-16 06:53:03
【问题描述】:

我想对该目录中的所有文件运行 tail -f 命令,但该目录中的一个文件除外。有人可以建议我一种方法吗?谢谢。

【问题讨论】:

  • Tail multiple files in CentOS 的可能重复项
  • @BillKarwin:这不是关于不友好的输出,而是关于从参数列表中排除单个文件;带排除的通配符。

标签: bash


【解决方案1】:
       ls | grep -v unwanted | xargs tail -f

【讨论】:

  • 或者如果你想避开parsing the output of ls,你可以使用find . -maxdepth 1 -type f -not -name 'file_to_exclude' -print0 | xargs -0 tail -f
  • 另外,如果您对 xargs 过敏,请使用反引号语法。 tail -f `ls|grep -v unwanted`
  • @SF 你可以像这样把反引号放在三个反引号内This is a backtick ` in code
【解决方案2】:

您还可以将exec 标志与find 一起使用,为您提供一个很好的凝聚力:

find . -maxdepth 1 -type f ! -name unwanted.txt -exec tail -f {} +

如果您想深入当前目录,也可以使用-maxdepth 标志,或者如果您想递归遍历当前目录和所有子目录,则完全省略它。

您还可以使用-a 标志添加其他排除文件,如下所示:

find . -maxdepth 1 -type f ! -name unwanted.txt -a -type f ! -name unwanted2.txt -exec tail -f {} +

但是对于大量文件来说,这可能会有点乏味。

【讨论】:

    【解决方案3】:

    你可以使用bash的extended globbing,例如:

    $ shopt -s extglob
    
    $ ll
    total 20K
    drwxr-xr-x 2 foo foo 4.0K Sep 16 10:15 ./
    drwxr-xr-x 5 foo foo 4.0K Sep 16 10:14 ../
    -rw-r--r-- 1 foo foo    2 Sep 16 10:15 one
    -rw-r--r-- 1 foo foo    2 Sep 16 10:15 three
    -rw-r--r-- 1 foo foo    2 Sep 16 10:15 two
    
    $ ll !(three)
    -rw-r--r-- 1 foo foo    2 Sep 16 10:15 one
    -rw-r--r-- 1 foo foo    2 Sep 16 10:15 two
    
    $ tail *
    ==> one <==
    1
    
    ==> three <==
    3
    
    ==> two <==
    2
    
    $ tail !(three)
    ==> one <==
    1
    
    ==> two <==
    2
    

    【讨论】:

      猜你喜欢
      • 2017-04-04
      • 2013-03-24
      • 1970-01-01
      • 2012-05-18
      • 1970-01-01
      相关资源
      最近更新 更多