linux type 命令和Linux的五个查找命令

type命令用来显示指定命令的类型。一个命令的类型可以是如下之一

  • alias 别名
  • keyword 关键字,Shell保留字
  • function 函数,Shell函数
  • builtin 内建命令,Shell内建命令
  • file 文件,磁盘文件,外部命令
  • unfound 没有找到

它是Linux系统的一种自省机制,知道了是那种类型,我们就可以针对性的获取帮助。比如内建命令可以用help命令来获取帮助,外部命令用man或者info来获取帮助。

常用参数

type命令的基本使用方式就是直接跟上命令名字。

type -a可以显示所有可能的类型,比如有些命令如pwd是shell内建命令,也可以是外部命令。

type -p只返回外部命令的信息,相当于which命令。

type -f只返回shell函数的信息。

type -t 只返回指定类型的信息。

使用示例

示例一 type自己是什么类型的命令

[root@new55 ~]# type -a type 
type is a shell builtin
[root@new55 ~]# help type 
type: type [-afptP] name [name ...]
    For each NAME, indicate how it would be interpreted if used as a
    command name.
    
    If the -t option is used, `type' outputs a single word which is one of
    `alias', `keyword', `function', `builtin', `file' or `', if NAME is an
    alias, shell reserved word, shell function, shell builtin, disk file,
    or unfound, respectively.
    
    If the -p flag is used, `type' either returns the name of the disk
    file that would be executed, or nothing if `type -t NAME' would not
    return `file'.
    
    If the -a flag is used, `type' displays all of the places that contain
    an executable named `file'.  This includes aliases, builtins, and
    functions, if and only if the -p flag is not also used.
    
    The -f flag suppresses shell function lookup.
    
    The -P flag forces a PATH search for each NAME, even if it is an alias,
    builtin, or function, and returns the name of the disk file that would
    be executed.
typeset: typeset [-afFirtx] [-p] name[=value] ...
    Obsolete.  See `declare'.
[root@new55 ~]#

示例二 常见命令的类型

[root@new55 ~]# type -a cd 
cd is a shell builtin
[root@new55 ~]# type -a pwd 
pwd is a shell builtin
pwd is /bin/pwd
[root@new55 ~]# type -a time 
time is a shell keyword
time is /usr/bin/time
[root@new55 ~]# type -a date 
date is /bin/date
[root@new55 ~]# type -a which 
which is aliased to `alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
which is /usr/bin/which
[root@new55 ~]# type -a whereis 
whereis is /usr/bin/whereis
[root@new55 ~]# type -a whatis 
whatis is /usr/bin/whatis
[root@new55 ~]# type -a function 
function is a shell keyword
[root@new55 ~]# type -a ls 
ls is aliased to `ls --color=tty'
ls is /bin/ls
[root@new55 ~]# type -a ll 
ll is aliased to `ls -l --color=tty'
[root@new55 ~]# type -a echo 
echo is a shell builtin
echo is /bin/echo
[root@new55 ~]# type -a bulitin 
-bash: type: bulitin: not found
[root@new55 ~]# type -a builtin 
builtin is a shell builtin
[root@new55 ~]# type -a keyword 
-bash: type: keyword: not found
[root@new55 ~]# type -a command 
command is a shell builtin
[root@new55 ~]# type -a alias 
alias is a shell builtin
[root@new55 ~]# type -a grep 
grep is /bin/grep
[root@new55 ~]#

问题思考

相关资料

【1】Blue_Stone's OpenWorld    Linux中的type命令     
【2】山海经  Linux中的type命令     
【3】鸟哥的私房菜  Bash shell 的內建命令: type    

1. find

find是最常见和最强大的查找命令,你可以用它找到任何你想找的文件。

find的使用格式如下:

  $ find <指定目录> <指定条件> <指定动作>

  - <指定目录>: 所要搜索的目录及其所有子目录。默认为当前目录。

  - <指定条件>: 所要搜索的文件的特征。

  - <指定动作>: 对搜索结果进行特定的处理。

如果什么参数也不加,find默认搜索当前目录及其子目录,并且不过滤任何结果(也就是返回所有文件),将它们全都显示在屏幕上。

find的使用实例:

  $ find . -name "my*"

搜索当前目录(含子目录,以下同)中,所有文件名以my开头的文件。

  $ find . -name "my*" -ls

搜索当前目录中,所有文件名以my开头的文件,并显示它们的详细信息。

  $ find . -type f -mmin -10 (注意-10)

搜索当前目录中,所有过去10分钟中更新过的普通文件。如果不加-type f参数,则搜索普通文件+特殊文件+目录。

2. locate

locate命令其实是“find -name”的另一种写法,但是要比后者快得多,原因在于它不搜索具体目录,而是搜索一个数据库(/var/lib/locatedb),这个数据库中含有本地所有文件信息。Linux系统自动创建这个数据库,并且每天自动更新一次,所以使用locate命令查不到最新变动过的文件。为了避免这种情况,可以在使用locate之前,先使用updatedb命令,手动更新数据库。

locate命令的使用实例:

  $ locate /etc/sh

搜索etc目录下所有以sh开头的文件。

  $ locate ~/m

搜索用户主目录下,所有以m开头的文件。

  $ locate -i ~/m

搜索用户主目录下,所有以m开头的文件,并且忽略大小写。

3. whereis

whereis命令只能用于程序名的搜索,而且只搜索二进制文件(参数-b)、man说明文件(参数-m)和源代码文件(参数-s)。如果省略参数,则返回所有信息。

whereis命令的使用实例:

  $ whereis grep

4. which

which命令的作用是,在PATH变量指定的路径中,搜索某个系统命令的位置,并且返回第一个搜索结果。也就是说,使用which命令,就可以看到某个系统命令是否存在,以及执行的到底是哪一个位置的命令。

which命令的使用实例:

  $ which grep

5. type

type命令其实不能算查找命令,它是用来区分某个命令到底是由shell自带的,还是由shell外部的独立二进制文件提供的。如果一个命令是外部命令,那么使用-p参数,会显示该命令的路径,相当于which命令。

type命令的使用实例:

  $ type cd

系统会提示,cd是shell的自带命令(build-in)。

  $ type grep

系统会提示,grep是一个外部命令,并显示该命令的路径。

  $ type -p grep

加上-p参数后,就相当于which命令。


【4】阮一峰的网络日志    Linux的五个查找命令     

转自:http://codingstandards.iteye.com/blog/831504   

原文地址:https://www.cnblogs.com/youxin/p/3543549.html