我想列出使用Linux命令包含字符串的文件的名称。
使用egrep时,会显示字符串的所有实例,这可能是倍数。
例如:
egrep disco music_file*.txt
可能会显示;
music_file1.txt: blah blah blah disco stuff
music_file1.txt: disco blah blah blah
music_file1.txt: I like to listen to italodisco
music_file2.txt: I like to party to disco
music_file3.txt: Does your dog like disco?
我想要的只是:
music_file1.txt
music_file2.txt
music_file3.txt
问题: 如何在Linux中搜索字符串时显示每个文件名的一个实例?
答案 0 :(得分:6)
将-l
添加到您的egrep表达式中:
egrep -l disco music_file*.txt
来自man grep
:
-l, - with-matches-matches
抑制正常输出;而是打印每个输入文件的名称 通常会打印哪个输出。扫描将停止 在第一场比赛。 (-l由POSIX指定。)
答案 1 :(得分:2)
grep -l应该适合你。
grep -l pattern files*.txt
从手册页:
-l, --files-with-matches
Suppress normal output; instead print the name of each input
file from which output would normally have been printed. The
scanning will stop on the first match. (-l is specified by
POSIX.)