Linux命令幫助文檔的使用及簡單命令使用-2016-7-25

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:

      -L print the value of $PWD if it names the current working

     Directory

(-L 顯示變量$PWD的值,鏈接地址也顯示)

      -P print 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

hits command

   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  

hits command

   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

hits command

   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命令優先級高于外部命令

 

命令行歷史

基礎概念

v保存你輸入的命令歷史??梢杂盟鼇碇貜蛨绦忻?/span>

v登錄shell時,會讀取命令歷史文件中記錄下的命令~/.bash_history

v登錄進shell后新執行的命令只會記錄在緩存中;這些命令會用戶退出時“追加”至命令歷史文件中

相關命令history

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

vhistory -anrw[filename]

vhistory -psarg[arg…]

-c: 清空命令歷史

-d: 刪除歷史中指定的命令;

#: 顯示最近的#條歷史

-a: 追加本次會話新執行的命令歷史列表至歷史文件

-n: 讀歷史文件中未讀過的行到歷史列表

-r: 讀歷史文件附加到歷史列表

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

-p: 展開歷史參數成多個行,但不存在歷史列表中

-s: 展開歷史參數成一行,附加在歷史列表后

 

命令歷史相關環境變量

vHISTSIZE:命令歷史記錄的條數;

vHISTFILE:指定歷史文件,默認為~/.bash_history

vHISTFILESIZE:命令歷史文件記錄歷史的條數;

vHISTTIMEFORMAT=%F %T“ 顯示時間

vHISTIGNORE=str1:str2:… “ 忽略string1,string2歷史

v控制命令歷史的記錄方式:

  環境變量:HISTCONTROL

  ignoredups:忽略重復的命令;連續且相同方為“重復”

  ignorespace:忽略所有以空白開頭的命令

  ignorebothignoredups, ignorespace

  erasedups:刪除重復命令

export 變量名="值“

存放在/etc/profile ~/.bash_profile

 

注意事項:

 命令歷史永久生效方法:

 變量建議放在profile文件下:全局生效: /etc/profile  個人生效:~/.bash_profile

 

 別名永久生效的方法:

 別名建議放在bashrc文件中:全局生效: /etc/bashrc    個人生效:~/.bashrc

 

bash的快捷鍵

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

vCtrl+c取消命令的執行

vCtrl-a 會移動到命令行的最前面

vCtrl-e 會移動到命令行的最后面

vCtrl-u 會刪除到行首

vCtrl-k 會刪除到行尾

vEsc-b左移一個單詞

vEsc-f右移一個單詞

 

原創文章,作者:linux_root,如若轉載,請注明出處:http://www.www58058.com/27679

(0)
linux_rootlinux_root
上一篇 2016-08-04
下一篇 2016-08-04

相關推薦

  • bash腳本編程實例

    bash腳本編程實例 1.寫一個腳本 接受一個以上文件路徑作為參數 顯示每個文件擁有的行數 總結說明本次共為幾個文件統計了其行數 #!/bin/bash # read -p “please input some paths:” paths if [ -z $paths ];then echo “There are not any paths inputtin…

    Linux干貨 2017-08-14
  • 第十八周作業

    1、為LNMP架構添加memcached支持,并完成對緩存效果的測試報告; 環境準備: 1)LNMP(php-fpm)環境已搭建完畢 2)Wordpress已部署完成 1.在memcache服務器上安裝memcached包并啟動服務 ]# yum install memcached -y ]# systemctl start memcached.servic…

    2017-06-22
  • 第五周作業

    1、顯示當前系統上root、fedora或user1用戶的默認shell; 答:#egrp ‘^(root|fedora|user1)’ /etc/passwd | cut -f1,7 -d: 2、找出/etc/rc.d/init.d/functions文件中某單詞后面跟一組小括號的行,形如:hello(); 答:#egrp -o &…

    Linux干貨 2017-02-25
  • tomcat

      編程語言:硬件級:微碼編程,匯編語言系統級:C,C++,…應用級:Java, PHP, Python, Ruby, Perl, C#, …Python: PVMStandard LibraryWeb Framework:Django, Flask, …Java:JVM,JDKbash:bash ex…

    Linux干貨 2017-05-22
  • PHP異常處理詳解

    PHP異常處理詳解          異常處理(又稱為錯誤處理)功能提供了處理程序運行時出現的錯誤或異常情況的方法。   異常處理通常是防止未知錯誤產生所采取的處理措施。異常處理的好處是你不用再絞盡腦汁去考慮各種錯誤,這為處理某一類錯誤提供了一個很有效的方法,使編程效率大大提高。當異常被觸發時,通常會發生:&…

    Linux干貨 2015-06-02
  • windows server 2008 r2 每隔一段時間自動關機

    首先來說一下我的狗血經歷:     話說作為一個地鐵汪,每天背著電腦從房山到朝陽那簡直就是能累死我的節奏,所以我跟公司申請我不要用自己的筆記本了(補助我不要了)。最后公司把一臺不用的server 2008r2給我用,12G內存,志強CPU,跑虛擬機杠杠的。 但是噩夢開始了,我發現一個特點,這臺server 2008 r…

    2015-11-26
欧美性久久久久