linux命令学习简介

目录

1 什么是linux命令

2 type命令


1 什么是linux命令

linux 命令是对 Linux 系统进行管理的命令。对于 Linux 系统来说,无论是中央处理器、内存、磁盘驱动器、键盘、鼠标,还是用户等都是文件,Linux 系统管理的命令是它正常运行的核心,与之前的 DOS 命令类似。linux 命令在系统中有两种类型:内置 Shell 命令和 Linux 命令。

首先介绍一个名词“控制台(console)”,它就是我们通常见到的使用字符操作界面的人机接口,例如 dos。我们说控制台命令,就是指通过字符界面输入的可以操作系统的命令,例如 dos 命令就是控制台命令。我们要了解的是基于 Linux 操作系统的基本控制台命令。有一点一定要注意,和 dos 命令不同的是,Linux 的命令(也包括文件名等等)对大小写是敏感的,也就是说,如果你输入的命令大小写不对的话,系统是不会做出你期望的响应的。

2 type命令

首先第一个要学习的命令就是type命令。type命令可以查看一个命令的类型,这些类型分为`alias', `keyword', `function', `builtin', `file'。 可以看几个例子:

parallels@ubuntu-linux-20-04-desktop:~/C_test1$ type cd
cd is a shell builtin
parallels@ubuntu-linux-20-04-desktop:~/C_test1$ type which
which is hashed (/usr/bin/which)
parallels@ubuntu-linux-20-04-desktop:~/C_test1$ type apt
apt is /usr/bin/apt

如果只想得到一个命令的简单的类型,可以加上-t参数。例如:

parallels@ubuntu-linux-20-04-desktop:~/C_test1$ type -t cd
builtin
parallels@ubuntu-linux-20-04-desktop:~/C_test1$ type -t which
file
parallels@ubuntu-linux-20-04-desktop:~/C_test1$ type -t apt
file

那么得到一个命令的类型有什么用处呢。得到一个命令的类型之后就可以使用和help或者man来查看该命令的帮助手册了。如果是builtin命令,则使用help命令来查看该命令的帮助手册,如果是file命令,则使用man命令来查看该命令的帮助手册。比如:

parallels@ubuntu-linux-20-04-desktop:~/C_test1$ help cd
cd: cd [-L|[-P [-e]] [-@]] [dir]
    Change the shell working directory.
    
    Change the current directory to DIR.  The default DIR is the value of the
    HOME shell variable.
    
    The variable CDPATH defines the search path for the directory containing
    DIR.  Alternative directory names in CDPATH are separated by a colon (:).
    A null directory name is the same as the current directory.  If DIR begins
    with a slash (/), then CDPATH is not used.
    
    If the directory is not found, and the shell option `cdable_vars' is set,
    the word is assumed to be  a variable name.  If that variable has a value,
    its value is used for DIR.
    
    Options:
      -L	force symbolic links to be followed: resolve symbolic
    		links in DIR after processing instances of `..'
      -P	use the physical directory structure without following
    		symbolic links: resolve symbolic links in DIR before
    		processing instances of `..'
      -e	if the -P option is supplied, and the current working
    		directory cannot be determined successfully, exit with
    		a non-zero status
      -@	on systems that support it, present a file with extended
    		attributes as a directory containing the file attributes
    
    The default is to follow symbolic links, as if `-L' were specified.
    `..' is processed by removing the immediately previous pathname component
    back to a slash or the beginning of DIR.
    
    Exit Status:
    Returns 0 if the directory is changed, and if $PWD is set successfully when
    -P is used; non-zero otherwise.

这样我们就得到了一个学习linux命令的方法。比如我们要学习which命令,从上面的type查询中得到which命令是一个file命令,因此可以使用man which查询该命令的使用方法。

WHICH(1)                                                                             General Commands Manual                                                                             WHICH(1)

NAME
       which - locate a command

SYNOPSIS
       which [-a] filename ...

DESCRIPTION
       which returns the pathnames of the files (or links) which would be executed in the current environment, had its arguments been given as commands in a strictly POSIX-conformant shell.  It
       does this by searching the PATH for executable files matching the names of the arguments. It does not canonicalize path names.

OPTIONS
       -a     print all matching pathnames of each argument

EXIT STATUS
       0      if all specified commands are found and executable

       1      if one or more specified commands is nonexistent or not executable

       2      if an invalid option is specified

从上面的手册中可以知道which命令可以定位一个命令的位置,其中-a参数可以用来打印所有匹配到的命令的位置。

扫描二维码关注公众号,回复: 15061659 查看本文章
parallels@ubuntu-linux-20-04-desktop:~/C_test1$ which ls
/usr/bin/ls
parallels@ubuntu-linux-20-04-desktop:~/C_test1$ which -a ls
/usr/bin/ls
/bin/ls

实际上type命令也能给出一个命令的位置,type命令比which命令给出的信息更多。 

×

猜你喜欢

转载自blog.csdn.net/daida2008/article/details/124717426