Linux基础学习05

5.搜索命令

whereis命令

是搜索系统命令的命令,只能搜索系统的命令

which命令

和whereis相似,当时还能查找到别名

 [root@localhost ~]# whereis ls
 ls: /bin/ls /usr/share/man/man1p/ls.1p.gz /usr/share/man/man1/ls.1.gz
 [root@localhost ~]# which ls
 alias ls='ls --color=auto'
     /bin/ls

locate命令

可以按照文件名搜索普通文件的命令

  • 优点:按照数据库搜索,搜索速度快,消耗资源小,数据库位置/var/lib/mlocate/mlocate.db

  • 缺点:只能按照文件名来搜索文件,而不能执行更负载的搜索,比如按照权限、大小、修改时间等方式来搜索文件

 [root@localhost ~]# locate abc
 /usr/lib64/python2.6/_abcoll.py
 /usr/lib64/python2.6/_abcoll.pyc
 /usr/lib64/python2.6/_abcoll.pyo
 /usr/lib64/python2.6/abc.py
 /usr/lib64/python2.6/abc.pyc
 /usr/lib64/python2.6/abc.pyo
 /usr/share/foomatic/db/source/PPD/Sharp/shabc260.ppd.gz
 /usr/share/foomatic/db/source/PPD/Sharp/shabc320.ppd.gz
 ...
 [root@localhost ~]# touch abcdef
 [root@localhost ~]# locate abcdef
 [root@localhost ~]# updatedb
 [root@localhost ~]# locate abcdef
 /root/abcdef

配置文件

 /etc/updatedb.conf
 ​
 PRUNE_BIND_MOUNTS = "yes"
 PRUNEFS = "9p afs anon_inodefs auto autofs bdev binfmt_misc cgroup cifs coda configfs cpuset debugfs devpts ecryptfs exofs fuse fusectl gfs gfs2 gpfs hugetlbfs inotifyfs iso9660 jffs2 lustre mqueue ncpfs nfs nfs4 nfsd pipefs proc ramfs rootfs rpc_pipefs securityfs selinuxfs sfs sockfs sysfs tmpfs ubifs udf usbfs"
 PRUNENAMES = ".git .hg .svn"
 PRUNEPATHS = "/afs /media /net /sfs /tmp /udev /var/cache/ccache /var/spool/cups /var/spool/squid /var/tmp"

也就是搜索不到/tmp下文件的原因

find命令

1、按照文件名搜索

 find 搜索路径 [选项] 搜索内容
 选项:
     -name   按文件名
     -iname  按文件名,不区分大小写
     -inum   按inode搜索
 [root@localhost ~]# ls
 abc  ABC  abcdef  anaconda-ks.cfg  copytest  install.log  install.log.syslog  test
 [root@localhost ~]# find . -name abc
 ./abc
 [root@localhost ~]# find . -iname abc
 ./ABC
 ./abc
 [root@localhost ~]# ls -i abc
 263485 abc
 [root@localhost ~]# find . -inum 263485
 ./abc

2、按照文件大小搜索

 find 搜素路径 [选项] 搜索内容
 选项:
     -size [+|-]大小   按照指定大小搜索文件
 [root@localhost ~]# ll -h
 总用量 56K
 -rw-r--r--. 1 user1 user1   39 4月   8 23:11 abc
 -rw-r--r--. 1 root  root     0 4月   8 23:07 ABC
 -rw-r--r--. 1 root  root     0 4月   8 22:44 abcdef
 -rw-------. 1 root  root  1.3K 4月   8 05:06 anaconda-ks.cfg
 -rw-r--r--. 1 root  root     9 4月   8 19:56 copytest
 -rw-r--r--. 1 root  root   28K 4月   8 05:06 install.log
 -rw-r--r--. 1 root  root  8.9K 4月   8 05:04 install.log.syslog
 -rw-r--r--. 1 root  root     0 4月   8 21:26 test
 [root@localhost ~]# find . -size 28k
 ./install.log
 [root@localhost ~]# find . -size 28k
 ./install.log
 [root@localhost ~]# find . -size -28k
 .
 ./ABC
 ./.bash_logout
 ./.bash_history
 ./.Xauthority
 ./copytest
 ./install.log.syslog
 ./.cshrc
 ./anaconda-ks.cfg
 ./abc
 ./.bash_profile
 ./.bashrc
 ./test
 ./.tcshrc
 ./abcdef
 [root@localhost ~]# find . -size 39
 [root@localhost ~]# find . -size 39c
 ./abc

-size 后面的默认单位是b 如-size 15 就是15*512字节

3.按时间搜索

 find 搜素路径 [选项] 搜索内容
 选项:
     -atime [+|-]时间  按照文件访问时间
     -mtime [+|-]时间  按照文件数据修改时间
     -ctime [+|-]时间  按照文件状态修改时间

时间轴(图片摘自网络)

img

4、按照权限搜索 

find 搜素路径 [选项] 搜索内容
 选项:
     -perm 权限模式   查找文件权限刚好等于“权限模式”的文件
     -perm -权限模式  查找文件权限包含“权限模式”的文件
     -perm +权限模式  按照文件权限包含“权限模式”的任意一个权限的文件

-权限,意味着所有的权限都必须比该权限大

+权限,意味着只要一个权限大于就行了

示例:创建find1 权限 644 ,find2权限 600 效果如下

 -rw-r--r--. 1 root root 0 4月   8 23:42 find1
 -rw-------. 1 root root 0 4月   8 23:42 find2
 [root@localhost test]# find . -perm +444
 .
 ./find1
 ./find2
 [root@localhost test]# find . -perm -444
 .
 ./find1

5.按照所有者和所有组搜索

 find 搜素路径 [选项] 搜索内容
 选项:
     -uid   用户ID     根据用户ID查找所有者知道ID的文件
     -gid   用户组ID    按照用户组ID查找
     -user  用户名      按照用户名查找
     -group 组名       按照所属组查找
     -nouser          查找没有所有者得文件

6.按照文件类型搜索

 
find 搜素路径 [选项] 搜索内容
 选项:
     -type   d   按照目录
     -type   f   按照普通文件
     -type   l   按照软连接文件

7.逻辑运算符

 find 搜素路径 [选项] 搜索内容
 选项:
     -a   and逻辑与
     -o
     -not    
 [root@localhost ~]# find . -size +1k
 .
 ./.bash_history
 ./install.log.syslog
 ./anaconda-ks.cfg
 ./install.log
 ./test
 [root@localhost ~]# find . -size +1k -a -type f
 ./.bash_history
 ./install.log.syslog
 ./anaconda-ks.cfg
 ./install.log

8.其他选项

-exec选项

 find 搜索路径 [选项] 搜索内容 -exec 命令2 {} ;

-ok 与-exec基本一致,只是会询问。

示例:

[root@localhost ~]# find . -size +1k -a -type f -exec ls -lh {} ;
 -rw-------. 1 root root 1.6K 4月   8 09:41 ./.bash_history
 -rw-r--r--. 1 root root 8.9K 4月   8 05:04 ./install.log.syslog
 -rw-------. 1 root root 1.3K 4月   8 05:06 ./anaconda-ks.cfg
 -rw-r--r--. 1 root root 28K 4月   8 05:06 ./install.log

9.grep命令

在文件中提取和匹配符合条件的字符串行

 grep [选项] “搜索内容” 文件名
 选项:
     -i 忽略大小写
     -n 输出行号
     -v 反向查找
     --color==auto 搜索出的字符创颜色显示
 [root@localhost ~]# cat abc
 123123123
 hasdaskljfghakjsdhkajshdkasd
 [root@localhost ~]# grep "123" abc
 123123123
 [root@localhost ~]# echo askdhasf >> abc
 [root@localhost ~]# grep -n "123" abc
 1:123123123
 [root@localhost ~]# grep -vn "123" abc
 2:hasdaskljfghakjsdhkajshdkasd
 3:askdhasf      
  • find命令

    • 用于在系统中搜索符合条件的文件名,如果需要模糊查询,则使用通配符进行匹配,搜索时文件名是完全匹配的。(可使用-regex选项装换为正则表达式规则)

  • grep命令

    • 用于在文件中搜索符合条件的字符创,如果需要模糊查询,则使用正则表达式进行匹配,搜索时字符串是包含匹配的。

通配符:用于匹配文件名,完全匹配

通配符作用
? 匹配一个任意字符
* 匹配0个或任意多个字符
[] 匹配中括号中任意一个字符。例如[abc]代表匹配a或b或c,只要匹配一个即可
[-] 匹配中括号中任意一个字符,-代表一个范围。如[a-z]代表匹配一个小写字母
[^] 逻辑非,表示匹配不是中括号内的一个字符。如[^0-9 ]表示匹配一个不是数字的字符

正则表达式:用于匹配字符串,包含匹配

正则符作用
匹配前一个字符重复0次,或1次
* 匹配前一个字符重复0次或任意多次
[] 匹配中括号中任意一个字符。例如[abc]代表匹配a或b或c,只要匹配一个即可
[-] 匹配中括号中任意一个字符,-代表一个范围。如[a-z]代表匹配一个小写字母
[^] 逻辑非,表示匹配不是中括号内的一个字符。如[^0-9 ]表示匹配一个不是数字的字符
^ 匹配行首
$ 匹配行尾
原文地址:https://www.cnblogs.com/yinqs/p/12660988.html