Linux - 在当前系统内查找信息的方法

时间:2023-03-09 15:08:27
Linux - 在当前系统内查找信息的方法

查找文件或目录

find命令示例:

  • find . -name 'Do*' 搜索当前目录(含子目录)中文件名以Do开头的文件;
  • find . -name 'Do*' -ls 搜索当前目录(含子目录)中文件名以Do开头的文件,并显示详细信息;
  • find . -type f -mmin -10 搜索当前目录(含子目录)中过去10分钟更新过的普通文件;如果不加-type f参数,则搜索普通文件、特殊文件和目录;

locate命令示例:

  • locate *.log 搜索locate命令已知的所有log结尾的文件;
  • locate ~/Do 搜索当前用户目录中以Do开头的目录及文件;
  • locate -i ~/Do 忽略大小写,搜索当前用户目录中以Do开头的目录及文件;

注意:locate命令并不能实时反映情况。

  • locate命令其实是通过搜索本地所有文件信息的缓存(/var/lib/locatedb)来反馈结果。
  • 这个缓存在系统启动时被创建,并且每天自动更新一次,所以使用locate命令无法查到最新的变动。
  • 建议在使用locate之前,先使用updatedb命令手动更新数据库。

查找文本

注意:在搜索指定开头的行和单词的使用区别。

[root@CentOS-7 ~]# ls -l
total 4
-rw-------. 1 root root 2018 Aug 30 2016 anaconda-ks.cfg
drwxr-xr-x. 2 root root 6 Mar 1 2017 Desktop
drwxr-xr-x. 2 root root 6 Mar 1 2017 Documents
drwxr-xr-x. 2 root root 6 Mar 1 2017 Downloads
drwxr-xr-x. 2 root root 6 Mar 1 2017 Music
drwxr-xr-x. 2 root root 6 Mar 1 2017 Pictures
drwxr-xr-x. 2 root root 6 Mar 1 2017 Public
drwxr-xr-x. 2 root root 6 Mar 1 2017 Templates
drwxr-xr-x. 2 root root 6 Mar 1 2017 Videos
[root@CentOS-7 ~]#
[root@CentOS-7 ~]# ls -l |grep M*
drwxr-xr-x. 2 root root 6 Mar 1 2017 Music
[root@CentOS-7 ~]#
[root@CentOS-7 ~]# ls -l |grep ^M
[root@CentOS-7 ~]#
[root@CentOS-7 ~]# ls -l |grep ^-rw
-rw-------. 1 root root 2018 Aug 30 2016 anaconda-ks.cfg
[root@CentOS-7 ~]#
[root@CentOS-7 ~]#

查找命令

获取命令帮助信息

  • which 显示命令的路径;
  • whereis 显示命令的路径、手册等信息(locate the binary, source, and manual page files for a command);
  • whatis 显示命令手册的页眉行,等同于man -f命令,可确认有哪些章节存在;
  • type 判断是否是内置命令,如果是外部命令将给出简要信息;

使用示例:

[root@CentOS-7 ~]# which find
/usr/bin/find
[root@CentOS-7 ~]#
[root@CentOS-7 ~]# whereis find
find: /usr/bin/find /usr/share/man/man1/find.1.gz /usr/share/man/man1p/find.1p.gz
[root@CentOS-7 ~]#
[root@CentOS-7 ~]# whatis find
find (1) - search for files in a directory hierarchy
find (1p) - find files
[root@CentOS-7 ~]#
[root@CentOS-7 ~]# type find
find is hashed (/usr/bin/find)
[root@CentOS-7 ~]#
[root@CentOS-7 ~]# type cd
cd is a shell builtin
[root@CentOS-7 ~]#

查找文档

  • /usr/share/doc/目录:程序相关文档
  • /usr/share/man目录:man命令的帮助文件

Manual Page Chapter List

1:所有用户可以操作的指令或可执行文件
2:系统核心调用的函数与工具
3:子调用,常用的函数与函数库
4:设备,硬件文件说明,通常是/dev/的文件
5:文件格式,配置文件或者是某些档案的格式
6:游戏相关
7:杂项,例如linux文件系统、网络协议、ASCIIcode等说明
8:系统管理员可用的命令
9:跟kernel有关的文件