2016725日学习笔记

Linux命令帮助文档的使用

  相关命令 whatis    COMMAND --help    man and info 本地帮助文档/usr/share/doc

  在使用系统内建的帮助文档之前,我们需要了解需要命令帮助是否是内部命令和外部命令;使用type命令来检测需要寻求的命令是内部还是外部命令,内部命令用 help COMMAMD , 外部命令使用 man COMMAND

了解几个命令:

type   COMMADN : 查看命令是外部命令还是内部命令

whatis  COMMADN : 查看命令帮助的man文件章节

which  COMMADN : 看命令的路径

man -f  COMMADN : 查看命令帮助的man文件章节

type命令:

 

功能说明:

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

alias 别名

keyword 关键字,Shell保留字

function 函数,Shell函数

builtin 内建命令,Shell内建命令

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

unfound 没有找到

常用参数

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

type -a可以显示所有可能的类型

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

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

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

举例:

[root@love721 testdir]#  type   cd   ---> 显示为内部命令

cd is a shell builtin

[root@love721 testdir]# type  -a  cd

cd is a shell builtin

cd is /usr/bin/cd

---> 显示为内部命令,第二行显示在/usr/bin/cd也有cd的命令,两个cd的功能是不一样的,不要搞错了。

 

[root@love721 testdir]# type  -f  cd  

cd is a shell builtin

[root@love721 testdir]# type  -t  cd

builtin

 

 

 

[root@love721 testdir]# type  -a  ls- --> 显示为ls设置了别名还显示了命令路径

ls is aliased to `ls --color=auto'

ls is /usr/bin/ls

[root@love721 testdir]# type  -f  ls

ls is aliased to `ls --color=auto'

[root@love721 testdir]# type  -t  ls  ---> 显示为ls设置了别名

Alias

 

内部命令的帮助

#help COMMAND

例如:

[root@love721 testdir]# type  pwd pwd 为内部命令)

pwd is a shell builtin

[root@love721 testdir]# help pwd (使用help命令帮助)

pwd: pwd [-LP]

    Print the name of the current working directory.

(说明:显示当前工作目录的名称)

     Options:

      -Lprint the value of $PWD if it names the current working

    Directory

(-L 显示变量$PWD的值,链接地址也显示)

      -Pprint the physical directory, without any symbolic links   

    By default, `pwd' behaves as if `-L' were specified.

    (-P 显示物理地址,不显示链接地址)

    

    Exit Status:

    Returns 0 unless an invalid option is given or the current directory

    cannot be read.

#man bash/pwd向下搜索关键字  ?pwd向上搜索关键字 n上翻页 N下翻页

 由于 PWDbash的内置命令,所以还可以在bash的帮助文件中搜索pwd的帮助文件。

 

[root@love721 testdir]# man bash   ---->进入man bash 后搜索关键字,下翻页查找pwd   

 pwd [-LPPrint  the absolute pathname of the current working directory.  The pathname printed contains no symbolic links if the -P option is supplied or the -o physical option to the set builtin command is enabled.  If the  -L  option  is used,  the pathname printed may contain symbolic links.  The return status is 0 unless an error occurs while reading the name of the current directory or an invalid option is supplied.

 

 

 

外部命令帮助

外部命令寻求帮助的流程:

1.确定命令是不是外部命令 : type -a COMMAND

2.了解你需要帮助的类型,由于man 命令是分章节的,有可能一个命令在多个章节有相关的帮助文档,man帮助章节介绍说明:

1.用户命令

2.系统调用

3.C库调用

4.设备文件及特殊格式

5.配置文件格式

6.游戏

7.杂项

8.管理类命令

9.Linux内核API

 

例如:

[root@love681 ~]# type -a ls

ls is aliased to `ls --color=auto'

ls is /bin/ls

[root@love681 ~]# man  -f  ls  -->功用和whatis一样

ls                   (1)  - list directory contents  <命令基本介绍:显示目录内容>

ls                   (1p)  - list directory contents

[root@love681 ~]# man  1  ls  -->查看ls 第一章节的命令帮助

 

 

LS(1)                            User Commands                           LS(1)

 

NAME (名称介绍)

       ls - list directory contents

 

SYNOPSIS (语法格式)

       ls [OPTION]... [FILE]...

(    参数    文件     []表示可选,  .......表示多选)

 

DESCRIPTION (详细说明)

       List  information about the FILEs (the current directory by default).  Sort entries

       alphabetically if none of -cftuvSUX nor --sort.

 

       Mandatory arguments to long options are mandatory for short options too.

 

       -a, --all  (各个参数说明)

              do not ignore entries starting with .

 

       -A, --almost-all

              do not list implied . and ..

 

       --author

              with -l, print the author of each file

 

       -b, --escape

              print octal escapes for nongraphic characters

 

       --block-size=SIZE

              use SIZE-byte blocks.  See SIZE format below

 

       -B, --ignore-backups

              do not list implied entries ending with ~

 

       -c     with -lt: sort by, and show, ctime (time of last modification of file status

              information) with -l: show ctime and sort by name otherwise: sort by ctime

 

       -C     list entries by columns

 

       --color[=WHEN]

              colorize the output.  WHEN defaults to alwaysor can be neveror auto.

              More info below

 

       -d, --directory (各个参数说明)

              list directory entries instead of contents, and do not dereference  symbolic

              links

 

       -D, --dired

              generate output designed for Emacsdired mode

 

       -f     do not sort, enable -aU, disable -ls --color

 

       -F, --classify

              append indicator (one of */=>@|) to entries

 

       --file-type

              likewise, except do not append *

 

       --format=WORD

              across  -x, commas -m, horizontal -x, long -l, single-column -1, verbose -l,

              vertical -C

 

       --full-time

              like -l --time-style=full-iso

 

       -g     like -l, but do not list owner

 

       --group-directories-first

              group directories before files.

 

              augment with a --sort option, but  any  use  of  --sort=none  (-U)  disables

              grouping

 

       -G, --no-group

              in a long listing, dont print group names

 

       -h, --human-readable (各个参数说明)

              with -l, print sizes in human readable format (e.g., 1K 234M 2G)

..................................................................................

(以下省很多字,主要英文不好,不过系统提供的帮助文件很详细,知道如何查询的方法就慢慢尝试多看帮忙文件的)

 

 

 

关于配置文件的帮助文件查询

由于在man章节中有提供相关的配置文件的说明及其帮助,我们也可以尝试找相应的配置文件如何修改。

例如:我们需要让TTY用户登录显示 当前登录时间,当前主机名,当前登录终端号

我们知道TTY用户登录的信息文件为:/etc/issue的配置文件,我们通过man如何实现我们需要的要求?

1.man issue

SSUE(5)                                Linux Programmer's Manual                            ISSUE(5)

 

NAME

       issue - prelogin message and identification file

 

DESCRIPTION

       The  file /etc/issue is a text file which contains a message or system identification to be

       printed before the login prompt.  It may contain various @char and \char sequences, if sup

       ported by the getty-type program employed on the system.

 

FILES

       /etc/issue

 

SEE ALSO

       motd(5), agetty(8), mingetty(8)   (参考相关文件)

 

COLOPHON

       This  page  is  part  of release 3.53 of the Linux man-pages project.  A description of the

       project,    and    information    about    reporting    bugs,    can    be     found     at

       http://www.kernel.org/doc/man-pages/.

 

Linux                                       1993-07-24                                    ISSUE(5

 

由于没有找到可用信息,我们就在man 相关的文件 motd   agetty   mingetty

 

2.man agetty (找到相关的信息,我们就可以在 issue中尝试一下)

       b      Insert the baudrate of the current line.

 

       d      Insert the current date.  (插入时间)

 

       s      Insert the system name, the name of the operating system.

 

       l      Insert the name of the current tty line.  (插入 tty终端号 )

 

       m      Insert the architecture identifier of the machine, eg. i486

 

       n      Insert the nodename of the machine, also known as the hostname.(插入主机名)

 

       o      Insert the NIS domainname of the machine.

 

       O      Insert the DNS domainname of the machine.

 

       r      Insert the release number of the OS, eg. 1.1.9.

 

       t      Insert the current time.  (插入时间)

 

       u      Insert the number of current users logged in.

 

       U      Insert the string "1 user" or "<n> users" where <n> is the number of current

              users logged in.

 

       v      Insert the version of the OS, eg. the build-date etc.

 

 

最后尝试可以把/etc/issue文件修改为一下 可以实现我们的要求:

[root@love721 testdir]# cat /etc/issue

\S

Kernel \r on an \m

Mage Education Learning Services

http://www.magedu.com

TTY is  \l

HOSTNAME is \n

DATE is  \t

 

 

基础命令整理

Bash特性文件展开符 :{}

举例:touch  f {1,2,3,4,5,6}.{txt,log}   

[root@love721 testdir]# touch f{1,2,3,4,5,6}.{txt,log}   --->一次性创建多个文件

[root@love721 testdir]# ll

total 4

-rw-r--r-- 1 root root   0 Jul 25 19:48 f1.log

-rw-r--r-- 1 root root   0 Jul 25 19:48 f1.txt

-rw-r--r-- 1 root root   0 Jul 25 19:48 f2.log

-rw-r--r-- 1 root root   0 Jul 25 19:48 f2.txt

-rw-r--r-- 1 root root   0 Jul 25 19:48 f3.log

-rw-r--r-- 1 root root   0 Jul 25 19:48 f3.txt

-rw-r--r-- 1 root root   0 Jul 25 19:48 f4.log

-rw-r--r-- 1 root root   0 Jul 25 19:48 f4.txt

-rw-r--r-- 1 root root   0 Jul 25 19:48 f5.log

-rw-r--r-- 1 root root   0 Jul 25 19:48 f5.txt

-rw-r--r-- 1 root root   0 Jul 25 19:48 f6.log

-rw-r--r-- 1 root root   0 Jul 25 19:48 f6.txt

 

 

[root@love721 testdir]# rm  -f  f{1,2,3,4,5,6}.{txt,log}  --->一次性删除多个文件

[root@love721 testdir]# ll

total 4

-rw-r--r-- 1 root root 559 Jul 25 17:42 2016-7-25.sh

[root@love721 testdir]#

 

命令优先级顺序

alias,内部,hash,外部

     alias的命令优先级最高,毋庸置疑

[root@love721 ~]#  alias

alias cls='clear'

alias cp='cp -i'

alias egrep='egrep --color=auto'

alias fgrep='fgrep --color=auto'

alias grep='grep --color=auto'

alias ip='ifconfig'

alias l.='ls -d .* --color=auto'

alias ll='ls -l --color=auto'

alias ls='ls --color=auto'

alias mv='mv -i'

alias rm='rm -i'

alias vimnet='vim /etc/sysconfig/network-scripts/ifcfg-eno16777736'

alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'

[root@love721 ~]#  hash

hitscommand

   1/usr/bin/ls

   1/usr/bin/clear

[root@love721 ~]# ip

eno16777736: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500

        inet 10.1.16.71  netmask 255.255.0.0  broadcast 10.1.255.255

        inet6 fe80::20c:29ff:fe04:cb7e  prefixlen 64  scopeid 0x20<link>

        ether 00:0c:29:04:cb:7e  txqueuelen 1000  (Ethernet)

        RX packets 714429  bytes 53378867 (50.9 MiB)

        RX errors 0  dropped 7  overruns 0  frame 0

        TX packets 21775  bytes 8831960 (8.4 MiB)

        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536

        inet 127.0.0.1  netmask 255.0.0.0

        inet6 ::1  prefixlen 128  scopeid 0x10<host>

        loop  txqueuelen 0  (Local Loopback)

        RX packets 172  bytes 15908 (15.5 KiB)

        RX errors 0  dropped 0  overruns 0  frame 0

        TX packets 172  bytes 15908 (15.5 KiB)

        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

以上可以开到ip的别名是:ifconfig,敲ip就直接使用ifconfig的命令了,hash中缓存的是/usr/sbin/ifconfig 的命令。

 

内部命名高于hash和外部命令的测试

[root@love721 ~]#  type  -a  cd --->可以看到内部和外部都一个命令是cd  不过两个的功能不一样,注意!

cd is a shell builtin

cd is /usr/bin/cd

 

 测试思路:我们先屏蔽内部命令cd,在使用外部命令cd看能不能切换目录,如果不能说明调用的是外部命令cd,由于使用过外部命令就会缓存到hash中去,现在我们在开启内部命令cd,看能不不能切换目录,如果可以正常的切换,说明内部命令的优先级高于 hash和 外部命令。

 

[root@love721 /]# enable  -n  cd  ----> 禁用内部命令

[root@love721 /]# cd /tmp  -----> 目录无法切换

[root@love721 /]# hash  

hitscommand

   1/usr/sbin/ifconfig

   1/usr/bin/man

   1/usr/bin/ls

   1/usr/bin/cd  ---->hash缓存的是外部命令

   2/usr/bin/clear

 

 

[root@love721 /]# enable cd  ----> 开启内部命令cd

[root@love721 /]# cd /tmp    ----> 可以正常的切换目录

[root@love721 tmp]# hash

hitscommand

   1/usr/sbin/ifconfig

   1/usr/bin/man

   1/usr/bin/ls

   1/usr/bin/cd

   2/usr/bin/clear

 

 

以上就完成我们 内部命令 高于 hash和外部命令

 

 

 

hash命令优先级高于外部命令

测试流程:我们禁用内部命令cd ; hash中的 /usr/bin/cd 移动到别的地方,再来测试cd命令报不报错

[root@love721 tmp]#  mv /usr/bin/cd /tmp/   --> 移除外部命令cd到别的地方

[root@love721 tmp]# cd                                       

-bash: /usr/bin/cd: No such file or directory  --> 提示在hash中查找 

[root@love721 tmp]# hash -r  --> 清空hash

[root@love721 tmp]# hash

hash: hash table empty

[root@love721 tmp]# cd    --> 提示找不到该命令了

-bash: cd: command not found

[root@love721 tmp]# mv /tmp/cd /usr/bin/

[root@love721 tmp]# cd

[root@love721 tmp]#

 

 

以上可以确定hash命令优先级高于外部命令

 

命令行历史

基础概念

保存你输入的命令历史。可以用它来重复执行命令

登录shell时,会读取命令历史文件中记录下的命令~/.bash_history

登录进shell后新执行的命令只会记录在缓存中;这些命令会用户退出时“追加”至命令历史文件中

相关命令history

history  [-c]  [-d offset]  [n]

vhistory -anrw[filename]

vhistory -psarg[arg...]

-c: 清空命令历史

-d: 删除历史中指定的命令;

#: 显示最近的#条历史

-a: 追加本次会话新执行的命令历史列表至历史文件

-n: 读历史文件中未读过的行到历史列表

-r: 读历史文件附加到历史列表

-w: 保存历史列表到指定的历史文件

-p: 展开历史参数成多个行,但不存在历史列表中

-s: 展开历史参数成一行,附加在历史列表后

 

命令历史相关环境变量

HISTSIZE:命令历史记录的条数;

HISTFILE:指定历史文件,默认为~/.bash_history

HISTFILESIZE:命令历史文件记录历史的条数;

HISTTIMEFORMAT=%F %T“ 显示时间

HISTIGNORE=str1:str2:… “ 忽略string1,string2历史

控制命令历史的记录方式:

  环境变量:HISTCONTROL

  ignoredups:忽略重复的命令;连续且相同方为“重复”

  ignorespace:忽略所有以空白开头的命令

  ignorebothignoredups, ignorespace

  erasedups:删除重复命令

export 变量名="值“

存放在/etc/profile ~/.bash_profile

 

注意事项:

 命令历史永久生效方法:

 变量建议放在profile文件下:全局生效: /etc/profile  个人生效:~/.bash_profile

 

 别名永久生效的方法:

 别名建议放在bashrc文件中:全局生效: /etc/bashrc    个人生效:~/.bashrc

 

bash的快捷键

Ctrl+l 清屏,相当于clear命令

Ctrl+c取消命令的执行

Ctrl-a 会移动到命令行的最前面

Ctrl-e 会移动到命令行的最后面

Ctrl-u 会删除到行首

Ctrl-k 会删除到行尾

Esc-b左移一个单词

Esc-f右移一个单词