因此,我有一个命令,如:grep "\"tool\":\"SEETEST\"" * -l,它非常独立-它打印出当前目录中为所选工具生成的JSON文件列表。
但是,如果我把它输到像这样的xargs那里:grep "\"tool\":\"SEETEST\"" * -l | xargs ls -lSh
它打印当前目录中的所有文件!
如何使它只打印匹配的文件名并将它们输送到按大小排序的ls?
发布于 2018-06-18 15:18:02
如果xargs没有匹配项,那么它将列出当前目录中的所有文件:
#----------- current files in the directory
mortiz@florida:~/Documents/projects/bash/test$ ls -ltr
total 8
-rw-r--r-- 1 mortiz mortiz 585 Jun 18 12:13 json.example2
-rw-r--r-- 1 mortiz mortiz 574 Jun 18 12:14 json.example
#----------- using your command
mortiz@florida:~/Documents/projects/bash/test$ grep "\"title\": \"example\"" * -l
json.example
#-----------adding xargs to the previous command
mortiz@florida:~/Documents/projects/bash/test$ grep "\"title\": \"example\"" * -l | xargs ls -lSh
-rw-r--r-- 1 mortiz mortiz 574 Jun 18 12:14 json.example
#-----------adding purposely an error on "title"
mortiz@florida:~/Documents/projects/bash/test$ grep "\"titleo\": \"example\"" * -l | xargs ls -lSh
total 8.0K
-rw-r--r-- 1 mortiz mortiz 585 Jun 18 12:13 json.example2
-rw-r--r-- 1 mortiz mortiz 574 Jun 18 12:14 json.example如果希望使用xargs,而grep没有返回任何匹配项,则添加"-r | --no-run-if-empty"以防止xargs列出当前目录中的所有文件:
grep "\"titleo\": \"example\"" * -l | xargs -r ls -lShhttps://stackoverflow.com/questions/50911818
复制相似问题