排除 . 和 .. 来自 find 和 ls 的结果

Jos*_*osh 6 ls zsh find

有什么办法来防止findls -l从上市...在他们的结果?我从不关心在输出中看到这个结果,它阻止我有效地将他们的输出输送wc -l到准确地计算事物。

万一重要,我在zsh.

mul*_*laz 7

对于ls,使用-A代替-a

来自man ls

   -A, --almost-all
          do not list implied . and ..
Run Code Online (Sandbox Code Playgroud)


Sté*_*las 4

zsh

count() echo $#
count *        # non-hidden files (all types)
count *(D)     # files (all types)
count **/*(D)  # files recursively (all types)
count **/*(D/)  # directories only (recursively)
Run Code Online (Sandbox Code Playgroud)

zshglob 永远不会包含.,甚至在启用时也不会包含(例如使用..globbing限定符))。dotglob(D)

为了避免在没有文件时出现错误消息,请添加Nglobbing 限定符:

count *(ND)
Run Code Online (Sandbox Code Playgroud)

如果没有匹配,这会导致 glob 扩展为空(没有参数,不是空参数)。

请注意,由于换行符与文件名中的任何字符一样有效,因此管道输出lsfindtowc -l是不正确的。

POSIXly,你可以计算/字符而不是换行符:

find . ! -name . -prune -print | grep -c /
Run Code Online (Sandbox Code Playgroud)

或者递归地使用这个技巧:

find .//. ! -name . -prune -print | grep -c //
Run Code Online (Sandbox Code Playgroud)