grep -rl 'open' . --include=*.cpp
例如:搜索所有结尾是.cpp的文件内,是否包含open的字样。只要包含这个字样,就将文件名里出来,结果如下
./test/testall/file.cpp
./test/testall/shell_test.cpp
./test/daemontest/main.cpp
但是有时候只显示文件名,也不知道出现的地方到底是什么样子的,如果还有顺带查看一下那一行的内容,可以用如下命令:
grep -rn 'open' . --include=*.cpp
则,执行结果如下:
./test/testall/file.cpp:270: FILE *file = fopen(file_name.c_str(),"w");
./test/testall/file.cpp:273: printf("Can't open the file\n");
./test/testall/shell_test.cpp:29: FILE *file = fopen(file_name, "r");
./test/daemontest/main.cpp:53: openlog("daemontest",LOG_PID,LOG_USER);
./test/testall/file.cpp:273: printf("Can't open the file\n");
./test/testall/shell_test.cpp:29: FILE *file = fopen(file_name, "r");
./test/daemontest/main.cpp:53: openlog("daemontest",LOG_PID,LOG_USER);
显示了文件名,行号以及该行内容。
2)使用find命令+grep
假设搜索所有的.cpp文件是否包含'open'字符串,如果包含了,则显示该文件,命令如下:
find -name '*.cpp' -exec grep -l 'open' {} \;
则结果如下:
./test/testall/file.cpp
./test/testall/shell_test.cpp
./test/daemontest/main.cpp
./test/testall/shell_test.cpp
./test/daemontest/main.cpp
3) 搜索一个名称为 ‘*asd*’
sample :
find ./ -name '*asd*'4) 搜索一个或者更多的字样 more than one sample.
find ./ -name '*asd*' -o -name '*bsc*'
5) 使用-size选项可以通过文件大小查找文件。
查找比指定文件大的文件
1
| find ~ -size +100M |
1
| find ~ -size -100M |
find ~ -size 100M
|
没有评论:
发表评论