我喜欢马丁·贝克特(Martin Beckett)的解决方案,但发现带有空格的文件名可能会使它崩溃(例如谁在文件名中使用空格pfft:D)。 我还想查看匹配的内容,所以我将匹配的文件移动到本地文件夹,而不是仅使用“ rm”命令删除它们:

# Make a folder in the current directory to put the matched files

$ mkdir -p './matched-files'

# Create a script to move files that match the grep

# NOTE: Remove "-name '*.txt'" to allow all file extensions to be searched.

# NOTE: Edit the grep argument 'something' to what you want to search for.

$ find . -name '*.txt' -print0 | xargs -0 grep -al 'something' | awk -F '\n' '{ print "mv \""$0"\" ./matched-files" }' > doit.sh

Or because its possible (in Linux, idk about other OS's) to have newlines in a file name you can use this longer, untested if works better (who puts newlines in filenames? pfft :D), version:

$ find . -name '*.txt' -print0 | xargs -0 grep -alZ 'something' | awk -F '\0' '{ for (x=1; x doit.sh

# Evaluate the file following the 'source' command as a list of commands executed in the current context:

$ source doit.sh

注意:我遇到grep无法在具有utf-16编码的文件内匹配的问题。解决方法请参见此处。 如果网站消失了,您可以使用grep的-a标志,该标志使grep将文件视为文本,并使用与每个扩展字符中的任何第一个字节匹配的正则表达式模式。 例如,要匹配Entité,请执行以下操作:

grep -a 'Entit.e'

如果这不起作用,请尝试以下操作:

grep -a 'E.n.t.i.t.e'

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐