什么是find,grep和sort的正确Linux命令?

时间:2018-01-25 04:13:08

标签: shell

我正在使用find,grep和sort编写一个命令来显示包含“some-text”的所有文件的排序列表。

我无法弄清楚命令。

这是我的尝试:

$find . -type f |grep -l "some-text" | sort

但它不起作用。

2 个答案:

答案 0 :(得分:0)

您需要使用XARGS之类的内容,以便通过管道|传递的每个文件的内容可用于grep。

XARGS:将标准输入的输入转换为命令的参数

就我而言,我有文件1,2,3,它们包含单词test。这样做。

za:tmp za$ find . -type f | xargs grep -l "test" | sort
./file1.txt
./file2.txt
./file3.txt

za:tmp za$ find . -type f | xargs grep -i "test" | sort
./file1.txt:some test string  
./file2.txt:some test string 
./file3.txt:some test string 

答案 1 :(得分:0)

您可以在任何unix中使用它:

找到。 -type f -exec sh -c'grep“some text”{} / dev / null> / dev / null 2>& 1'\; -a -print 2>的/ dev / null的|排序

更优化的解决方案,仅适用于GNU-grep:

找到。 -type f -exec grep -Hq“some-text”{} \; -a -print 2>的/ dev / null的|排序