linux常用命令(六)

用于查找系统文件的相关命令

  • grep
  • find
  • locate

grep:查找文件中符号条件的字符串(关键词)

命令语法:grep [选项] 查找模式 [文件名]

选项 选项含义
-E 模式是一个可扩展的正则表达式
-F 模式是一组有断行符分隔的定长字符串
-P 模式是一个Perl正则表达式
-b 在输出的每一行前显示包含匹配字符串的行在文件中的字节偏移量
-c 只显示匹配行的数量
-i 比较时不区分大小写
-h 抑制输出的文件名前缀
-l 只显示匹配的文件名
-L 只显示不匹配的文件名
-n 在输出前加上匹配字符串所在行的行号(文件首行行号为1)
-v 只显示不包含匹配字符的行
-x 强制模式仅完全匹配一行
-w 强制模式仅完全匹配字词
-e<模式> 用模式来进行匹配操作
-f<文件> 从文件取得模式
-r 递归读取每个目录下的所有文件
-q 禁止一切注册输出
-I 强制认为该二进制文件没有包含任何搜索样式
-s 取消错误消息
--color 显示颜色

例子:在/etc/passwd文件中过滤出包含sy的行

[root@localhost ~]# grep sy /etc/passwd
sync:x:5:0:sync:/sbin:/bin/sync systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin 

对匹配的关键词显示颜色

[root@localhost ~]# grep --color sy /etc/passwd
sync:x:5:0:sync:/sbin:/bin/sync systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin 

显示所有以d开头的文件中包含“test“的行数据内容

[root@localhost ~]# cat d1
1
test1
[root@localhost ~]# cat d2 2 test2 [root@localhost ~]# grep "test" d* d1:test1 d2:test2 

在/root/aa文件中找出以b开头的行内容

[root@localhost ~]# cat aa
bbb
111
[root@localhost ~]# grep ^b /root/aa bbb 

在/root/kkk文件种蔬菜以le结尾的行内容

[root@localhost ~]# cat kkk
test file
test file1
123
abc
[root@localhost ~]# grep le$ /root/kkk
test file

查找sshd进程信息

[root@localhost ~]# ps -ef|grep sshd
root       7078      1  0 18:50 ? 00:00:00 /usr/sbin/sshd -D root 7642 7078 0 20:38 ? 00:00:00 sshd: root@pts/1 root 7958 7646 0 22:04 pts/1 00:00:00 grep --color=auto sshd 

find:搜索文件系统内符号条件的文件和目录

命令语法:find [命令选项] [路径] [表达式选项]

选项 选项含义
-name<文件名> 按文档名称查找文件
-perm<权限> 按照文件的权限来查找文件
-user<用户名> 按照文件德语所有者来查找文件
-group<组名> 按照文件的组群所有者来查找文件
-atime n 在过去n天内被访问过(atime)的文件,n代表数字
-amin n 在过去n分钟内被访问过(atime)的文件,n代表数字
-ctime n 在过去n天内被更改过(ctime)的文件,n代表数字
-cmin n 在过去n分钟内被更改过(ctime)的文件,n代表数字
-mtime n 在过去n天内被修改过(mtime)的文件,n代表数字
-mmin n 在过去n分钟内被修改过(mtime)的文件,n代表数字
-size n[ckMG] 查找大写为n的文件,n代表数字,c代表字节,k代表KB,M代表MB,G代表GB
-empty 查找空文件,可以是普通的文件或目录
-type<文件类型> 按文档类型查找,文件(f)、目录(d)、设备(b,c)、链接(l)等
-fstype<文件系统类型> 按照指定文件系统类型来查找文件
-nogroup 没有组群的文件
-nouser 没有用户的文件
-uid<用户UID> 按照文件的用户所有者的UID来查找文件
-gid<组群GID> 按照文件的组群所有者的GID来查找文件
-inum n 按照文件的inode号码来查找文件
-readable 匹配只读文件
-samefile<文件名> 查找和指定文件相同inode文件
-writable 匹配可写文件
-links n 按照文件链接数来查找文件,n代表数字

在查找文件时可以定义不同的文件类型

字符 含义
b 块设备文件
d 目录
c 字符设备文件
p 管道文件
l 符号链接文件
f 普通文件
s socket文件

例子:查找/boot目录下的启动菜单配置文件grub.cfg

[root@localhost ~]# find /boot name grub.cfg

查找“/"目录下所有以".conf"为扩展名的文件

[root@localhost ~]# find / -name "*.conf"

列出当前目录及其子目录下所有最近20天内更改过的文件

[root@localhost ~]# find . -ctime -20

查找/root目录中为空的文件或子目录

[root@localhost ~]# find /root -empty

查找/boot目录中查找文件类型为目录的文件

[root@localhost ~]# find /boot -type d

查找/home目录下用户所有者UID为1000的文件

[root@localhost ~]# find /home -uid 1000

查找inode号码是33582158的文件,(使用"start [文件]"命令可以查看文件inode号码)

[root@localhost ~]# find /root -inum 33582158
/root/anaconda-ks.cfg

不区分大小写查找文档jacob

root@localhost ~]# find -iname "cs.log"
./cs.log

查找计算机中2天前的当天被修改过的文档

root@localhost ~]# find / -mtime 2

查找当前目录下大于10MB的文档

[root@localhost ~]# find ./ -size +10M
./boot/initramfs-0-rescue-7ad525c9157940e3847220b1bdb8be06.img
./boot/initramfs-3.10.0-957.el7.x86_64.img 

查找当前目录下所有普通文件

[root@localhost ~]# find ./ -type f

查找计算机中tom所拥有的所有文档

[root@localhost ~]# find ./ -user tom

查找大于1MB的文件后列出文件的详细信息

[root@localhost ~]# find ./ -size +1M -exec ls -l {} \;

查找计算机中所有大于1MB的文件

[root@localhost ~]# find / -size +1M -a -type f

locate:在数据库中查找文件

使用locate命令可以通过数据库(var/lib/mlocate/mlocate.db文件)来查找文件,这个数据库每天由cron程序来建立。当创建好这个数据库后,就可以方便地搜索所需文件了,它比find命令搜索速度还要快。 命令语法:locate [选项] [范本样式]

选项 选项含义
-q 安静模式,不会显示任何错误信息
-r 使用正则表达式作为搜索的条件
-i 匹配模式是忽略区分大小写
-c 显示找到的条目数
-w 匹配完整路径名

例子:查找httpd.conf文件

[root@localhost ~]# locate
-bash: locate: command not found
[root@localhost ~]# yum -y install mlocate
Loaded plugins: fastestmirror
Determining fastest mirrors
 * base: mirrors.cn99.com
 * extras: mirrors.cn99.com
 * updates: mirrors.cn99.com
base                                                                                | 3.6 kB  00:00:00 extras | 3.4 kB 00:00:00 updates | 3.4 kB 00:00:00 (1/4): base/7/x86_64/group_gz | 166 kB 00:00:02 (2/4): extras/7/x86_64/primary_db | 205 kB 00:00:02 (3/4): base/7/x86_64/primary_db | 6.0 MB 00:00:04 (4/4): updates/7/x86_64/primary_db | 6.4 MB 00:00:04 Resolving Dependencies --> Running transaction check ---> Package mlocate.x86_64 0:0.26-8.el7 will be installed --> Finished Dependency Resolution Dependencies Resolved =========================================================================================================== Package Arch Version Repository Size =========================================================================================================== Installing: mlocate x86_64 0.26-8.el7 base 113 k Transaction Summary =========================================================================================================== Install 1 Package Total download size: 113 k Installed size: 379 k Downloading packages: warning: /var/cache/yum/x86_64/7/base/packages/mlocate-0.26-8.el7.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID f4a80eb5: NOKEY Public key for mlocate-0.26-8.el7.x86_64.rpm is not installed mlocate-0.26-8.el7.x86_64.rpm | 113 kB 00:00:02 Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7 Importing GPG key 0xF4A80EB5: Userid : "CentOS-7 Key (CentOS 7 Official Signing Key) <[email protected]>" Fingerprint: 6341 ab27 53d7 8a78 a7c2 7bb1 24c6 a8a7 f4a8 0eb5 Package : centos-release-7-6.1810.2.el7.centos.x86_64 (@anaconda) From : /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7 Running transaction check Running transaction test Transaction test succeeded Running transaction Installing : mlocate-0.26-8.el7.x86_64 1/1 Verifying : mlocate-0.26-8.el7.x86_64 1/1 Installed: mlocate.x86_64 0:0.26-8.el7 Complete! [root@localhost ~]# updatedb [root@localhost ~]# locate httpd.conf /root/httpd.conf

摘自:《Linux实用教程》人民邮电出版社出版发行 於岳编著

猜你喜欢

转载自www.cnblogs.com/mylive/p/11106786.html