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

linux type 命令和Linux的五个查找命令的更多相关文章

  1. Linux的五个查找命令find,locate,whereis,which,type

    Linux的五个查找命令 1. find 最常见且最强大的命令,可以查找任何文件. 格式 $ find   指定目录   指定条件   指定动作   指定目录: 所要搜索的目录及其子目录,默认当前目录 ...

  2. Linux的五个查找命令(find、locate、whereis、which、type)

    1. find find是最常见和最强大的查找命令,你可以用它找到任何你想找的文件. find的使用格式如下: $ find <指定目录> <指定条件> <指定动作> ...

  3. Linux的五个查找命令:find,locate,whereis,which,type

    使用电脑的时候,经常需要查找文件. 在Linux中,有很多方法可以做到这一点.国外网站LinuxHaxor总结了五条命令,你可以看看自己知道几条.大多数程序员,可能经常使用其中的2到3条,对这5条命令 ...

  4. 【转】Linux的五个查找命令:find,locate,whereis,which,type

    原文网址:http://www.ruanyifeng.com/blog/2009/10/5_ways_to_search_for_files_using_the_terminal.html 最近,我在 ...

  5. Linux的五个查找命令:find,locate,whereis,which,type 及其区别

    1. find find是最常见和最强大的查找命令,你可以用它找到任何你想找的文件. find的使用格式如下: $ find <指定目录> <指定条件> <指定动作> ...

  6. linux下五种查找命令

      我们经常需要在系统中查找一个文件或者命令,那么在Linux系统中如何快速定位和精确查找它呢?下面总结了五个基础命令·分别是which.whereis.type.locate.find. 一 whi ...

  7. Linux下的五个查找命令:grep、find、locate、whereis、which

    原文转自 http://www.cnblogs.com/wanqieddy/archive/2011/07/15/2107071.html 1.grep grep(General Regular Ex ...

  8. Linux的五个查找命令

    1. find find是最常见和最强大的查找命令,你可以用它找到任何你想找的文件. find的使用格式如下: $ find <指定目录> <指定条件> <指定动作> ...

  9. Linux的五个查找命令 [转]

    最近,我在学习Linux,下面是一些笔记. 使用电脑的时候,经常需要查找文件. 在Linux中,有很多方法可以做到这一点.国外网站LinuxHaxor总结了五条命令,你可以看看自己知道几条.大多数程序 ...

随机推荐

  1. Android之断点续传下载

    今天学习了Android开发中比较难的一个环节,就是断点续传下载,很多人看到这个标题就感觉头大,的确,如果没有良好的逻辑思维,这块的确很难搞明白.下面我就将自己学到的知识和一些见解写下供那些在这个环节 ...

  2. 【转】深入理解Android的startservice和bindservice--不错

    原文网址:http://www.cnblogs.com/yejiurui/p/3429451.html 一.首先,让我们确认下什么是service?         service就是android系 ...

  3. TCP/IP 中文译名为传输控制协议/因特网互联协议,又叫网络通讯协议

    原文地址:http://hi.baidu.com/albyuyrgqgbbhoq/item/65006d2d002ab33195f62ba1 TCP/IP(Transmission Control P ...

  4. Abstract Methods and Classes

    阅读了Java的官方Doc,在此总结如下. Abstract Class & Method In jave, "abstract" can be a modifier to ...

  5. git 查看文件修改记录

    今天追了个几年前留下来的坑, 在 git 里追溯修改过程坑死个爹, 具体方法估计没多久又会忘, 还是记下来以后有的参考 大部分教程都会告诉大家使用 git log 来查看对应文件的修改记录, 就像这样 ...

  6. 跨平台utf8转unicode研究实现(2)

    最近在用VC++开发一个小工具,平时用惯了.NET,用起VC++最郁闷的就是字符串处理.当然最最让人难于琢磨的就是字符集,编码之间的转换.通过这几天的研究,终于明白了Unicode和UTF-8之间编码 ...

  7. poj 2976 Dropping tests (二分搜索之最大化平均值之01分数规划)

    Description In a certain course, you take n tests. If you get ai out of bi questions correct on test ...

  8. (转)iOS7人机界面设计规范 - 目录

    英文原文出自苹果官方的iOS7设计资源-iOS人机界面设计规范(预发布版本),由C7210自发翻译,并首发于Beforweb.com.如需转载,请注明译者及出处信息. UI设计基础 为iOS7而设计 ...

  9. 外观模式之C++实现

    说明:本文仅供学习交流,转载请标明出处.欢迎转载. 在我们学习程序设计时经常会用到模块化设计的思想,这一思想是我们首先把要实现的功能用一个模块表示,当用户想完毕某个人物时依次调用相应的函数. 然而.假 ...

  10. 带中文索引的ListView 仿微信联系人列表

    因为各种原因,项目经理和产品经理把我做的东西给否定了,所以决定分享出去. 主要功能: 1 .带中文索引的ListView 2.自己定义顶部搜索视图,能够对返回button,搜索button加入事件监听 ...