【Linux】【操作】Linux操作集锦系列之二——如何获取命令的相关信息

简介: 【Linux】【操作】Linux操作集锦系列之二——如何获取命令的相关信息

经常我们面对一个shell命令或者一个外部程序的时候,对它的用法、类型、源码等,一无所知。除了借助互联网或命令手册,也可借助Linux自带的机制,获取关于这个命令或程序的更多信息。


查看命令的类型


type


  • type命令用来显示指定命令的类型,判断给出的指令是内部指令还是外部指令,具体命令类型如下:


alias: 别名


keyword: 关键字,Shell保留字


function: 函数,Shell函数


builtin: 内建命令,Shell内建命令


file: 文件,磁盘文件,外部命令


unfound: 未找到


  • 语法:


tpye [选项] [参数]


选项:


-t: 输出"file"、"alias"或者 “builtin”,分别表示给定的指令为:“外部指令”、“命令别名” 或者 “内部指令”;


-p: 如果给出的指令为外部指令,则显示其绝对路径;


-a: 在环境变量“PATH”指定的路径中,显示给定指令的信息,包括命令别名。

参数:


指令,要显示类型的指令


  • 一个例子


[qxhgd@localhost ~]$ type ls
ls 是 `ls --color=auto' 的别名
[qxhgd@localhost ~]$ type cat
cat is /bin/cat
[qxhgd@localhost ~]$  type cd
cd is a shell builtin


file


  • 用来探测给定文件的类型,如


  • man page:


NAME
     file — determine file type
SYNOPSIS
     file [-bchiklLNnprsvz0] [--apple] [--mime-encoding] [--mime-type] [-e testname] [-F separator] [-f namefile] [-m magicfiles] file ...
     file -C [-m magicfiles]
     file [--help]


几个选项的含义:


-b:#列出结果,但不显示文件名称


-c:#详细显示指令执行过程


-L:#显示链接文件的源文件类型


-m<魔法数字文件>:#指定魔法数字文件


-v:#打印出版本信息


-z:#查看压缩文件的内容


  • 一个例子


[qxhgd@localhost ~]$file ls
ls: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamica`在这里插入代码片`lly linked (uses shared libs), for GNU/Linux 2.6.32, BuildID[sha1]=aaf05615b6c91d3cbb076af81aeff531c5d7dfd9, stripped
[qxhgd@localhost ~]$file xxx_func.c
xxx_func.c: C source, UTF-8 Unicode text, with CRLF line terminators


查看命令的位置


which


  • 查找环境变量中的命令位置


[qxhgd@localhost ~]$ which mkdir
/usr/bin/mkdir
[qxhgd@localhost ~]$which -a mkdir
/usr/bin/mkdir
/bin/mkdir


whereis


  • 查找命令及man手册的位置


[qxhgd@localhost ~]$ whereis mkdir
mkdir: /usr/bin/mkdir /usr/share/man/man1/mkdir.1.gz


find


  • find命令可以根据给定的路径和表达式查找的文件或目录。find参数选项很多,并且支持正则,功能强大。和管道结合使用可以实现复杂的功能;


  • find不光可以用于查证可执行程序,也可以查询其他文件;


locate


  • locate命令用于查找文件,它比find命令的搜索速度快,但是它需要一个数据库(/var/lib/mlocate/mlocage.db),这个该数据库一般是 crontab每天调用一次updatedb 命令来更新的;


  • 命令格式:


locate [选择参数] [样式]


  • 例子:


[qxhgd@localhost ~]$locate /etc/sh       
/etc/shadow
/etc/shadow-
/etc/shells


查看命令的用法


help


  • 可显示shell内部命令的帮助信息


  • 直接执行help,可以查看所有shell内置命令;


  • 例子:


[qxhgd@localhost ~]$help dir


man


  • 详细命令的帮助手册


  • 命令格式:


  • 配合whatis


man command
man -k  xxx     #根据关键字搜索联机帮助,是一种模糊搜索。例如要查找"passwd"相关的信息,使用man -k passwd会找到很多和passwd相关的帮助页。
man -f xxx      #关键字精确搜索,与-k不同,它只搜索与关键字完全匹配的帮助页。


info


  • info命令 是Linux下info格式的帮助指令。


  • 就内容来说,info页面比man page编写得要更好、更容易理解,也更友好,但man page使用起来确实要更容易得多。一个man page只有一页,而info页面几乎总是将它们的内容组织成多个区段(称为节点),每个区段也可能包含子区段(称为子节点)。


whatis


  • 作用:display manual page descriptions,打印man帮助页面的描述信息,只是一个brief信息;


  • 语法:


whatis [-dlv?V] [-r|-w] [-s list] [-m system[,...]] [-M path] [-L locale] [-C file] name ...


  • 例子:


[qxhgd@localhost ~]$ whatis ls
ls (1)               - list directory contents
[qxhgd@localhost ~]$ whatis cat
cat (1)              - concatenate files and print on the standard output


-h/–help或不带任何参数或带部分参数


  • 显示外部命令的帮助信息,尤其适用于那些开源的小程序(有的小程序不带参数执行,也会打印帮助信息);


  • 命令格式


command --help
command -h
command                 #不带参数
command xx              #仅带部分参数


  • 例子:


[qxhgd@localhost ~]$ ls --help
用法:ls [选项]... [文件]...
List information about the FILEs (the current directory by default).
Sort entries alphabetically if none of -cftuvSUX nor --sort is specified.
Mandatory arguments to long options are mandatory for short options too.
[qxhgd@localhost ~]$iwpriv wifi0
wifi0    Available private ioctls :
         setHALparam     (8BE0) : set   2int   &get  0      
         getHALparam     (8BE1) : set   1int   &get   1int  


查看命令的版本


一般对于shell或外部命令,可以尝试用下面命令获取具体的版本信息


[qxhgd@localhost ~]$bash --version


查看命令的源码


对于shell内置命令


  • 查看当前用的什么shell


[qxhgd@localhost ~]$ env | grep -i shell
SHELL=/bin/bash


  • 查看bash的版本:


[qxhgd@localhost ~]$bash --version
NewTitan
GNU bash, version 4.2.46(2)-release (x86_64-redhat-linux-gnu)
Copyright (C) 2011 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.


  • 去对应网站下对应版本的bash的源码


bash源码


对于外置命令:


  • 查看对应的包,不同系统对应不同命令:


dpkg -S /usr/bin/cat #ubuntu系统、Debian系统
rpm -qif `which cat` #redhat系统


这里以redhat为例来说明:


[qxhgd@localhost ~]$rpm -qif `which cat` #redhat系统
Name        : coreutils
Version     : 8.22
Release     : 24.el7
Architecture: x86_64
Install Date: Sat 11 Jan 2020 06:50:40 PM CST
Group       : System Environment/Base
Size        : 14593469
License     : GPLv3+
Signature   : RSA/SHA256, Fri 23 Aug 2019 05:21:30 AM CST, Key ID 24c6a8a7f4a80eb5
Source RPM  : coreutils-8.22-24.el7.src.rpm
Build Date  : Tue 20 Aug 2019 02:27:26 PM CST
Build Host  : x86-01.bsys.centos.org
Relocations : (not relocatable)
Packager    : CentOS BuildSystem <http://bugs.centos.org>
Vendor      : CentOS
URL         : http://www.gnu.org/software/coreutils/
Summary     : A set of basic GNU tools commonly used in shell scripts
Description :
These are the GNU core utilities.  This package is the combination of
the old GNU fileutils, sh-utils, and textutils packages.


  • 获取对应包的源码


– 不用管什么系统,都可直接去gnu官网找coreutils对应源码;


– 对于支持apt-get的系统,可用下面命令下载源码:


sudo apt-get source coreutils


– 对于RedHat系统:


1、下载对应的xxx.src.rpm源码包


yumdownloader --source coreutils -8.22


2、提取源码,可使用下面两个手段:


1)直接安装源码:


rpm -i coreutils-8.5-7.fc14.src.rpm


2)从rpm提取:


查询rpm中的tar文件,源码一般放在里面


rpm -qpl coreutils-8.5-7.fc14.src.rpm | grep tar


从rpm包中提取指定的文件


rpm2cpio coreutils-8.5-7.fc14.src.rpm | cpio -idv coreutils.tar.gz
相关文章
|
1天前
|
Linux
Linux系统ps命令
这些是一些常见的 `ps`命令选项和用法,用于查看系统中运行的进程及其相关信息。您可以根据需要选择合适的选项以满足您的任务要求。
8 0
|
2天前
|
存储 Linux Shell
linux课程第二课------命令的简单的介绍2
linux课程第二课------命令的简单的介绍2
|
2天前
|
Linux C语言 数据安全/隐私保护
linux课程第二课------命令的简单的介绍3
linux课程第二课------命令的简单的介绍3
|
2天前
|
监控 Unix Linux
如何使用 Linux less 命令?
【4月更文挑战第25天】
11 1
如何使用 Linux less 命令?
|
2天前
|
JSON 网络协议 Linux
Linux ip命令:网络的瑞士军刀
【4月更文挑战第25天】
8 1
|
2天前
|
安全 Linux C语言
linux课程第一课------命令的简单的介绍
linux课程第一课------命令的简单的介绍
|
2天前
|
SQL 缓存 监控
|
2天前
|
前端开发 Linux Shell
|
2天前
|
网络协议 Linux Shell
|
2天前
|
NoSQL Linux Shell
2.Docker常用命令(linux)
2.Docker常用命令(linux)