grep命令指定文件名或目录: grep '关键字' 文件名或目录 例如,要在/home/user/documents目录下的所有PDF文件中查找包含关键字"example"的行,可以使用以下命令: grep 'example' /home/user/documents/*.pdf
使用通配符搜索: grep '关键字' * 例如,要在当前目录下搜索包含关键字"example"的所有文件,可以使用以下命令: grep 'example' *
在多个文件中搜索: grep '关键字' 文件1 文件2 文件3 例如,要在file1.txt和file2.txt文件中查找包含关键字"example"的行,可以使用以下命令: grep 'example' file1.txt file2.txt
递归搜索子目录: grep -r '关键字' 目录名 例如,要在/home/user/documents目录及其所有子目录下的所有文件中查找包含关键字"example"的行,可以使用以下命令: grep -r 'example' /home/user/documents/
忽略大小写: grep -i '关键字' 文件名或目录 例如,要在file1.txt和file2.txt文件中查找包含关键字"Example"(不区分大小写)的行,可以使用以下命令: grep -i 'Example' file1.txt file2.txt
显示行号: grep -n '关键字' 文件名或目录 例如,要在file1.txt和file2.txt文件中查找包含关键字"example"的行并显示行号,可以使用以下命令: grep -n 'example' file1.txt file2.txt
高亮显示匹配项: grep --color=always '关键字' 文件名或目录 例如,要在file1.txt和file2.txt文件中查找包含关键字"example"的行并高亮显示匹配项,可以使用以下命令: grep --color=always 'example' file1.txt file2.txt
这些是一些常用的grep命令选项,可以帮助您根据需要定制关键字查找范围。