使用 grep 查找所有包含指定文本的文件
目标:本文提供一些关于如何搜索出指定目录或整个文件系统中那些包含指定单词或字符串的文件。
难度:容易
约定:
- # - 需要使用 root 权限来执行指定命令,可以直接使用 root 用户来执行也可以使用 sudo 命令
- $ - 可以使用普通用户来执行指定命令
第一个例子让我们来搜索 /etc/ 目录下所有包含 stretch 字符串的文件,但不去搜索其中的子目录:
# grep -s stretch /etc/* /etc/os-release:PRETTY_NAME="Debian GNU/Linux 9 (stretch)" /etc/os-release:VERSION="9 (stretch)"
grep 的 -s 选项会在发现不存在或者不能读取的文件时隐藏报错信息。结果显示除了文件名之外,还有包含请求字符串的行也被一起输出了。
上面案例中忽略了所有的子目录。所谓递归搜索就是指同时搜索所有的子目录。
下面的命令会在 /etc/ 及其子目录中搜索包含 stretch 字符串的文件:
# grep -R stretch /etc/* /etc/apt/sources.list:# deb cdrom:[Debian GNU/Linux testing _Stretch_ - Official Snapshot amd64 NETINST Binary-1 20170109-05:56]/ stretch main /etc/apt/sources.list:#deb cdrom:[Debian GNU/Linux testing _Stretch_ - Official Snapshot amd64 NETINST Binary-1 20170109-05:56]/ stretch main /etc/apt/sources.list:deb http://ftp.au.debian.org/debian/ stretch main /etc/apt/sources.list:deb-src http://ftp.au.debian.org/debian/ stretch main /etc/apt/sources.list:deb http://security.debian.org/debian-security stretch/updates main /etc/apt/sources.list:deb-src http://security.debian.org/debian-security stretch/updates main /etc/dictionaries-common/words:backstretch /etc/dictionaries-common/words:backstretch's /etc/dictionaries-common/words:backstretches /etc/dictionaries-common/words:homestretch /etc/dictionaries-common/words:homestretch's /etc/dictionaries-common/words:homestretches /etc/dictionaries-common/words:outstretch /etc/dictionaries-common/words:outstretched /etc/dictionaries-common/words:outstretches /etc/dictionaries-common/words:outstretching /etc/dictionaries-common/words:stretch /etc/dictionaries-common/words:stretch's /etc/dictionaries-common/words:stretched /etc/dictionaries-common/words:stretcher /etc/dictionaries-common/words:stretcher's /etc/dictionaries-common/words:stretchers /etc/dictionaries-common/words:stretches /etc/dictionaries-common/words:stretchier /etc/dictionaries-common/words:stretchiest /etc/dictionaries-common/words:stretching /etc/dictionaries-common/words:stretchy /etc/grub.d/00_header:background_image -m stretch `make_system_path_relative_to_its_root "$GRUB_BACKGROUND"` /etc/os-release:PRETTY_NAME="Debian GNU/Linux 9 (stretch)" /etc/os-release:VERSION="9 (stretch)"
上面 grep 命令的案例中列出的是所有包含字符串 stretch 的文件。也就是说包含 stretches , stretched 等内容的行也会被显示。 使用 grep 的 -w 选项会只显示包含特定单词的行:
# grep -Rw stretch /etc/* /etc/apt/sources.list:# deb cdrom:[Debian GNU/Linux testing _Stretch_ - Official Snapshot amd64 NETINST Binary-1 20170109-05:56]/ stretch main /etc/apt/sources.list:#deb cdrom:[Debian GNU/Linux testing _Stretch_ - Official Snapshot amd64 NETINST Binary-1 20170109-05:56]/ stretch main /etc/apt/sources.list:deb http://ftp.au.debian.org/debian/ stretch main /etc/apt/sources.list:deb-src http://ftp.au.debian.org/debian/ stretch main /etc/apt/sources.list:deb http://security.debian.org/debian-security stretch/updates main /etc/apt/sources.list:deb-src http://security.debian.org/debian-security stretch/updates main /etc/dictionaries-common/words:stretch /etc/dictionaries-common/words:stretch's /etc/grub.d/00_header:background_image -m stretch `make_system_path_relative_to_its_root "$GRUB_BACKGROUND"` /etc/os-release:PRETTY_NAME="Debian GNU/Linux 9 (stretch)" /etc/os-release:VERSION="9 (stretch)"
上面的命令都会产生多余的输出。下一个案例则会递归地搜索 etc 目录中包含 stretch 的文件并只输出文件名:
# grep -Rl stretch /etc/* /etc/apt/sources.list /etc/dictionaries-common/words /etc/grub.d/00_header /etc/os-release
默认情况下搜索是大小写敏感的,也就是说当搜索字符串 stretch 时只会包含大小写一致内容的文件。
通过使用 grep 的 -i 选项,grep 命令还会列出所有包含 Stretch , STRETCH , StReTcH 等内容的文件,也就是说进行的是大小写不敏感的搜索。
# grep -Ril stretch /etc/* /etc/apt/sources.list /etc/dictionaries-common/default.hash /etc/dictionaries-common/words /etc/grub.d/00_header /etc/os-release
grep 命令也可以只在指定文件中进行搜索。比如,我们可以只在配置文件(扩展名为.conf)中搜索指定的文本/字符串。 下面这个例子就会在 /etc 目录中搜索带字符串 bash 且所有扩展名为 .conf 的文件:
# grep -Ril bash /etc/*.conf OR # grep -Ril --include=\*.conf bash /etc/* /etc/adduser.conf
类似的,也可以使用 --exclude 来排除特定的文件:
# grep -Ril --exclude=\*.conf bash /etc/* /etc/alternatives/view /etc/alternatives/vim /etc/alternatives/vi /etc/alternatives/vimdiff /etc/alternatives/rvim /etc/alternatives/ex /etc/alternatives/rview /etc/bash.bashrc /etc/bash_completion.d/grub /etc/cron.daily/apt-compat /etc/cron.daily/exim4-base /etc/dictionaries-common/default.hash /etc/dictionaries-common/words /etc/inputrc /etc/passwd /etc/passwd- /etc/profile /etc/shells /etc/skel/.profile /etc/skel/.bashrc /etc/skel/.bash_logout
跟文件一样,grep 也能在搜索时排除指定目录。 使用 --exclude-dir 选项就行。
下面这个例子会搜索 /etc 目录中搜有包含字符串 stretch 的文件,但不包括 /etc/grub.d 目录下的文件:
# grep --exclude-dir=/etc/grub.d -Rwl stretch /etc/* /etc/apt/sources.list /etc/dictionaries-common/words /etc/os-release
-n 选项还会显示指定字符串所在行的行号:
# grep -Rni bash /etc/*.conf /etc/adduser.conf:6:DSHELL=/bin/bash
最后这个例子使用 -v 来列出所有不包含指定字符串的文件。
例如下面命令会搜索 /etc 目录中不包含 stretch 的所有文件:
# grep -Rlv stretch /etc/*
以上是使用 grep 查找所有包含指定文本的文件的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

热门话题

无法以 root 身份登录 MySQL 的原因主要在于权限问题、配置文件错误、密码不符、socket 文件问题或防火墙拦截。解决方法包括:检查配置文件中 bind-address 参数是否正确配置。查看 root 用户权限是否被修改或删除,并进行重置。验证密码是否准确无误,包括大小写和特殊字符。检查 socket 文件权限设置和路径。检查防火墙是否阻止了 MySQL 服务器的连接。

C语言条件编译是一种根据编译时条件选择性编译代码块的机制,入门方法有:使用#if和#else指令根据条件选择代码块。常用条件表达式包括STDC、_WIN32和linux。实战案例:根据操作系统打印不同消息。根据系统位数使用不同的数据类型。根据编译器支持不同的头文件。条件编译增强了代码的可移植性和灵活性,使其适应编译器、操作系统和CPU架构变化。

1.0.1前言这个项目(包括代码和注释)是在我自学Rust的过程中记录的。可能有不准确或表述不清的地方,还请大家谅解。如果您从中受益,那就更好了。1.0.2为什么使用RustRust可靠且高效。Rust可以取代C和C,性能相似但安全性更高,并且不需要像C和C那样频繁重新编译来检查错误。主要优点包括:内存安全(防止空指针取消引用、悬空指针和数据争用)。线程安全(确保多线程代码在执行前是安全的)。避免未定义的行为(例如,数组越界、未初始化的变量或访问已释放的内存)。Rust提供现代语言功能(例如泛型

Linux的五个基本组件是:1.内核,管理硬件资源;2.系统库,提供函数和服务;3.Shell,用户与系统交互的接口;4.文件系统,存储和组织数据;5.应用程序,利用系统资源实现功能。

MySQL启动失败的原因有多种,可以通过检查错误日志进行诊断。常见原因包括端口冲突(检查端口占用情况并修改配置)、权限问题(检查服务运行用户权限)、配置文件错误(检查参数设置)、数据目录损坏(恢复数据或重建表空间)、InnoDB表空间问题(检查ibdata1文件)、插件加载失败(检查错误日志)。解决问题时应根据错误日志进行分析,找到问题的根源,并养成定期备份数据的习惯,以预防和解决问题。

MySQL安装报错的解决方法是:1.仔细检查系统环境,确保满足MySQL的依赖库要求,不同操作系统和版本需求不同;2.认真阅读报错信息,根据提示(例如缺少库文件或权限不足)采取对应措施,例如安装依赖或使用sudo命令;3.必要时,可尝试源码安装并仔细检查编译日志,但这需要一定的Linux知识和经验。最终解决问题的关键在于仔细检查系统环境和报错信息,并参考官方文档。

MySQL无法直接在Android上运行,但可以通过以下方法间接实现:使用轻量级数据库SQLite,由Android系统自带,无需单独服务器,资源占用小,非常适合移动设备应用。远程连接MySQL服务器,通过网络连接到远程服务器上的MySQL数据库进行数据读写,但存在网络依赖性强、安全性问题和服务器成本等缺点。

C语言函数库是一个包含各种函数的工具箱,这些函数被组织在不同的库文件中。添加函数库需要通过编译器的命令行选项来指定,例如 GCC 编译器使用 -l 选项,后跟库名的缩写。如果库文件不在默认搜索路径下,则需要使用 -L 选项指定库文件路径。库有静态库和动态库之分,静态库在编译时直接链接到程序中,而动态库在运行时被加载。
