亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

《鳥哥的Linux私房菜》學習筆記(1)——文件與

系統 2235 0

  在Linux中,任何設備都是文件,不僅如此,連數據通信的接口也有專門的文件負責。可以說,一切皆文件,目錄也是一種文件,是路徑映射。因此,文件系統是Linux的基礎。

一、文件與目錄管理命令

1、ls(list)--查看文件與目錄命令

  在linux中,該命令可能是最多被執行的,因為我們隨時要知道文件或者目錄的相關信息。ls命令默認顯示非隱藏文件的文件名(以文件名進行排序)以及文件名代表的顏色。

命令格式:ls [選項] [目錄名]

常用參數:

  -l(long):列出長字符串,包含文件的屬性與權限等數據。

  -a(all):列出所有文件,包含隱藏文件

  -d(directory):僅列出所指定目錄本身,而不列出目錄內的文件數據

  -R(Recursive):連同子目錄內容一起列出來,等于該目錄下所有文件都會顯示出來

  -h(human-readable):將文件容量以人類易讀的方式(GB,KB)列出來

2、cp(copy)--文件或者目錄復制命令

命令格式:cp [options] source1 source2 source3...directory ——多個文件拷貝到一個目錄

或者:cp [options] <source file or directory> <target file or directory> ——單個文件或目錄的拷貝

  如果是多個文件拷貝到一個目錄下,則該目錄必須存在。如果某個文件拷貝到某個目錄下,則會保持原來的名字

      
        1
      
      
        cp
      
       /etc/
      
        passwd
      
       /tmp
    

  也可以重命名,如

      
        1
      
      
        cp
      
       /etc/
      
        passwd
      
       /tmp/test_passwd
    

  則會重新命名為test_passwd,如果test_passwd文件已經存在,則會默認覆蓋該文件。

  cp這個命令,不同身份者執行這個命令會有不同的結果產生,尤其是-a -p參數。

常用參數:

  -i(interactive):若目標文件已經存在,則會在覆蓋時詢問

  -r(recursive):復制目錄及目錄下所有文件

  -l(link):進行硬鏈接的連接文件創建,而非復制文件本身

  -s(symbolic link):復制為符號鏈接文件,即快捷方式文件

      
        1
      
      
        cp
      
       ~/.bashrc /tmp/
      
        bashrc


      
      
        2
      
      
        cp
      
       -l  /tmp/bashrc /tmp/
      
        bashrc_hlink


      
      
        3
      
      
        cp
      
       -s /tmp/bashrc /tmp/bashrc_slink
    

?  -p(preserve):連同文件屬性一塊復制過去,而非使用默認屬性

  在cp命令不加-p參數時候,源文件與目標文件的權限是不同的,目的文件的所有者通常會是命令操作者本身。而且時間戳等也會改變。因此用于備份時候,可以用-p參數,完整復制文件權限的參數。

3、rm(remove)——移除文件或者目錄

命令格式 :rm [-fir]文件或者目錄

  該命令也可以用于對文件或者目錄重命名。如下面將目錄mvtest重命名為mvtest1,當然也可以使用rename命令

      hao@hao:/tmp$ 
      
        mv
      
       mvtest mvtest1
    

常用參數

  -f(force):忽略不存在的文件,不會出現警告信息。

  -i(interactive):互動模式,在刪除前會詢問用戶是否操作

  -r(recursive):遞歸刪除目錄及目錄下所有文件。

4、mv(move)——移動文件與目錄、或者更名

命令格式: mv [options] source1 source2 source3...director

命令參數:

-f(force):強制的意思,如果目標文件已經存在不會詢問直接覆蓋

  -i(interactive):互動模式,在目標文件已經存在時,詢問是否覆蓋

  -u(update):若目標文件已經存在,且source比較新,才會更新

      
         1 hao@hao:/$ cd /tmp

 2 hao@hao:/tmp$ cp ~/.bashrc bashrc

 3 hao@hao:/tmp$ mkdir mvtest

 4 hao@hao:/tmp$ mv bashrc mvtest

 5 hao@hao:/tmp$ ll /tmp


      
      
         6
      
       total 
      
        36
      
      
         7
      
       drwxrwxrwt  
      
        8
      
       root    root    
      
        4096
      
       Jul 
      
        24
      
      
        16
      
      :
      
        54
      
       ./


      
         8
      
       drwxr-xr-x 
      
        23
      
       root    root    
      
        4096
      
       Jul 
      
        21
      
      
        20
      
      :
      
        45
      
       ../


      
         9
      
       drwxrwxrwt  
      
        2
      
       root    root    
      
        4096
      
       Jul 
      
        24
      
      
        16
      
      :
      
        20
      
       .ICE-unix/


      
        10
      
       -r--r--r--  
      
        1
      
       root    root      
      
        11
      
       Jul 
      
        24
      
      
        16
      
      :
      
        20
      
       .X0-
      
        lock


      
      
        11
      
       drwxrwxrwt  
      
        2
      
       root    root    
      
        4096
      
       Jul 
      
        24
      
      
        16
      
      :
      
        20
      
       .X11-unix/


      
        12
      
       drwxrwxrwt  
      
        2
      
       lightdm lightdm 
      
        4096
      
       Jul 
      
        24
      
      
        16
      
      :
      
        20
      
       at-spi2/


      
        13
      
       drwxrwxr-x  
      
        2
      
       hao     hao     
      
        4096
      
       Jul 
      
        24
      
      
        16
      
      :
      
        54
      
       mvtest/


      
        14
      
       drwx------  
      
        2
      
       lightdm lightdm 
      
        4096
      
       Jul 
      
        24
      
      
        16
      
      :
      
        20
      
       pulse-2L9K88eMlGn7/


      
        15
      
       drwx------  
      
        2
      
       root    root    
      
        4096
      
       Jul 
      
        24
      
      
        16
      
      :
      
        20
      
       pulse-PKdhtXMmr18n/


      
        16
      
       -rw-rw-r--  
      
        1
      
       lightdm lightdm    
      
        0
      
       Jul 
      
        24
      
      
        16
      
      :
      
        20
      
       unity_support_test.
      
        0
      
      
        17 hao@hao:/tmp$ ll /tmp/mvtest


      
      
        18
      
       total 
      
        12
      
      
        19
      
       drwxrwxr-x 
      
        2
      
       hao  hao  
      
        4096
      
       Jul 
      
        24
      
      
        16
      
      :
      
        54
      
       ./


      
        20
      
       drwxrwxrwt 
      
        8
      
       root root 
      
        4096
      
       Jul 
      
        24
      
      
        16
      
      :
      
        54
      
       ../


      
        21
      
       -rw-r--r-- 
      
        1
      
       hao  hao  
      
        3486
      
       Jul 
      
        24
      
      
        16
      
      :
      
        53
      
      
         bashrc


      
      
        22 hao@hao:/tmp$ mv mvtest mvtest1

23 hao@hao:/tmp$ ll /tmp


      
      
        24
      
       total 
      
        36
      
      
        25
      
       drwxrwxrwt  
      
        8
      
       root    root    
      
        4096
      
       Jul 
      
        24
      
      
        16
      
      :
      
        55
      
       ./


      
        26
      
       drwxr-xr-x 
      
        23
      
       root    root    
      
        4096
      
       Jul 
      
        21
      
      
        20
      
      :
      
        45
      
       ../


      
        27
      
       drwxrwxrwt  
      
        2
      
       root    root    
      
        4096
      
       Jul 
      
        24
      
      
        16
      
      :
      
        20
      
       .ICE-unix/


      
        28
      
       -r--r--r--  
      
        1
      
       root    root      
      
        11
      
       Jul 
      
        24
      
      
        16
      
      :
      
        20
      
       .X0-
      
        lock


      
      
        29
      
       drwxrwxrwt  
      
        2
      
       root    root    
      
        4096
      
       Jul 
      
        24
      
      
        16
      
      :
      
        20
      
       .X11-unix/


      
        30
      
       drwxrwxrwt  
      
        2
      
       lightdm lightdm 
      
        4096
      
       Jul 
      
        24
      
      
        16
      
      :
      
        20
      
       at-spi2/


      
        31
      
       drwxrwxr-x  
      
        2
      
       hao     hao     
      
        4096
      
       Jul 
      
        24
      
      
        16
      
      :
      
        54
      
       mvtest1/


      
        32
      
       drwx------  
      
        2
      
       lightdm lightdm 
      
        4096
      
       Jul 
      
        24
      
      
        16
      
      :
      
        20
      
       pulse-2L9K88eMlGn7/


      
        33
      
       drwx------  
      
        2
      
       root    root    
      
        4096
      
       Jul 
      
        24
      
      
        16
      
      :
      
        20
      
       pulse-PKdhtXMmr18n/


      
        34
      
       -rw-rw-r--  
      
        1
      
       lightdm lightdm    
      
        0
      
       Jul 
      
        24
      
      
        16
      
      :
      
        20
      
       unity_support_test.
      
        0
      
      
        35 hao@hao:/tmp$ cp ~/.bashrc bashrc1

36 hao@hao:/tmp$ cp ~/.bashrc bashrc2

37 hao@hao:/tmp$ mv bashrc1 bashrc2  mvtest1

38 hao@hao:/tmp$ ll /tmp/mvtest1


      
      
        39
      
       total 
      
        20
      
      
        40
      
       drwxrwxr-x 
      
        2
      
       hao  hao  
      
        4096
      
       Jul 
      
        24
      
      
        16
      
      :
      
        58
      
       ./


      
        41
      
       drwxrwxrwt 
      
        8
      
       root root 
      
        4096
      
       Jul 
      
        24
      
      
        16
      
      :
      
        58
      
       ../


      
        42
      
       -rw-r--r-- 
      
        1
      
       hao  hao  
      
        3486
      
       Jul 
      
        24
      
      
        16
      
      :
      
        53
      
      
         bashrc


      
      
        43
      
       -rw-r--r-- 
      
        1
      
       hao  hao  
      
        3486
      
       Jul 
      
        24
      
      
        16
      
      :
      
        58
      
      
         bashrc1


      
      
        44
      
       -rw-r--r-- 
      
        1
      
       hao  hao  
      
        3486
      
       Jul 
      
        24
      
      
        16
      
      :
      
        58
      
       bashrc2
    

? 二、文件內容及屬性查閱

1、cat(concatenate)——文件內容查看命令

cat命令可以同時查看多個文件內容。cat命令是由第一行到最后一行連續顯示在屏幕上,還有一個命令tac是將文件內容由最后一行到第一行反向在屏幕上顯示出來。當文件內容過多時候,可以配合more或者less命令來執行較好。也可以配合shift+PageUp或者PageDown鍵使用。

命令格式 :cat [options] [file]...

命令參數:

  -b(number-nonblank):列出行號,僅對非空白行做出行號顯示

  -n(number):列出行號,對空白行也作出行號顯示

  -E(show-ends):顯示文本行結束符號'$',不同于windows的'^M$'

  -T(show-tabs):將[Tab]鍵以^I顯示出來

  -v(show-nonprinting):列出一些看不出來的特殊符號

      
         1 hao@hao:/tmp$ cat -n -E -T /etc/issue /etc/fstab


      
      
         2
      
      
        1
      
          Ubuntu 
      
        12.04
      
      .
      
        1
      
      
         LTS \n \l$


      
      
         3
      
      
        2
      
      
            $


      
      
         4
      
      
        3
      
          # /etc/fstab: static 
      
        file
      
      
         system information.$


      
      
         5
      
      
        4
      
      
            #$


      
      
         6
      
      
        5
      
          # Use 
      
        '
      
      
        blkid
      
      
        '
      
       to print the universally unique identifier 
      
        for
      
      
         a$


      
      
         7
      
      
        6
      
          # device; this may be used with UUID= as a 
      
        more
      
      
         robust way to name devices$


      
      
         8
      
      
        7
      
          # that works even 
      
        if
      
       disks are added and removed. See fstab(
      
        5
      
      
        ).$


      
      
         9
      
      
        8
      
      
            #$


      
      
        10
      
      
        9
      
          # <
      
        file
      
       system> <
      
        mount
      
       point>   <type>  <options>       <dump>  <pass>
      
        $


      
      
        11
      
      
        10
      
          proc            /proc           proc    nodev,noexec,nosuid 
      
        0
      
      
        0
      
      
        $


      
      
        12
      
      
        11
      
          # / was on /dev/
      
        sda1 during installation$


      
      
        13
      
      
        12
      
          UUID=edc98e92-b3a2-44f0-a0e9-079b479d229b /               ext4    errors=remount-ro 
      
        0
      
      
        1
      
      
        $


      
      
        14
      
      
        13
      
          # swap was on /dev/
      
        sda5 during installation$


      
      
        15
      
      
        14
      
          UUID=8f9d467f-
      
        8359
      
      -421d-96
      
        cc
      
      -156b9e4310e5 none            swap    sw              
      
        0
      
      
        0
      
      
        $


      
      
        16
      
      
        15
      
          /dev/fd0        /media/floppy0  auto    rw,user,noauto,exec,utf8 
      
        0
      
      
        0
      
      $
    

  cat命令還可以用'cat file1 file2 >> file'來將幾個文件追加到另一個文件的末尾,或者用'cat file1 > file2'用來覆蓋文件。或者用'cat > file'從磁盤來創建文件(在輸入有錯時候可以用ctrl+backspace刪除,最后按ctrl+d保存)

      
         1 hao@hao:/tmp$ cat fstab


      
      
         2
      
       # /etc/fstab: static 
      
        file
      
      
         system information.


      
      
         3
      
      
        #


      
      
         4
      
       # Use 
      
        '
      
      
        blkid
      
      
        '
      
       to print the universally unique identifier 
      
        for
      
      
         a


      
      
         5
      
       # device; this may be used with UUID= as a 
      
        more
      
      
         robust way to name devices


      
      
         6
      
       # that works even 
      
        if
      
       disks are added and removed. See fstab(
      
        5
      
      
        ).


      
      
         7
      
      
        #


      
      
         8
      
       # <
      
        file
      
       system> <
      
        mount
      
       point>   <type>  <options>       <dump>  <pass>


      
         9
      
       proc            /proc           proc    nodev,noexec,nosuid 
      
        0
      
      
        0
      
      
        10
      
       # / was on /dev/
      
        sda1 during installation


      
      
        11
      
       UUID=edc98e92-b3a2-44f0-a0e9-079b479d229b /               ext4    errors=remount-ro 
      
        0
      
      
        1
      
      
        12
      
       # swap was on /dev/
      
        sda5 during installation


      
      
        13
      
       UUID=8f9d467f-
      
        8359
      
      -421d-96
      
        cc
      
      -156b9e4310e5 none            swap    sw              
      
        0
      
      
        0
      
      
        14
      
       /dev/fd0        /media/floppy0  auto    rw,user,noauto,exec,utf8 
      
        0
      
      
        0
      
      
        15
      
       Ubuntu 
      
        12.04
      
      .
      
        1
      
      
         LTS \n \l


      
      
        16
      
      
        17 hao@hao:/tmp$ cat issue


      
      
        18
      
       Ubuntu 
      
        12.04
      
      .
      
        1
      
      
         LTS \n \l


      
      
        19
      
      
        20 hao@hao:/tmp$ cat issue > fstab

21 hao@hao:/tmp$ cat fstab


      
      
        22
      
       Ubuntu 
      
        12.04
      
      .
      
        1
      
      
         LTS \n \l


      
      
        23
      
      
        24 hao@hao:/tmp$ cat issue


      
      
        25
      
       Ubuntu 
      
        12.04
      
      .
      
        1
      
      
         LTS \n \l


      
      
        26
      
      
        27 hao@hao:/tmp$ cp /etc/fstab  /tmp

28 hao@hao:/tmp$ cat /tmp/issue >>/tmp/fstab

29 hao@hao:/tmp$ cat issue


      
      
        30
      
       Ubuntu 
      
        12.04
      
      .
      
        1
      
      
         LTS \n \l


      
      
        31
      
      
        32 hao@hao:/tmp$ cat fstab


      
      
        33
      
       # /etc/fstab: static 
      
        file
      
      
         system information.


      
      
        34
      
      
        #


      
      
        35
      
       # Use 
      
        '
      
      
        blkid
      
      
        '
      
       to print the universally unique identifier 
      
        for
      
      
         a


      
      
        36
      
       # device; this may be used with UUID= as a 
      
        more
      
      
         robust way to name devices


      
      
        37
      
       # that works even 
      
        if
      
       disks are added and removed. See fstab(
      
        5
      
      
        ).


      
      
        38
      
      
        #


      
      
        39
      
       # <
      
        file
      
       system> <
      
        mount
      
       point>   <type>  <options>       <dump>  <pass>


      
        40
      
       proc            /proc           proc    nodev,noexec,nosuid 
      
        0
      
      
        0
      
      
        41
      
       # / was on /dev/
      
        sda1 during installation


      
      
        42
      
       UUID=edc98e92-b3a2-44f0-a0e9-079b479d229b /               ext4    errors=remount-ro 
      
        0
      
      
        1
      
      
        43
      
       # swap was on /dev/
      
        sda5 during installation


      
      
        44
      
       UUID=8f9d467f-
      
        8359
      
      -421d-96
      
        cc
      
      -156b9e4310e5 none            swap    sw              
      
        0
      
      
        0
      
      
        45
      
       /dev/fd0        /media/floppy0  auto    rw,user,noauto,exec,utf8 
      
        0
      
      
        0
      
      
        46
      
       Ubuntu 
      
        12.04
      
      .
      
        1
      
      
         LTS \n \l


      
      
        47
      
      
        48
      
      
         hao@hao:/tmp$ cat >test.txt


      
      
        49
      
       this is a test 
      
        file
      
      
        50
      
      
         hao@hao:/tmp$ cat test.txt


      
      
        51
      
       this is a test 
      
        file
      
    

2、more——一頁一頁翻動

命令格式: more[options]file 或 more [-dlfpcsu] [-num] [+/pattern] [+linenum] [fileNames..]

命令參數:?

常用功能鍵

Enter????向下n行,需要定義。默認為1行

  Ctrl+F???向下滾動一屏

  空格鍵??向下滾動一屏

  b或Ctrl+b ?返回上一屏

  /字符串 向下查詢“字符串”關鍵字

  =???????輸出當前行的行號

  :f?????輸出文件名和當前行的行號

  V??????調用vi編輯器

  !命令???調用Shell,并執行命令?

  q???????退出more

less命令可以使用上下等按鍵功能來往前往后翻看文件,而more沒有辦法向前翻

3、head——數據選取命令( 默認前十行)。

命令格式: head [-n number] file

      [hao@hao ~]$ 
      
        head
      
       /etc/
      
        man
      
      .config
    
      [hao@hao ~]$ 
      
        head
      
       -n 
      
        20
      
       /etc/
      
        man
      
      .config
    

?4、tail——取出后面幾行

命令格式 :tail [-n -number] file

      [hao@hao ~]$ 
      
        tail
      
       /etc/
      
        man
      
      
        .config 

[hao@hao 
      
      ~]$ 
      
        tail
      
       -n -
      
        20
      
       /etc/
      
        man
      
      .config 
    

如果不知道文件有幾行,但是想列出100行以后的數據時

      [hao@hao ~]$ 
      
        tail
      
       -n +
      
        100
      
       /etc/main.config
    

命令參數:

  -f(follow):即時輸出追加到文件末尾的新內容,該參數可以用來監控日志文件等

5、touch——修改文件時間或者創建新文件

文件時間:在linux下,有3個時間參數,用來記錄每個文件的主要變動時間。可以用stat命令來查看這三個時間。

a、access time(atime):訪問時間。當對文件進行讀取時,會改變這個時間。如使用cat.more.less,tail,head,cp等命令。ls命令不會更改該時間

      
        hao@hao ~]$ touch /tmp/tstouch.txt

[hao@hao ~]$ stat /tmp/
      
      
        
          tstouch.txt
        
         

  File: `
      
      /tmp/tstouch.txt
      
        '


      
        Size: 
      
        0
      
                   Blocks: 
      
        0
      
                IO Block: 
      
        4096
      
         regular empty 
      
        file
      
      
        

Device: 803h
      
      /2051d    Inode: 
      
        133739
      
            Links: 
      
        1
      
      
        

Access: (
      
      
        0664
      
      /-rw-rw-r--)  Uid: (  
      
        500
      
      /     hao)   Gid: (  
      
        500
      
      /
      
             hao)

Access: 
      
      
        2014
      
      -
      
        08
      
      -
      
        05
      
      
        13
      
      :
      
        39
      
      :
      
        39.387999895
      
       +
      
        0800
      
      
        

Modify: 
      
      
        2014
      
      -
      
        08
      
      -
      
        05
      
      
        13
      
      :
      
        39
      
      :
      
        39.387999895
      
       +
      
        0800
      
      
        

Change: 
      
      
        2014
      
      -
      
        08
      
      -
      
        05
      
      
        13
      
      :
      
        39
      
      :
      
        39.387999895
      
       +
      
        0800
      
      
        

[hao@hao ~]$ cat /tmp/tstouch.txt 

[hao@hao ~]$ stat /tmp/
      
      
        
          tstouch.txt
        
         

  File: `
      
      /tmp/tstouch.txt
      
        '


      
        Size: 
      
        0
      
                   Blocks: 
      
        0
      
                IO Block: 
      
        4096
      
         regular empty 
      
        file
      
      
        

Device: 803h
      
      /2051d    Inode: 
      
        133739
      
            Links: 
      
        1
      
      
        

Access: (
      
      
        0664
      
      /-rw-rw-r--)  Uid: (  
      
        500
      
      /     hao)   Gid: (  
      
        500
      
      /
      
             hao)

Access: 
      
      
        2014
      
      -
      
        08
      
      -
      
        05
      
      
        13
      
      :
      
        40
      
      :
      
        12.682999977
      
       +
      
        0800
      
      
        

Modify: 
      
      
        2014
      
      -
      
        08
      
      -
      
        05
      
      
        13
      
      :
      
        39
      
      :
      
        39.387999895
      
       +
      
        0800
      
      
        

Change: 
      
      
        2014
      
      -
      
        08
      
      -
      
        05
      
      
        13
      
      :
      
        39
      
      :
      
        39.387999895
      
       +
      
        0800
      
      
        

[hao@hao ~]$ ls /tmp/
      
      
        
          tstouch.txt
        
      
      /tmp/
      
        tstouch.txt


        
          [hao@hao 
        
      
      
        ~]$ stat /tmp/
      
      
        
          tstouch.txt
        
         

  File: `
      
      /tmp/tstouch.txt
      
        '


      
        Size: 
      
        0
      
                   Blocks: 
      
        0
      
                IO Block: 
      
        4096
      
         regular empty 
      
        file
      
      
        

Device: 803h
      
      /2051d    Inode: 
      
        133739
      
            Links: 
      
        1
      
      
        

Access: (
      
      
        0664
      
      /-rw-rw-r--)  Uid: (  
      
        500
      
      /     hao)   Gid: (  
      
        500
      
      /
      
             hao)

Access: 
      
      
        2014
      
      -
      
        08
      
      -
      
        05
      
      
        13
      
      :
      
        40
      
      :
      
        12.682999977
      
       +
      
        0800
      
      
        

Modify: 
      
      
        2014
      
      -
      
        08
      
      -
      
        05
      
      
        13
      
      :
      
        39
      
      :
      
        39.387999895
      
       +
      
        0800
      
      
        

Change: 
      
      
        2014
      
      -
      
        08
      
      -
      
        05
      
      
        13
      
      :
      
        39
      
      :
      
        39.387999895
      
       +
      
        0800
      
    

b、 modification time(mtime):修改時間。當文件內容修改時,會改變該時間。如命令vi改變內容時,會改變該時間。使用ll命令默認顯示該時間參數

      
        [hao@hao ~]$ vi /tmp/tstouch.txt 

[hao@hao ~]$ cat /tmp/
      
      
        
          tstouch.txt
        
         

hello
      
      ! world!
      
        

[hao@hao ~]$ stat /tmp/
      
      
        
          tstouch.txt
        
         

  File: `
      
      /tmp/tstouch.txt
      
        '


      
        Size: 
      
        14
      
                  Blocks: 
      
        8
      
                IO Block: 
      
        4096
      
         regular 
      
        file
      
      
        

Device: 803h
      
      /2051d    Inode: 
      
        133739
      
            Links: 
      
        1
      
      
        

Access: (
      
      
        0664
      
      /-rw-rw-r--)  Uid: (  
      
        500
      
      /     hao)   Gid: (  
      
        500
      
      /
      
             hao)

Access: 
      
      
        2014
      
      -
      
        08
      
      -
      
        05
      
      
        13
      
      :
      
        51
      
      :
      
        01.097999996
      
       +
      
        0800
      
      
        

Modify: 
      
      
        2014
      
      -
      
        08
      
      -
      
        05
      
      
        13
      
      :
      
        50
      
      :
      
        51.630999942
      
       +
      
        0800
      
      
        

Change: 
      
      
        2014
      
      -
      
        08
      
      -
      
        05
      
      
        13
      
      :
      
        50
      
      :
      
        51.633999974
      
       +
      
        0800
      
    

c、status time(ctime):狀態時間。是該文件的i節點最后一次被修改的時間,通過chmod、chown命令修改一次文件屬性,這個時間就會更新。

命令格式:touch [options] file

命令參數:

  -a(access time):僅修改訪問時間.

  -m(modification time):僅改變修改時間。

  -t(stamp):后面可以接欲修改的時間而不用目前的時間。格式為[YYMMDDhhmm]

      
        [hao@hao ~]$ touch -a /tmp/tstouch.txt

[hao@hao ~]$ stat /tmp/
      
      
        
          tstouch.txt
        
         

  File: `
      
      /tmp/tstouch.txt
      
        '


      
        Size: 
      
        14
      
                  Blocks: 
      
        8
      
                IO Block: 
      
        4096
      
         regular 
      
        file
      
      
        

Device: 803h
      
      /2051d    Inode: 
      
        133739
      
            Links: 
      
        1
      
      
        

Access: (
      
      
        0664
      
      /-rw-rw-r--)  Uid: (  
      
        500
      
      /     hao)   Gid: (  
      
        500
      
      /
      
             hao)

Access: 
      
      
        2014
      
      -
      
        08
      
      -
      
        05
      
      
        13
      
      :
      
        56
      
      :
      
        56.491999787
      
       +
      
        0800
      
      
        

Modify: 
      
      
        2014
      
      -
      
        08
      
      -
      
        05
      
      
        13
      
      :
      
        50
      
      :
      
        51.630999942
      
       +
      
        0800
      
      
        

Change: 
      
      
        2014
      
      -
      
        08
      
      -
      
        05
      
      
        13
      
      :
      
        56
      
      :
      
        56.491999787
      
       +
      
        0800
      
      
        

[hao@hao ~]$ touch -m /tmp/tstouch.txt

[hao@hao ~]$ stat /tmp/
      
      
        
          tstouch.txt
        
         

  File: `
      
      /tmp/tstouch.txt
      
        '


      
        Size: 
      
        14
      
                  Blocks: 
      
        8
      
                IO Block: 
      
        4096
      
         regular 
      
        file
      
      
        

Device: 803h
      
      /2051d    Inode: 
      
        133739
      
            Links: 
      
        1
      
      
        

Access: (
      
      
        0664
      
      /-rw-rw-r--)  Uid: (  
      
        500
      
      /     hao)   Gid: (  
      
        500
      
      /
      
             hao)

Access: 
      
      
        2014
      
      -
      
        08
      
      -
      
        05
      
      
        13
      
      :
      
        56
      
      :
      
        56.491999787
      
       +
      
        0800
      
      
        

Modify: 
      
      
        2014
      
      -
      
        08
      
      -
      
        05
      
      
        14
      
      :
      
        06
      
      :
      
        10.507000067
      
       +
      
        0800
      
      
        

Change: 
      
      
        2014
      
      -
      
        08
      
      -
      
        05
      
      
        14
      
      :
      
        06
      
      :
      
        10.507000067
      
       +
      
        0800
      
      
        

[hao@hao ~]$ touch -t 1408050206 /tmp/tstouch.txt

[hao@hao ~]$ stat /tmp/
      
      
        
          tstouch.txt
        
         

  File: `
      
      /tmp/tstouch.txt
      
        '


      
        Size: 
      
        14
      
                  Blocks: 
      
        8
      
                IO Block: 
      
        4096
      
         regular 
      
        file
      
      
        

Device: 803h
      
      /2051d    Inode: 
      
        133739
      
            Links: 
      
        1
      
      
        

Access: (
      
      
        0664
      
      /-rw-rw-r--)  Uid: (  
      
        500
      
      /     hao)   Gid: (  
      
        500
      
      /
      
             hao)

Access: 
      
      
        2014
      
      -
      
        08
      
      -
      
        05
      
      
        02
      
      :
      
        06
      
      :
      
        00.000000000
      
       +
      
        0800
      
      
        

Modify: 
      
      
        2014
      
      -
      
        08
      
      -
      
        05
      
      
        02
      
      :
      
        06
      
      :
      
        00.000000000
      
       +
      
        0800
      
      
        

Change: 
      
      
        2014
      
      -
      
        08
      
      -
      
        05
      
      
        14
      
      :
      
        07
      
      :
      
        21.688000461
      
       +
      
        0800
      
    

6、cut——分割文件中的字符串

命令格式 :cut [options] [file]

命令參數:

  -b(bytes):只選中指定的這些字節。

  -c(characters):只選擇指定的這些字符。

  -d(delimiter):使用指定分界符代替制表符作為區域分界。

  -f(fields):只選中指定的這些域,并打印所有不包含分界符的行。

  這里-3,代表的是選擇第一個到第三個字符,3-5則是選取第三個到第五個字符,3-5,8則是選取第三個到第五個字符以及第八個字符。

      
        [hao@hao ~]$
      
      
        cat
      
       /tmp/
      
        passwd
      
      
        

root:x:
      
      
        0
      
      :
      
        0
      
      :root:/root:/bin/
      
        bash

bin:x:
      
      
        1
      
      :
      
        1
      
      :bin:/bin:/sbin/
      
        nologin

daemon:x:
      
      
        2
      
      :
      
        2
      
      :daemon:/sbin:/sbin/
      
        nologin

adm:x:
      
      
        3
      
      :
      
        4
      
      :adm:/var/adm:/sbin/
      
        nologin

...


        
          [hao@hao 
        
      
      ~]$ 
      
        cut
      
       -b -
      
        3
      
       /tmp/
      
        passwd
      
      
        

roo

bin

dae

adm

...


        
          [hao@hao 
        
      
      ~]$ 
      
        cut
      
       -b 
      
        3
      
      -
      
        5
      
      ,
      
        8
      
       /tmp/
      
        passwd
      
      
        

ot:
      
      
        0
      
      
        

n:x:

emox

m:x:
        
...

  -c的用法和-b差不多,只不過是截取的字符,而不是字節。漢字也被認為是一個字符。

  -d后面根的冒號是分割文件行的的分割符,-f用來設置提取第幾個域。-d一般情況下根-f一起使用,而不能和-b,-c一起使用

      
        
          [hao@hao ~]$
        
      
      
        cut
      
       -f 
      
        1
      
       /tmp/
      
        passwd
      
      |
      
        head
      
       -n 
      
        5
      
      
        

root:x:
      
      
        0
      
      :
      
        0
      
      :root:/root:/bin/
      
        bash

bin:x:
      
      
        1
      
      :
      
        1
      
      :bin:/bin:/sbin/
      
        nologin

daemon:x:
      
      
        2
      
      :
      
        2
      
      :daemon:/sbin:/sbin/
      
        nologin

adm:x:
      
      
        3
      
      :
      
        4
      
      :adm:/var/adm:/sbin/
      
        nologin


      
      
        lp
      
      :x:
      
        4
      
      :
      
        7
      
      :
      
        lp
      
      :/var/spool/lpd:/sbin/
      
        nologin


        
          
            [hao@hao 
          
        
      
      
        
          ~]$
        
      
      
        cut
      
       -d : -f 
      
        1
      
       /tmp/
      
        passwd
      
      |
      
        head
      
       -n 
      
        5
      
      
        

root

bin

daemon

adm


      
      
        lp
      
      
        
          
             [hao@hao
          
        
      
      
        ~]$
      
      
        cut
      
       -d : -f 
      
        1
      
      ,
      
        2
      
       /tmp/
      
        passwd
      
      |
      
        head
      
       -n 
      
        5
      
      
        

root:x

bin:x

daemon:x

adm:x


      
      
        lp
      
      
        :x


        
          
            [hao@hao 
          
        
      
      
        
          ~]$
        
         c
      
      ut -d : -f 
      
        2
      
      -
      
        3
      
      ,
      
        5
      
       /tmp/
      
        passwd
      
      |
      
        head
      
       -n 
      
        5
      
      
        

x:
      
      
        0
      
      
        :root

x:
      
      
        1
      
      
        :bin

x:
      
      
        2
      
      
        :daemon

x:
      
      
        3
      
      
        :adm

x:
      
      
        4
      
      :
      
        lp
      
    

7、awk

8、sed

9、grep

10.sort——排序命令

  不會改變源文件的次序,只會改變顯示的次序。默認排序方法,根據字符ASCII碼值得升序排序。

命令格式: sort [options] [file]

命令參數:

  -n(number):按數值大小排序

  -r(reverse):降序排序

  -t(field separator):指定字符分隔符

  -k(key):指定字段

  -f(ignore-case):不區分大小寫

  -u(unique):相同內容只顯示一次。這里的相同是指 相鄰 并且內容相同。

      
        [hao@hao ~]$ cat /etc/
      
      
        
          fstab
        
        



#

# 
      
      /etc/
      
        fstab

# Created by anaconda on Sat Jul 
      
      
        26
      
      
        20
      
      :
      
        12
      
      :
      
        53
      
      
        2014
      
      
        

#

# Accessible filesystems, by reference, are maintained under 
      
      
        '
      
      
        /dev/disk
      
      
        '
      
      
        

# See 
      
      
        man
      
       pages fstab(
      
        5
      
      ), findfs(
      
        8
      
      ), 
      
        mount
      
      (
      
        8
      
      ) and/or blkid(
      
        8
      
      ) 
      
        for
      
      
        more
      
      
        info
      
      
        

#

UUID
      
      =dfdbed34-b8ea-4f72-859c-58d511c2c5f9 /                       ext4    defaults        
      
        1
      
      
        1
      
      
        

UUID
      
      =96cf0fa8-75f7-
      
        4215
      
      -ab4e-ed9e1f223a41 /boot                   ext4    defaults        
      
        1
      
      
        2
      
      
        

UUID
      
      =3c71e294-3ab0-4bfa-ad86-a38fa51919d2 swap                    swap    defaults        
      
        0
      
      
        0
      
      
        

tmpfs                   
      
      /dev/shm                tmpfs   defaults        
      
        0
      
      
        0
      
      
        

devpts                  
      
      /dev/pts                devpts  gid=
      
        5
      
      ,mode=
      
        620
      
      
        0
      
      
        0
      
      
        

sysfs                   
      
      /sys                    sysfs   defaults        
      
        0
      
      
        0
      
      
        

proc                    
      
      /proc                   proc    defaults        
      
        0
      
      
        0
      
      
        
           [hao@hao ~]$ sort /etc/
        
      
      
        
          
            fstab
          
        
        



#

#

#

# Accessible filesystems, by reference, are maintained under 
      
      
        '
      
      
        /dev/disk
      
      
        '
      
      
        

# Created by anaconda on Sat Jul 
      
      
        26
      
      
        20
      
      :
      
        12
      
      :
      
        53
      
      
        2014
      
      
        

devpts                  
      
      /dev/pts                devpts  gid=
      
        5
      
      ,mode=
      
        620
      
      
        0
      
      
        0
      
      
        

# 
      
      /etc/
      
        fstab

proc                    
      
      /proc                   proc    defaults        
      
        0
      
      
        0
      
      
        

# See 
      
      
        man
      
       pages fstab(
      
        5
      
      ), findfs(
      
        8
      
      ), 
      
        mount
      
      (
      
        8
      
      ) and/or blkid(
      
        8
      
      ) 
      
        for
      
      
        more
      
      
        info
      
      
        

sysfs                   
      
      /sys                    sysfs   defaults        
      
        0
      
      
        0
      
      
        

tmpfs                   
      
      /dev/shm                tmpfs   defaults        
      
        0
      
      
        0
      
      
        

UUID
      
      =3c71e294-3ab0-4bfa-ad86-a38fa51919d2 swap                    swap    defaults        
      
        0
      
      
        0
      
      
        

UUID
      
      =96cf0fa8-75f7-
      
        4215
      
      -ab4e-ed9e1f223a41 /boot                   ext4    defaults        
      
        1
      
      
        2
      
      
        

UUID
      
      =dfdbed34-b8ea-4f72-859c-58d511c2c5f9 /                       ext4    defaults        
      
        1
      
      
        1
      
    

  把/tmp/passwd按照第三個字段的數值大小排序

      
        
          [hao@hao ~]$
        
      
      
        sort
      
       -t : -k 
      
        3
      
       -n /tmp/
      
        passwd
      
      
        

root:x:
      
      
        0
      
      :
      
        0
      
      :root:/root:/bin/
      
        bash

bin:x:
      
      
        1
      
      :
      
        1
      
      :bin:/bin:/sbin/
      
        nologin

daemon:x:
      
      
        2
      
      :
      
        2
      
      :daemon:/sbin:/sbin/
      
        nologin

adm:x:
      
      
        3
      
      :
      
        4
      
      :adm:/var/adm:/sbin/
      
        nologin


      
      
        lp
      
      :x:
      
        4
      
      :
      
        7
      
      :
      
        lp
      
      :/var/spool/lpd:/sbin/
      
        nologin


      
      
        sync
      
      :x:
      
        5
      
      :
      
        0
      
      :
      
        sync
      
      :/sbin:/bin/
      
        sync
      
      
        

shutdown:x:
      
      
        6
      
      :
      
        0
      
      :shutdown:/sbin:/sbin/
      
        shutdown

halt:x:
      
      
        7
      
      :
      
        0
      
      :halt:/sbin:/sbin/
      
        halt

mail:x:
      
      
        8
      
      :
      
        12
      
      :mail:/var/spool/mail:/sbin/
      
        nologin

uucp:x:
      
      
        10
      
      :
      
        14
      
      :uucp:/var/spool/uucp:/sbin/
      
        nologin

operator:x:
      
      
        11
      
      :
      
        0
      
      :operator:/root:/sbin/
      
        nologin

games:x:
      
      
        12
      
      :
      
        100
      
      :games:/usr/games:/sbin/
      
        nologin

gopher:x:
      
      
        13
      
      :
      
        30
      
      :gopher:/var/gopher:/sbin/
      
        nologin


      
      
        ftp
      
      :x:
      
        14
      
      :
      
        50
      
      :FTP User:/var/
      
        ftp
      
      :/sbin/
      
        nologin

rpcuser:x:
      
      
        29
      
      :
      
        29
      
      :RPC Service User:/var/lib/nfs:/sbin/
      
        nologin

rpc:x:
      
      
        32
      
      :
      
        32
      
      :Rpcbind Daemon:/var/cache/rpcbind:/sbin/
      
        nologin

ntp:x:
      
      
        38
      
      :
      
        38
      
      ::/etc/ntp:/sbin/
      
        nologin

gdm:x:
      
      
        42
      
      :
      
        42
      
      ::/var/lib/gdm:/sbin/
      
        nologin

apache:x:
      
      
        48
      
      :
      
        48
      
      :Apache:/var/www:/sbin/
      
        nologin

webalizer:x:
      
      
        67
      
      :
      
        67
      
      :Webalizer:/var/www/usage:/sbin/
      
        nologin

haldaemon:x:
      
      
        68
      
      :
      
        68
      
      :HAL daemon:/:/sbin/
      
        nologin

vcsa:x:
      
      
        69
      
      :
      
        69
      
      :virtual console memory owner:/dev:/sbin/
      
        nologin

tcpdump:x:
      
      
        72
      
      :
      
        72
      
      ::/:/sbin/
      
        nologin

sshd:x:
      
      
        74
      
      :
      
        74
      
      :Privilege-separated SSH:/var/empty/sshd:/sbin/
      
        nologin

dbus:x:
      
      
        81
      
      :
      
        81
      
      :System message bus:/:/sbin/
      
        nologin

postfix:x:
      
      
        89
      
      :
      
        89
      
      ::/var/spool/postfix:/sbin/
      
        nologin

tomcat:x:
      
      
        91
      
      :
      
        91
      
      :Apache Tomcat:/usr/share/tomcat6:/sbin/
      
        nologin

nobody:x:
      
      
        99
      
      :
      
        99
      
      :Nobody:/:/sbin/
      
        nologin

usbmuxd:x:
      
      
        113
      
      :
      
        113
      
      :usbmuxd user:/:/sbin/
      
        nologin

avahi
      
      -autoipd:x:
      
        170
      
      :
      
        170
      
      :Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/
      
        nologin

abrt:x:
      
      
        173
      
      :
      
        173
      
      ::/etc/abrt:/sbin/
      
        nologin

pulse:x:
      
      
        497
      
      :
      
        496
      
      :PulseAudio System Daemon:/var/run/pulse:/sbin/
      
        nologin

saslauth:x:
      
      
        498
      
      :
      
        76
      
      :
      
        "
      
      
        Saslauthd user
      
      
        "
      
      :/var/empty/saslauth:/sbin/
      
        nologin

rtkit:x:
      
      
        499
      
      :
      
        497
      
      :RealtimeKit:/proc:/sbin/
      
        nologin

hao:x:
      
      
        500
      
      :
      
        500
      
      :haohao:/home/hao:/bin/
      
        bash

nfsnobody:x:
      
      
        65534
      
      :
      
        65534
      
      :Anonymous NFS User:/var/lib/nfs:/sbin/nologin
    

?

      [hao@hao ~]$ 
      
        sort
      
        /tmp/
      
        tssort.txt 


      
      
        111
      
      
        111
      
      
        231
      
      
        456
      
      
        456
      
      
        67
      
      
        7890
      
      
        

[hao@hao 
      
      ~]$ 
      
        sort
      
       -n  /tmp/
      
        tssort.txt 


      
      
        67
      
      
        111
      
      
        111
      
      
        231
      
      
        456
      
      
        456
      
      
        7890
      
      
        
           [hao@hao ~]$
        
      
      
        sort
      
       -u -n /tmp/
      
        tssort.txt 


      
      
        67
      
      
        111
      
      
        231
      
      
        456
      
      
        7890
      
    

11.uniq——報告相同的行,也可以去除相同的行

  這里的相同的行也是指相鄰并且內容相同的行

命令參數:

  -d(repeated):打印出重復的行

  -c(count):顯示行 重復 的次數

      
        
          [hao@hao ~]$
        
      
      
        cat
      
       /tmp/
      
        tssort.txt


      
      
        456
      
      
        456
      
      
        67
      
      
        111
      
      
        231
      
      
        456
      
      
        7890
      
      
        111
      
      
        
           [hao@hao ~]$
        
      
      
        uniq
      
       /tmp/
      
        tssort.txt       


      
      
        456
      
      
        67
      
      
        111
      
      
        231
      
      
        456
      
      
        7890
      
      
        111
      
    

?

      
        
          [hao@hao ~]$
        
      
      
        uniq
      
       -d /tmp/
      
        tssort.txt


      
      
        456
      
    

?

      
        
          [hao@hao ~]$
        
      
      
        uniq
      
       -c /tmp/
      
        tssort.txt

      
      
      
        2
      
      
        456
      
      
        1
      
      
        67
      
      
        1
      
      
        111
      
      
        1
      
      
        231
      
      
        1
      
      
        456
      
      
        1
      
      
        7890
      
      
        1
      
      
        111
      
    

12.wc(word count)——文本統計

命令參數:

  -l(line):只顯示行數

  -w(word):只顯示單詞數

  -c(bytes):只顯示字節數

  -m(chars):只顯示字符數

  -L(max-line-length):最長一行包含的字節數

  下面的命令輸出含義,15行,78個單詞,805個字節(包括空白)

      
        
          [hao@hao ~]$
        
      
      
        wc
      
       /etc/
      
        fstab          

 
      
      
        15
      
      
        78
      
      
        805
      
       /etc/fstab
    

?

      [hao@hao ~]$ 
      
        wc
      
       -L /etc/
      
        fstab


      
      
        93
      
       /etc/fstab
    

?

13.tr(translate or remove)——轉換或刪除字符

命令格式: tr [OPTION]... SET1 [SET2]

命令參數:

  -d(delete):刪除指定字符

  將小寫字母a,b轉換為大寫字母A、B

?

      
        
          [hao@hao ~]$
        
      
      
        tr
      
      
        '
      
      
        ab
      
      
        '
      
      
        '
      
      
        AB
      
      
        '
      
      
        

acd

Acd

abcd

ABcd

cd

cd

acdb        

AcdB
      
    

?

  由上面可以看出,tr可以直接對終端界面進行輸入并且替換。命令格式中并沒有對指定文件進行轉換,因此,若要對文件進行轉換,則需要用到輸入重定向。

      
        
          [hao@hao ~]$ 
        
      
      
        tr
      
      
        '
      
      
        ab
      
      
        '
      
      
        '
      
      
        AB
      
      
        '
      
       < /tmp/
      
        passwd
      
      |
      
        head
      
       -n 
      
        5
      
      
        

root:x:
      
      
        0
      
      :
      
        0
      
      :root:/root:/Bin/
      
        BAsh

Bin:x:
      
      
        1
      
      :
      
        1
      
      :Bin:/Bin:/sBin/
      
        nologin

dAemon:x:
      
      
        2
      
      :
      
        2
      
      :dAemon:/sBin:/sBin/
      
        nologin

Adm:x:
      
      
        3
      
      :
      
        4
      
      :Adm:/vAr/Adm:/sBin/
      
        nologin


      
      
        lp
      
      :x:
      
        4
      
      :
      
        7
      
      :
      
        lp
      
      :/vAr/spool/lpd:/sBin/nologin
    

將所有的小寫字母變成大寫字母

      [hao@hao ~]$ 
      
        tr
      
      
        '
      
      
        a-z
      
      
        '
      
      
        '
      
      
        A-Z
      
      
        '
      
    

  刪除字母a、b

      
        
          [hao@hao ~]$ tr -d 'ab'
        
      
      
                           

abc

c

ac

c

acb

c
      
    

?

《鳥哥的Linux私房菜》學習筆記(1)——文件與目錄


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 国内国产精品天干天干 | 影音先锋在线亚洲精品推荐 | 欧美视频色 | 成人高清毛片a | 久久久99精品免费观看精品 | 色老老精品偷偷鲁 | 国产精彩视频 | 99中文字幕 | 99久久香蕉国产综合影院 | 免费一看一级毛片人 | 国产亚洲一区二区在线观看 | 日本日韩欧美 | 美女被爆羞羞视频网站视频 | 麻豆国产在线不卡一区二区 | 国产精品久久久久久久久 | 成人一区二区免费中文字幕 | 91视频老司机 | 97最新网址 | 特黄一级大片 | 九九九精品在线观看 | 久久一日本道色综合久久m 久久一色本道亚洲 | 久久久久久全国免费观看 | 日韩欧美亚洲综合一区二区 | 欧洲美女高清一级毛片 | 伊人热人久久中文字幕 | 亚洲精品国产免费 | 成人国产综合 | 亚洲日韩成人 | 五月天婷婷网站 | 久久久精品视频在线观看 | 亚洲一级毛片在线播放 | 97精品国产自在现线免费 | 久久九色综合九色99伊人 | 久久精品爱 | 色综合五月天 | 欧美性猛交xxxx免费看久久 | 夜夜穞狠狠穞 | 大片在线播放日本一级毛片 | 亚洲天堂777| 国产女人水多毛片18 | 天天干天天碰 |