查找命令

一、简介

在Linux中要想查找出文件的文件,对于初学者来说可能是一件非常困难的事情。因为Linux都是使用字符界面的,不像Windows那样直接在搜索框中搜索就好了。特别是当你的文件目录非常深,并且你忘记了文件的名字的时候。但是这篇文章就教我们怎么样在Linux系统中,快速的找出我们想要寻找的东西。并且查找的速度是远远大于Windwos系统的。

 

二、which

which是用来查询可执行程序的命令、程序或文件,并且在指定路径下的。也就是说所有的可执行文件都在指定路劲下,都是可以用which的命令来查找的。

 

在控制台下直接输入which可以得到使用帮助

[root@node2 ~]# which
Usage: /usr/bin/which [options] [--] COMMAND [...]
Write the full path of COMMAND(s) to standard output.

  --version, -[vV] Print version and exit successfully.
  --help,          Print this help and exit successfully.
  --skip-dot       Skip directories in PATH that start with a dot.
  --skip-tilde     Skip directories in PATH that start with a tilde.
  --show-dot       Don't expand a dot to current directory in output.
  --show-tilde     Output a tilde for HOME directory for non-root.
  --tty-only       Stop processing options on the right if not on tty.
  --all, -a        Print all matches in PATH, not just the first
  --read-alias, -i Read list of aliases from stdin.
  --skip-alias     Ignore option --read-alias; don't read stdin.
  --read-functions Read shell functions from stdin.
  --skip-functions Ignore option --read-functions; don't read stdin.

Recommended use is to write the output of (alias; declare -f) to standard
input, so that which can show aliases and shell functions. See which(1) for
examples.

If the options --read-alias and/or --read-functions are specified then the
output can be a full alias or function definition, optionally followed by
the full path of each command used inside of those.

Report bugs to <which-bugs@gnu.org>.

 

我们搜索which pass可以看到下面的结果

[root@node2 ~]# which pass
/usr/bin/which: no pass in (/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/opt/IBM/t
ivoli/netcool/install:/opt/IBM/tivoli/netcool/bin:/opt/IBM/tivoli/netcool/omnibus/install:/opt/IBM/tivoli/netcool/omnibus/bin:/bin:/opt/IBM/tivoli/netcool/omnibus/probes:/bin:/opt/IBM/tivoli/netcool/omnibus/utils/shell:/opt/IBM/tivoli/tipv2/bin:/opt/IBM/tivoli/netcool/precision/bin:/root/bin)

 

显示结果表示which命令在在这些路径下找不到pass这个可执行文件。因为which是基于PATH环境变量查找的,所以速度非常快。那这个路径是谁呢?

其实这个环境变量是$PATH的值。所有这个命令会根据执行用户的不同有不一样的结果。注意PATH所保存的目录都是可执行的一些文件。

 

vim /root/.bash.profile这个文件就是当前用户的环境变量信息。

[root@node2 ~]# cat ~/.bash_profile 
# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
    . ~/.bashrc
fi

# User specific environment and startup programs

PATH=$PATH:$HOME/bin

export PATH

 

我们也可以直接在这里修改PATH环境变量的值。

修改后需要当前环境下是不生效的,可以重新登陆shell,或者使用source命令来让它生效。

source ~/.bash_profile

 

所以which是用来查找可执行文件的路径的。所以当我们不知道某个程序的可执行文件在什么位置的时候,第一反应就是要想到which来查找。

 

三、whereis

whereis查找的时候,是不需要像which那样需要在只当环境变量下查找的。它是在某些指定位置查找我们需要查找的文件的。因为我们知道在Linux中安装一些标准的程序,他们的文件存放位置都是已经指定好的了。所以whereis查找的速度也是非常快的。

 

如,当我们查找passwd的时候

[root@node2 ~]# whereis passwd
passwd: /usr/bin/passwd /etc/passwd /usr/share/man/man1/passwd.1.gz /usr/share/man/man5/
passwd.5.gz

 

它的返回结果给我们的信息是在这个路径下都找到了passwd。对于标准的程序来说它的安装位置大概是这个样子的

/usr/bin/    # 这个目录存放是可执行文件的路径
/etc/passwd/        # 这个目录存放的是配置文件的路径
/usr/share/man/man1/    # 这两个文件存放的是帮助文件的路径
/usr/shars/man/man5/

 

而当我们查找一个不存在的文件时,它的返回结果是这样的。

[root@node2 ~]# whereis pass
pass:

它的返回信息中并没有提示在这些标准路径下找到pass文件。

 

whereis它能帮助找出对应的可执行文件的位置,同样还可以帮我们找出可它相关的一些文件的位置。

 whereis和locate都是由自己的数据库,所以它们两个命令在查找的时候是更具它们的数据库信息来查找的。比如说我们安装一个标准的LInux程序,安装程序的信息,都会写入到这个数据库中。所以它的查找数据是非常的快的。

这些信息包括,可执行文件的位置、配置文件的位置、帮助文档的位置。

 

四、locate

locate查找的结果是非常多的,比which和whereis都要多很多。

 

它有模糊并配的味道,知道你的安装程序在安装的时候,写入的信息含有你要查找的指定字符,它都可以会帮我们找出来。

所以locate来查找我们记忆不清楚的的命令的。

 

所以在使用这三个命令的优先选择是,which->whereis->locate。应为它们的返回结果的准确度是越来越低的。但是它们三个查找速度都是很快的。

 

五、find

前面的查找都是和可执行程序/文件的。那么我们想要查找一些普通文件的时候,需要怎么办呢?那么find命令就可以帮助我们了。

 

格式

find 查找位置 选项

 

和时间相关

find /home -mtime 0 # 表示24小时内修改过的文件
find /home -mtime 3 # 表示3天前那一天修改过的文件
find /home -mtime +3 # 表示3天内修改过的所有文件
find /home -mtime -3 # 表示3天内修改的文件全部排除

 

和用户相关

find /home -user zhangsan # 查找和zhangsan相关的文件,取决于文件属组,和用户组
find /home -group zhangsan # 查找zhangsan这个用户组的文件
find /home -nouser # 查找不属于任何用户和用户组的文件

 

和文件权限相关

find / -name passwd # 查找passwd命名的文件,这是精确查找的
find / -iname passwd # 这个查找passwd命名的文件,不区分大小写
find / -type f #根据文件类型类查找文件,这里查找普通文件
    # f 普通文件(file)
    # l 链接文件(link)
    # d 目录(dir)
find /etc -size +1000k # 查找文件>=1000K的文件,-号就是小于啦
find /etc -name 'pas*' # 查找以pas开头的文件
find / -perm +7000 # 查找有特殊权限的文件
posted @ 2018-10-11 16:30  周末没早餐  阅读(386)  评论(0)    收藏  举报