syn*_*ror 6 ls shell directory wildcards files
假设我有如下情况,一个典型的商用 PC 情况:
drwxr-xr-x 1 whatever whoever 3 Oct 3 16:40 invoices2009
drwxr-xr-x 1 whatever whoever 4 Oct 3 16:40 invoices2010
drwxr-xr-x 1 whatever whoever 2 Oct 3 16:40 invoices2011
-rwxr-xr-x 1 whatever whoever 440575 Oct 3 16:40 tax2010_1
-rwxr-xr-x 1 whatever whoever 461762 Oct 3 16:40 tax2010_2
-rwxr-xr-x 1 whatever whoever 609123 Oct 3 16:40 tax2010_3
Run Code Online (Sandbox Code Playgroud)
现在让我们偷懒,输入:
$ ls -l *2010*
Run Code Online (Sandbox Code Playgroud)
假设有是什么东西在invoices2010目录,按预期的方式将无法正常工作。由于目录名称也包含2010年,ls
因此也会列出invoices2010中的文件,尽管我只想列出当前目录中的文件。更有趣的是:想象一下 tax2010* 文件根本不存在,也没有示例中的那三个目录,而是其中的 50 个。是的,我已经试过了:ls
甚至不会指出哪些文件在哪个目录中,而只是自上而下列出它们,就像所有文件都驻留在当前目录中一样(除非您明确指定该-R
选项,我当然知道)
另外,我知道我也可以用 来做到这一点find
,但是还有什么方法可以用简单的单行代码来完成这项任务ls
(显然,它的语法要简单得多)?
rus*_*ush 13
看起来您的问题是“如何按模式列出文件,ls
仅排除目录”。
没有办法用 pure 来做到这一点ls
。您可以组合ls
+grep
像:
ls -ld *2010* | grep -v '^d'
Run Code Online (Sandbox Code Playgroud)
但是,最好使用find
它:
find . -maxdepth 1 -type f -name "*2010*"
Run Code Online (Sandbox Code Playgroud)