linux 查找01
2016-12-04 本文已影响5人
小小机器人
which
which命令用于查找并显示给定命令的绝对路径,环境变量PATH中保存了查找命令时需要遍历的目录。which指令会在环境变量$PATH设置的目录里查找符合条件的文件。也就是说,使用which命令,就可以看到某个系统命令是否存在,以及执行的到底是哪一个位置的命令。
[xxjqr@localhost ~]$ touch mytestfile
[xxjqr@localhost ~]$ which mytestfile
/usr/bin/which: no mytestfile in (/usr/lib64/qt-3.3/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin:/home/xxjqr/bin)
[xxjqr@localhost ~]$ which touch
/bin/touch
执行env命令可以查看当前的环境变量
PATH=/usr/lib64/qt-3.3/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin:/home/xxjqr/bin
也可以打开/root/.bash_profile来添加环境变量
whereis
whereis命令只能用于程序名(命令文件也是程序)的搜索,而且只搜索二进制文件(参数-b)、man说明文件(参数-m)和源代码文件(参数-s)。如果省略参数,则返回所有信息;搜索原理也是用的数据库和locate一样
[root@localhost xxjqr]# whereis touch
touch: /bin/touch /usr/share/man/man1p/touch.1p.gz /usr/share/man/man1/touch.1.gz
locate
locate命令其实是find -name的另一种写法,但是要比后者快得多,原因在于它不搜索具体目录,而是搜索一个数据库/var/lib/locatedb,这个数据库中含有本地所有文件信息。Linux系统自动创建这个数据库,并且每天自动更新一次,所以使用locate命令查不到最新变动过的文件。为了避免这种情况,可以在使用locate之前,先使用updatedb命令,手动更新数据库。
[root@localhost home]# touch testfile
[root@localhost home]# updatedb
[root@localhost home]# locate testfile
/home/testfile
经测试,查找文件的时候都不会在/tmp目录下去搜索