Las*_*all 27 command-line bash ls pipe
当我输入以下内容时:
find . -name *foo* | ls -lah
Run Code Online (Sandbox Code Playgroud)
它返回与普通ls
命令相同的结果,就好像它没有输入一样。
然而:
ls -lah $( find . -name *foo* )
Run Code Online (Sandbox Code Playgroud)
效果很好,但只有当find
命令有结果时。
是否可以通过管道传输ls
?
Pri*_*ley 35
您可以-exec
与find
命令一起使用。
find . -name '*foo*' -exec ls -lah {} \;
Run Code Online (Sandbox Code Playgroud)
小智 23
find . -name *foo* | xargs -r ls -lah
Run Code Online (Sandbox Code Playgroud)
那应该工作。
这适用于带有空格或异常字符的文件名,并且ls
可以对所有文件进行排序:
find . -name *foo* -print0 | xargs -0 ls -lah
Run Code Online (Sandbox Code Playgroud)
-print0
意味着诸如此类的文件file foo 1
名将获得输出,find
后跟空值。“-0”参数xargs
告诉它期待这种输入,因此带空格的文件名可以ls
正确地通过管道传输到命令。
该xargs
工程是在某些方面优于find etc -exec ls {} +
因为所有的文件名送送ls
一次,所以如果你想通过时间戳对它们进行排序的所有(使用ls
),像这样的作品:
find . -iname *pdf -print0 | xargs -0 ls -ltr
Run Code Online (Sandbox Code Playgroud)
在 NetBSD 系统上,“-printx”也是一个选项(这对我来说似乎是一个有用的论点,但无论如何,我们有 xargs -0 并且没关系):
find . -name *foo* -printx | xargs ls -lah` # not for Ubuntu
Run Code Online (Sandbox Code Playgroud)