【发布时间】:2013-09-16 06:53:03
【问题描述】:
我想对该目录中的所有文件运行 tail -f 命令,但该目录中的一个文件除外。有人可以建议我一种方法吗?谢谢。
【问题讨论】:
-
@BillKarwin:这不是关于不友好的输出,而是关于从参数列表中排除单个文件;带排除的通配符。
标签: bash
我想对该目录中的所有文件运行 tail -f 命令,但该目录中的一个文件除外。有人可以建议我一种方法吗?谢谢。
【问题讨论】:
标签: bash
ls | grep -v unwanted | xargs tail -f
【讨论】:
find . -maxdepth 1 -type f -not -name 'file_to_exclude' -print0 | xargs -0 tail -f
tail -f `ls|grep -v unwanted`
This is a backtick ` in code
您还可以将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 {} +
但是对于大量文件来说,这可能会有点乏味。
【讨论】:
你可以使用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
【讨论】: