天天看點

Linux之指令引入篇

    在了解計算機基礎和Linux曆史後通過以下知識,相信應該能開始着手學習Linux了吧.

1)如何登陸Linux?

    login: 使用者名 + Enter

    Password: 密碼 + Enter

2)什麼是終端?

     終端(Computer terminal)是用來讓使用者輸入資料及顯示其計算結果的機器。終端有些是全電子的,也有些是機電的。其又名終端機。

    Linux是多使用者多任務的,在早期一台Unix主機連接配接多個終端機,讓每個終端使用者均可使用。

    Linux也模拟實作了UNIX的終端功能:

        實體終端:Console

        虛拟終端:/dev/tty# [1,7]

        圖形終端:/dev/tty7

        模拟終端:/dev/pts/# [1,+oo)

3)使用者接口:與核心互動的接口

    在Windows中:桌面

<a href="https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=1890542426,2831379711&amp;fm=27&amp;gp=0.jpg" target="_blank"></a>

使用者僅需輕按兩下圖示即可完成使用者與核心互動

    在Linux中: xprotocol  

        桌面:Gnome,KDE,XFCE(用于嵌入式環境)

            使用者僅需輕按兩下圖示即可完成使用者與核心互動

        指令行:由sh,bash,csh,zsh,ksh程式提供

<a href="https://s1.51cto.com/wyfs02/M01/9D/33/wKiom1l7LLOBNYi8AAAFK4Ojm6Y780.png" target="_blank"></a>

    輸入指令+Enter鍵即可完成使用者與核心互動

4)Linux的哲學思想

    1、一切皆檔案,幾乎所有資源都映射為檔案:例如:裝置 

    2、由衆多單一目的的小程式組成複雜程式,例如:管道

    3、避免跟使用者互動,實作shell程式設計

    4、文本編輯指令即可配置文本

5)檔案:

    不能完成路徑映射

6)目錄:

    完成路徑映射

7)檔案名

    1、區分字元大小寫

    2、/不能作為檔案名,/為路徑分隔符

    3、長度小于255個字元

    4、以.開頭的檔案為隐藏檔案

    5、見名知義,單詞的縮寫; copy 的指令為cp

8)路徑

    /etc/sysconfig/network-scripts

    工作目錄:目前目錄

        ./  指的是 network-scripts

    上一級目錄:

        ../  指的是 sysconfig

    相對路徑:從目前目錄開始

        ./ifcfg-eth0

    絕對路徑:從根開始

         /etc/sysconfig/network-scripts/ifcfg-eth0

9)程式的組成

    指令 + 資料(來自于檔案,變量、數組、連結清單)

    算法 + 資料結構

    二進制檔案、庫檔案、配置檔案、幫助檔案

10)指令提示符:

1

2

3

<code>[root@izpo45bh60h6bsz ~]</code><code># </code>

<code>    </code><code>對于管理者: </code><code>#</code>

<code>    </code><code>普通使用者:$</code>

    提示符後鍵入指令按Enter鍵:

        使用者接口解析指令,分析指令,請求核心配置設定CPU時間片和核心資源将其運作為一個程序(動态)

        shell解析指令的過程:從定義的PATH環境變量中,從左而右依次查找目錄下是否存在此指令,查找到,記錄于緩存中以便二次查找,如果不存在,BASH指令報錯。

4

5

6

<code>[root@izpo45bh60h6bsz ~]</code><code># cat /etc/issue  #指令為cat,</code>

<code>\S</code>

<code>Kernel \r on an \m</code>

<code>[root@izpo45bh60h6bsz ~]</code><code># echo $PATH      #輸入指令時,shell程式會從變量的路徑中從左而右的查找</code>

<code>/usr/local/nginx/sbin</code><code>:</code><code>/usr/local/sbin</code><code>:</code><code>/usr/local/bin</code><code>:</code><code>/usr/sbin</code><code>:</code><code>/usr/bin</code><code>:</code><code>/root/bin</code>

11)指令的格式

    指令:

    指令的位置: 

            普通指令: bin中

            管理指令: sbin中

    選項:用于啟用或關閉指令的某個或某些功能

7

8

9

10

<code>[root@izpo45bh60h6bsz ~]</code><code># cat /etc/fstab</code>

<code>#</code>

<code># /etc/fstab</code>

<code># Created by anaconda on Fri Feb 24 02:58:22 2017</code>

<code>[root@izpo45bh60h6bsz ~]</code><code># cat -n /etc/fstab   #可以明顯看出,-n啟用了,可以檢視每行編号的功能。</code>

<code>     </code><code>1</code>

<code>     </code><code>2 </code><code>#</code>

<code>     </code><code>3 </code><code># /etc/fstab</code>

<code>     </code><code>4 </code><code># Created by anaconda on Fri Feb 24 02:58:22 2017</code>

短選項: -c,-l,-h,多個選項間用空格分隔

    多個選項可合并,例如:-l -h = -hl,合并後的結果仍不會改變

11

12

13

14

<code>[root@izpo45bh60h6bsz ~]</code><code># ls -l -h /</code>

<code>total 1.5M</code>

<code>drwxr-xr-x   10 root  root   4.0K Jun 28 15:19 app</code>

<code>-rw-r--r--    1 root  root   1.4M Jul  3 16:04 app.</code><code>tar</code><code>.gz</code>

<code>lrwxrwxrwx.   1 root  root      7 Feb 24 10:58 bin -&gt; usr</code><code>/bin</code>

<code>dr-xr-xr-x.   4 root  root   4.0K Mar  3 12:27 boot</code>

<code>drwxr-xr-x    3 nginx nginx  4.0K Jun  2 21:26 cache</code>

<code>[root@izpo45bh60h6bsz ~]</code><code># ls -lh /</code>

長選項:--character,--human-readable

    長選項不能合并,合并使用時将會報錯,多個選項間用空格分隔

<code>[root@izpo45bh60h6bsz ~]</code><code># ls --all --human-readable</code>

<code>.         a.txt          .bash_profile   bin     .config  fstab     link_test1  mariadb-5.5.56-linux-i686.</code><code>tar</code><code>.gz  .pip              .rnd       .tcshrc  tomcat</code>

<code>..        .bash_history  .bashrc         b.sh    c.sh     gentoo    .</code><code>local</code>      <code>.mysql_history                    .pki              slackware  test1    tomcat1</code>

<code>.ansible  .bash_logout   .bashrc_docker  .cache  .cshrc   .lesshst  manifests   </code><code>passwd</code>                            <code>.pydistutils.cfg  .</code><code>ssh</code>       <code>tmpfile  .viminfo</code>

<code>[root@izpo45bh60h6bsz ~]</code><code># ls --all--human-readable</code>

<code>ls</code><code>: unrecognized option </code><code>'--all--human-readable'</code>

<code>Try </code><code>'ls --help'</code> <code>for</code> <code>more</code> <code>information.</code>

<code>[root@izpo45bh60h6bsz ~]</code><code># ls --allhuman-readable</code>

<code>ls</code><code>: unrecognized option </code><code>'--allhuman-readable'</code>

 混合使用:

<code>[root@izpo45bh60h6bsz ~]</code><code># ls --all -lh</code>

<code>total 207M</code>

<code>drwxr-xr-x. 17 root      root      4.0K Jul 28 17:33 .</code>

<code>dr-xr-xr-x. 25 root      root      4.0K Jul 28 14:54 ..</code>

<code>drwx------   3 root      root      4.0K Jun 14 16:23 .ansible</code>

<code>-rw-r--r--   1 root      root         5 Jul 13 10:06 a.txt</code>

<code>-rw-------   1 root      root       20K Jul 28 18:14 .bash_history</code>

<code>-rw-r--r--.  1 root      root        18 Dec 29  2013 .bash_logout</code>

<code>-rw-r--r--   1 root      root       203 Jul 10 08:45 .bash_profile</code>

參數:指令的作用對象

<code>[root@izpo45bh60h6bsz ~]</code><code># ls</code>

<code>a.txt  bin  b.sh  c.sh  fstab  gentoo  link_test1  manifests  mariadb-5.5.56-linux-i686.</code><code>tar</code><code>.gz  </code><code>passwd</code>  <code>slackware  test1  tmpfile  tomcat  tomcat1</code>

<code>[root@izpo45bh60h6bsz ~]</code><code># ls /tmp</code>

<code>123.</code><code>file</code>                                                               <code>systemd-private-5dcdf14753784b8d848b77111bf5ef1e-systemd-machined.service-2X5usM  tmp.37EDkMuqcT</code>

<code>Aegis-&lt;Guid(5A2C30A2-A87D-490A-9281-6765EDAD7CBA)&gt;                     systemd-private-a5389471547d457d95c6184b005585f9-named.service-Bjy16G             tmp.KEBXHvNPLz</code>

<code>mykernel                                                               systemd-private-a5389471547d457d95c6184b005585f9-ntpd.service-Jqrn5o              tmp.XESks7mpp9</code>

<code>systemd-private-5dcdf14753784b8d848b77111bf5ef1e-named.service-U672aF  tmp.1GRvDKa1HN</code>

當給出不同的參數,得出的結果,并不相同。

ls指令的作用是:list directory contents列出目錄下的檔案清單,傳遞不同的參數,将列出不同目錄下的檔案清單 。如果沒有參數,預設為目前目錄。

12)如何使用指令?

   指令的格式:

            windows: EXE,MSI

            linux: ELF

    指令的類型:

            shell自帶的指令: 在檔案系統所有路徑下沒有與之對應的程式檔案

<code>[root@izpo45bh60h6bsz ~]</code><code># type cd</code>

<code>cd</code> <code>is a shell </code><code>builtin</code>

<code>[root@izpo45bh60h6bsz ~]</code><code># enable -n cd  ##禁用内部指令</code>

<code>[root@izpo45bh60h6bsz ~]</code><code># enable -n  ##顯示所有被禁用的内部指令</code>

<code>enable</code> <code>-n </code><code>cd</code>

<code>[root@izpo45bh60h6bsz ~]</code><code># enable cd  ##啟動一個内部指令</code>

<code>[root@izpo45bh60h6bsz ~]</code><code># enable -a | fgrep cd  ##顯示所有的内部指令</code>

<code>enable</code> <code>cd</code>

            外部指令:有獨立的可執行檔案

<code>[root@izpo45bh60h6bsz ~]</code><code># type cat</code>

<code>cat</code> <code>is </code><code>/usr/bin/cat</code>

    shell自帶指令:

       # help 指令(COMMAND)

<code>[root@izpo45bh60h6bsz ~]</code><code># help cd</code>

<code>cd</code><code>: </code><code>cd</code> <code>[-L|[-P [-e]]] [</code><code>dir</code><code>]</code>

<code>    </code><code>Change the shell working directory.</code>

    外部指令:

        1)擷取幫助: COMMAND --help

<code>[root@izpo45bh60h6bsz ~]</code><code># cat --help</code>

<code>Usage: </code><code>cat</code> <code>[OPTION]... [FILE]...</code>

<code>Concatenate FILE(s), or standard input, to standard output.</code>

<code>  </code><code>-E, --show-ends          display $ at end of each line</code>

<code>  </code><code>-n, --number             number all output lines</code>

        2)幫助手冊: man [CHAPTER] COMMAND

        man 查詢如果沒有給出具體章節,預設顯示比對到首個章節。

            幫助手冊的位置: /usr/share/man

<code>[root@izpo45bh60h6bsz ~]</code><code># ls /usr/share/man  #ls作用的對象為/usr/share/man目錄,列出此目錄下的所有檔案和目錄</code>

<code>cs  de  fr  </code><code>id</code>  <code>ja  man0p  man1p  man2   man3   man3x  man4x  man5x  man6x  man7x  man8x  man9x  pl  pt_BR  sk  </code><code>tr</code>     <code>zh_TW</code>

<code>da  es  hu  it  ko  man1   man1x  man2x  man3p  man4   man5   man6   man7   man8   man9   mann   pt  ru     sv  zh_CN</code>

            不同幫助手冊的意義:

                man1,man2,man3,....,man8

                    man1: 普通指令

<code>[root@localhost ~]</code><code># man 1 ls (/bin/ls)</code>

                    man2: 系統調用

<code>[root@localhost ~]</code><code># man 2 read</code>

                    man3: 庫調用

                    man4: 裝置

<code>[root@localhost ~]</code><code># man 4 tty (/dev/tty)</code>

                    man5:配置檔案格式

<code>[root@localhost ~]</code><code># man 5 passwd (/etc/passwd)</code>

                    man6: 遊戲

                    man7:雜項

                    man8:管理指令

<code>[root@localhost ~]</code><code># man 8 init (/sbin/init)</code>

            擷取指令的幫助手冊: whatis COMMAND

<code>[root@localhost ~]</code><code># whatis passwd   #幫助手冊的位置1,5都有</code>

<code>passwd</code>               <code>(1)  - update user's authentication tokens</code>

<code>passwd</code>               <code>(5)  - password </code><code>file</code>

<code>passwd</code> <code>[sslpasswd]   (1ssl)  - compute password hashes</code>

            注意擷取指令是找到whatis資料庫實作的,如果有新的man手冊加入有必要運作指令:

<code># makewhatis</code>

         檢視指定手冊的幫助: man # COMMAND   #[1,9]

<code>[root@localhost ~]</code><code># man 1 passwd</code>

<code>PASSWD(1)                       User utilities                       PASSWD(1)</code>

<code>NAME</code>

<code>       </code><code>passwd</code> <code>- update user’s authentication tokens</code>

<code>[root@localhost ~]</code><code># man 5 passwd       </code>

<code>PASSWD(5)                  Linux Programmer’s Manual                 PASSWD(5)</code>

<code>       </code><code>passwd</code> <code>- password </code><code>file</code>

                快捷鍵:

                    Ctrl用 ^表示

                    ^u: 向上翻半屏

                    ^d: 向下翻半屏

                    Enter: 向下翻一行

                    k:向上翻一行

                    space:向下翻一屏

                    b:向上翻一屏

                    q: 退出

                    #G:跳到指定行

                    G:最後一行

                    1G/gg:道行

               搜尋:

                       /字元串 目前光标向下搜尋

                        ?字元串 目前光标向上搜尋

                            n 同向下一個

                            N 逆向下一個

     3)資訊頁:info

<code>[root@localhost ~]</code><code># info ls  #顯示指令的作者,所寫的日期,等與此指令相關曆史資訊</code>

      4)官方文檔 Documentation

        Hadoop: hadoop.apache.org

        nginx : nginx.org

        apache: apache.org        

        5)發行商的文檔: 

                redhat.com/docs

        6)谷歌、bing、百度

本文轉自 lccnx 51CTO部落格,原文連結:http://blog.51cto.com/sonlich/1951866,如需轉載請自行聯系原作者

繼續閱讀