我想获得多个文件的最后10行。 我知道他们都以“-access_log”结尾。 所以我试了一下:
tail -10 *-access_log
但这给了我一个错误,其中:
tail -10 file-*
给我我期望的输出。 我认为这可能与BASH更接近尾巴。 然而,命令如:
cat *-access_log
工作正常。
有什么build议么?
我相信你会想要:
tail -n 10 *-access.log
至于为什么 :
我不认为这与globbing有什么关系:
tail -10 foo-access.log arf-access.log tail: option used in invalid context -- 1
我认为只是碰巧你的glob扩展到一个文件。 它可能与一些古老的parsing选项有关,我懒得去读,但是如果你真的想知道在coreutils源文件的tail.c中看看,并剖析下面的函数:
parse_obsolete_option (int argc, char * const *argv, uintmax_t *n_units)
虽然有点老,这个问题仍然是相关的。 我遇到了类似的问题
ssh myserver.com 'tail -2 file-header*'
那给了我错误
tail:在无效的情况下使用的选项 – 2
但是,只能拖尾一个文件
ssh myserver.com 'tail -2 file-header-file-one'
工作正常。 查看tail.c的源代码,显示tail通过parsing过时的选项开始,然后parsing剩下的(即尚未处理的选项),常规选项。 但是, parse_obsolete_option()需要一个“过时的”用法,只有一个文件作为参数。
所以当提供更多的文件时,函数立即返回,并让正则parsing器在-2上窒息(期待-n 2 )。
/* With the obsolete form, there is one option string and at most one file argument. Watch out for "-" and "--", though. */ if (! (argc == 2 || (argc == 3 && ! (argv[2][0] == '-' && argv[2][1])) || (3 <= argc && argc <= 4 && STREQ (argv[2], "--")))) return false;
总之,最好总是使用-n常规的forms,知道“过时的”代码只接受一个文件。