小白入门:文件搜索命令
Tofloor
poster avatar
老陌
deepin
2018-08-01 18:00
Author
本帖最后由 myccloves 于 2019-7-31 22:47 编辑


1. find 命令
  1. Usage: find [搜索的范围]  [匹配条件] [动作]   
Copy the Code

作用:搜索匹配的文件。

此命令选项太多,下面列出只种常见的选项。


动作:
对搜索到的结果进行统一处理
  1. -exec/-ok/-ls  命令 {} \;
Copy the Code


其中-exec表示执行,-ok表示寻问执行, -ls是长格式打印文件(ls命令),{}表示搜索结果,动作选项必须以分号结尾,但分号被shell解析为命令分隔,所以需要转义。

Example:

  1. find /etc/ -name "smb.conf"
  2. /etc/samba/smb.conf
Copy the Code

在/etc目录及子目录中搜索文件:smb.conf,搜索的内容放到双引号或单引号中,防止被shell替换。

  1. find /etc -size +2M
Copy the Code

在/etc中搜索大于2M的文件。

  1. find /home -user linux
Copy the Code

在/home中搜索文件所有者是linux的文件

  1. find /etc  -mmin -5
Copy the Code

在/etc中搜索五分钟内被修改的文件

  1. find /etc -type l -ls
Copy the Code

在/etc中搜索所有的链接文件,并显示出来了。

  1. find /tmp -inum 267323 -exec rm {} \;
Copy the Code

在/tmp中搜索inode节点ID是267323的文件,找到后执行rm命令,删除这个文件。

  1. find /etc -size +1M -and -size -5M
Copy the Code

在/etc/中搜索大于1M,并且小于5M的文件

2. locate 命令
  1. locate 文件名
Copy the Code


作用:在文件资料库中搜索文件。搜索的时候用模糊搜索,速度很快,因为是在数据库中。



locate在数据库中搜索,我用centos测试发现数据库是:/var/lib/mlocate/mlocate.db,但用deepin并不在这个位置,经查在:/var/cache/locate/locatedb
不过细节不用考虑,我们知道他能搜索就行了。数据库不是时实更新,所以有些文件找不到。我们可以手动更新数据库:
  1. updatedb
Copy the Code


注意 :/tmp目录下的文件不被数据库收纳。对于centos系统,可以在/etc/updatedb.conf中配置,其中PRUNEPATHS = “/afs /media /net /sfs /tmp….” 把目录放到这里则不会被数据库收纳。

安装 :
默认这个命令没有被安装,我们可以在线安装:
centos:
  1. yum install mlocate
Copy the Code


deepin:
  1. apt install locate
Copy the Code


Example:

  1. updatedb
Copy the Code

更新数据库,初次安装是空的,啥也搜索不到,所以必须手动更新一下。

  1. locate '天空之城'
Copy the Code

搜索文件名或路径包含天空之城关键字的文件。

  1. locate -i flash_cs6
Copy the Code

不区分大小写,搜索flash_cs6文件。

3. which 命令
  1. Usage: which 命令
Copy the Code


作用:搜索命令所在的目录

Example:

  1. which ls
  2.         /bin/ls
Copy the Code


显示ls命令所在的目录,我们发现在/bin/中

4. whereis 命令
  1. Usage: whereis 命令
Copy the Code

作用:搜索命令所在的目录及帮助文档所在的目录

Example:
  1. whereis ls
  2. ls: /bin/ls /usr/share/man/man1/ls.1.gz
Copy the Code

显示ls命令所在的目录位置,同时还显示ls的帮助文档的位置。

5. grep 命令
  1. Usage: grep [OPTION]... PATTERN [FILE]...
Copy the Code


作用:在文件的内容中搜索。 这与上面的命令不同,他是在文件中搜索,搜索到的行会被打印出来。



Example:
  1. grep -i uuid /etc/fstab
Copy the Code


在/etc/fstab文件中搜索uuid关键字,而且不区分大小写。

  1. grep -v ‘^#’  /etc/samba/smb.conf
Copy the Code

^#是正则表达式,^表示行的开头,^#表示以#开头的行,在linux的配置文件中,这样的行表示注释。
此命令的作用是过滤掉注释行,只显示有用的行。


Reply Favorite View the author
All Replies
avatar
yjhenan
deepin
2018-08-02 03:21
#1
可以的
Reply View the author