文件管理及常用命令(一)

cat命令:

-n 增加行號 空行也增加行號
    
    [root@localhost ~]# cat f1
    abcd
    abcd
    
    abcd
    
    
    
    abcd
    
    [root@localhost ~]# cat -n f1
     1	abcd     2	abcd     3	
     4	abcd     5	
     6	
     7	
     8	abcd     9	-b 增加行號 空行跳過不加行號

    [root@localhost ~]# cat -b f1
     1	abcd     2	abcd     3	abcd     4	abcd


-s 將相鄰多余空行壓縮
    
    [root@localhost ~]# cat -s f1
    abcd
    abcd
    
    abcd
    
    abcd

-T 查看tab鍵
-v 查看文件的^M字符(也就是Windows下的空格)
-A 顯示文件里所有的字符
    
    [root@localhost ~]# cat -A f1
    abcd$    abcd$    $    abcd$    $    $    $    abcd$    $-E 顯示行結束符$  與-A的不同是——E顯示的只是$符號,-A顯示的是所有的字符
    
    [root@localhost ~]# cat -E f1
    abcd$    abcd$    $    abcd$    $    $    $    abcd$    $

tac命令:

倒序查看文件內容
    
    [root@localhost ~]# cat f1
    abcd
    efgh
    mnbv
    lkjh
    poiu
    qwer
    
    [root@localhost ~]# tac f1
    
    qwer
    poiu
    lkjh
    mnbv
    efgh
    abcd

rev命令:

反向顯示每一行(按字符)
    [root@localhost ~]# cat f1
    abcdefghijklmn
    [root@localhost ~]# rev f1
    nmlkjihgfedcba

more命令:只能向下翻

-d 給出提示(空格翻頁,q退出,回車翻一行)

less命令:

/字符串    n順著方向搜 N逆著方向搜  向下
?字符串  與/相反,n向上 N向下

顯示文本前或后行的內容

  • head命令:

    head [OPTION]... [FILE]...
      -c #:  指定獲取前# 字節
      -n #:  指定獲取前#行 行
    
      [root@localhost ~]# head f1
      abcaskdfj
      asfdkjhja
      lasdkfjldf
      lkjsloiweuoiuw
      werqwerttqw
      
      [root@localhost ~]# head -n 2 f1
      abcaskdfj
      asfdkjhja

    -#:指定行數

    [root@localhost ~]# head -4 f1
      abcaskdfj
      asfdkjhja
      lasdkfjldf
      lkjsloiweuoiuw
    
    
     默認情況下是head是查看10行 可以自己修改默認查看行數
  • tail 命令:

    tail -n # file 
      查看后#行   
      
      [root@localhost ~]# tail -n 2 f1
      werqwerttqw  -f:  跟蹤顯示文件新追加的內容,
          tail -n 0 -f /var/log/messages &
          實時查看日志最新信息(只顯示最新的,老日志不顯示)
      -c查看后#字節

按列抽取文本cut和合并文件paste

  • cut命令:

    -d DELIMITER:  指明分隔符,默認tab
      -f FILEDS:  #:  第# 個字段
    
      [root@localhost ~]# cat f1
       abc bcd efg hij lmn
      [root@localhost ~]# cat f1 |cut -d ' ' -f 2
      abc  #,#[,#] :離散的多個字段,例如1,3,6
      
      [root@localhost ~]# cat f1 |cut -d ' ' -f 2,3
      abc bcd  #-# :連續的多個字段,  例如1-6
          [root@localhost ~]# cat f1 |cut -d ' ' -f 2-5
          abc bcd efg hij
    
      混合使用:1-3,7
          [root@localhost ~]# cat f1 |cut -d ' ' -f 2-5,6 
          abc bcd efg hij lmn
    
      -c 按字符切割
      --output-delimiter=STRING
      
      cut -d : -f 1,3 file
      cut -c 44-46
      cut -d : -f 1,3  --output-delimiter=“ ” file
    
      [root@localhost ~]# df | tr -s ' '
      Filesystem 1K-blocks Used Available Use% Mounted on
      /dev/mapper/vg0-root 20511356 343196 19119584 2% /
      tmpfs 502068 0 502068 0% /dev/shm
      /dev/sda1 194241 34117 149884 19% /boot
      /dev/mapper/vg0-usr 10190136 1962620 7703228 21% /usr
      /dev/mapper/vg0-var 20511356 122168 19340612 1% /var
      [root@localhost ~]# df | tr -s ' '|tr ' ' ':'
      Filesystem:1K-blocks:Used:Available:Use%:Mounted:on
      /dev/mapper/vg0-root:20511356:343196:19119584:2%:/
      tmpfs:502068:0:502068:0%:/dev/shm
      /dev/sda1:194241:34117:149884:19%:/boot
      /dev/mapper/vg0-usr:10190136:1962620:7703228:21%:/usr
      /dev/mapper/vg0-var:20511356:122168:19340612:1%:/var
      [root@localhost ~]# df | tr -s ' '|tr ' ' ':'|cut -d: -f 5
      Use%  2%  0%  19%  21%  1%
      [root@localhost ~]# df | tr -s ' '|tr ' ' ':'|cut -d: -f 5|cut -d% -f 1
      Use  2
      0
      19
      21
      1
      [root@localhost ~]#

paste命令:

paste  合并兩個文件同行號的列到一行
paste [OPTION]... [FILE]...
-d  分隔符: 指定分隔符,默認用TAB
-s :  所有行合成一行顯示
paste f1 f2
paste -s f1 f2
    [root@localhost ~]# paste -s f1 f2
     abc bcd efg hij lmn    123 456 678 987 9765
    [root@localhost ~]# paste  f1 f2
     abc bcd efg hij lmn	123 456 678 987 9765
    [root@localhost ~]# paste -d ':' f1 f2
     abc bcd efg hij lmn:123 456 678 987 9765

收集文本統計數據wc

計數單詞總數、行總數、字節總數和字符總數
可以對文件或STDIN 中的數據運行
[root@localhost ~]# cat f1 
 abc bcd efg hij lmn
[root@localhost ~]# wc f1
 1    5    21    f1
行數 字數 字符數 文件名

使用 -l 來只計數行數
    
    [root@localhost ~]# cat f1
     abc bcd efg hij lmn
    qwertyuiolkgfdsazxc 
    [root@localhost ~]# wc -l f1
    2 f1

使用 -w 來只計數單詞總數

    [root@localhost ~]# wc -w f1
    6 f1

使用 -c 來只計數字節總數
    
    [root@localhost ~]# wc -c f1
    42 f1

使用 -m 來只計數字符總數
  
    [root@localhost ~]# wc -m f1
    42 f1

文本排序sort

把整理過的文本顯示在STDOUT ,不改變原始文件
 sort [options] file(s)
常用選項
-r 執行反方向(由上至下)整理
    
    [root@localhost ~]# df | sort -r 
    tmpfs             500680    6764    493916   2% /run
    tmpfs             500680       0    500680   0% /sys/fs/cgroup
    tmpfs             500680       0    500680   0% /dev/shm
    tmpfs             100136       0    100136   0% /run/user/0
    Filesystem     1K-blocks    Used Available Use% Mounted on
    devtmpfs          486152       0    486152   0% /dev
    /dev/sda3       20961280 2670020  18291260  13% /usr
    /dev/sda2       41922560  584656  41337904   2% /
    /dev/sda1         496300  140508    355792  29% /boot

-n 執行按數字大小整理
-f 選項忽略(fold )字符串中的字符大小寫
-u 選項(獨特,unique )刪除輸出中的重復行
    
    [root@localhost ~]# cat f1
    abc
    abc
    abc
    bcd
    abc
    abc
    [root@localhost ~]# cat f1|sort -u
    abc
    bcd

-t c 選項使用c 做為字段界定符
-k X 選項按照使用c 字符分隔的X 列來整理能夠使用多次
    [root@localhost ~]# df |tail -8|tr -s ' ' ':' | sort -t : -k2 
    tmpfs:100136:0:100136:0%:/run/user/0
    /dev/sda3:20961280:2670020:18291260:13%:/usr
    /dev/sda2:41922560:584656:41337904:2%:/
    devtmpfs:486152:0:486152:0%:/dev
    /dev/sda1:496300:140508:355792:29%:/boot
    tmpfs:500680:0:500680:0%:/dev/shm
    tmpfs:500680:0:500680:0%:/sys/fs/cgroup
    tmpfs:500680:6764:493916:2%:/run

uniq

uniq 命令:從輸入中刪除重復的前后相接的行
    
    [root@localhost ~]# cat f1
    abc
    abc
    abc
    bcd
    abc
    abc
    [root@localhost ~]# cat f1 |uniq
    abc
    bcd
    abc

uniq [OPTION]... [FILE]...
-c:  顯示每行重復出現的次數;

    [root@localhost ~]# cat f1 |uniq -c
      3 abc      1 bcd      2 abc

-d:  僅顯示重復過的行;
    
    [root@localhost ~]# cat f1 |uniq -d
    abc
    abc

-u:  僅顯示不曾重復的行;連續且完全相同方為重復
    
    [root@localhost ~]# cat f1 |uniq -u
    bcd


常和sort  命令一起配合使用:
    [root@localhost ~]# sort f1 |uniq -c
      5 abc      1 bcd

比較文件

比較兩個文件之間的區別
    $ diff foo.conf-broken foo.conf-works
    5c5
    < use_widgets = no
    ---
    > use_widgets = yes
    注明第5 行有區別(改變)
    
    
    復制對文件改變patch
        diff 命令的輸出被保存在一種叫做“補丁”的文件中
        使用 -u 選項來輸出“統一的(unified )”diff 格式文
        件,最適用于補丁文件。
        patch 命令復制在其它文件中進行的改變(要謹慎使用
        ?。?
        適用 -b 選項來自動備份改變了的文件
        $ diff -u foo.conf-broken foo.conf-works > foo.patch
        $ patch -b foo.conf-broken foo.patch

Linux上文本三劍客:

grep :文本過濾( 模式:pattern) 工具;    
    grep, egrep, fgrep (不 支持正則表達式 搜索)
sed :stream editor ,文本編輯工具;
awk :Linux 上的實現gawk ,文本報告生成器
  • grep

    grep: Global search REgular expression and Print out the line.
      作用:文本搜索工具,根據用戶指定的“模式”對目標文
      本逐行進行匹配檢查;打印匹配到的行;
      模式:由正則表達式字符及文本字符所編寫的過濾條件
      grep [OPTIONS] PATTERN [FILE...]
      grep root /etc/passwd
          
          [root@localhost ~]# grep root /etc/passwd
          root:x:0:0:root:/root:/bin/bash
          operator:x:11:0:operator:/root:/sbin/nologin
          rooter:x:1010:1010:rpc:/home/rooter:/bin/bash
    
      grep "$USER" /etc/passwd
          
          [root@localhost ~]# grep "$USER" /etc/passwd
          root:x:0:0:root:/root:/bin/bash
          operator:x:11:0:operator:/root:/sbin/nologin
          rooter:x:1010:1010:rpc:/home/rooter:/bin/bash
    
      grep '$USER' /etc/passwd
         單引號在這里是強引用 
          
      grep `whoami` /etc/passwd
          
          [root@localhost ~]# grep `whoami` /etc/passwd
          root:x:0:0:root:/root:/bin/bash
          operator:x:11:0:operator:/root:/sbin/nologin
          rooter:x:1010:1010:rpc:/home/rooter:/bin/bash

    選項:

    --color=auto:  對匹配到的文本著色顯示;
      -v:  顯示不能夠被pattern 匹配到的行;
          
          [root@localhost ~]# grep -v UUID /etc/fstab 
    
          #
          # /etc/fstab
          # Created by anaconda on Mon Jul 25 12:04:17 2016
          #
          # 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
          #
    
      -i:  忽略字符大小寫
          
          [root@localhost ~]# grep -i uuid /etc/fstab 
          UUID=f4406f6a-e495-45a0-a85e-3b059c0d3130 /                       xfs     defaults        0 0
          UUID=7c25120e-2371-413d-b584-fdd695b96702 /boot                   xfs     defaults        0 0
          UUID=19470291-724c-4f01-b6e1-7109ad22be1b /usr                    xfs     defaults        0 0
          UUID=c3460309-9e8c-4037-8684-4c6bdcabbacb swap                    swap    defaults        0 0
    
      -n:顯示匹配的行號
          
          [root@localhost ~]# grep -ni uuid /etc/fstab 
          9:UUID=f4406f6a-e495-45a0-a85e-3b059c0d3130 /                       xfs     defaults        0 0
          10:UUID=7c25120e-2371-413d-b584-fdd695b96702 /boot                   xfs     defaults        0 0
          11:UUID=19470291-724c-4f01-b6e1-7109ad22be1b /usr                    xfs     defaults        0 0
          12:UUID=c3460309-9e8c-4037-8684-4c6bdcabbacb swap                    swap    defaults        0 0
    
      -c:  統計匹配的行數
          
          [root@localhost ~]# grep -ci uuid /etc/fstab 
          4
    
      -o:  僅顯示匹配到的字符串;
          
          [root@localhost ~]# grep -o UUID /etc/fstab 
          UUID
          UUID
          UUID
          UUID
    
      -q:  靜默模式,不輸出任何信息
      -A # :after,  后#行 行
          [root@localhost ~]# grep -nA3 root /etc/passwd
          1:root:x:0:0:root:/root:/bin/bash
          2-bin:x:1:1:bin:/bin:/sbin/nologin
          3-daemon:x:2:2:daemon:/sbin:/sbin/nologin
          4-adm:x:3:4:adm:/var/adm:/sbin/nologin
          --      10:operator:x:11:0:operator:/root:/sbin/nologin
          11-games:x:12:100:games:/usr/games:/sbin/nologin
          12-ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
          13-nobody:x:99:99:Nobody:/:/sbin/nologin
    
      ? -B #: before,  前#行
          
          [root@localhost ~]# grep -nB3 root /etc/passwd
          1:root:x:0:0:root:/root:/bin/bash
          --      7-shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
          8-halt:x:7:0:halt:/sbin:/sbin/halt
          9-mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
          10:operator:x:11:0:operator:/root:/sbin/nologin
    
      ? -C # :context,  前后各#行
          
          [root@localhost ~]# grep -nC3 root /etc/passwd
          1:root:x:0:0:root:/root:/bin/bash
          2-bin:x:1:1:bin:/bin:/sbin/nologin
          3-daemon:x:2:2:daemon:/sbin:/sbin/nologin
          4-adm:x:3:4:adm:/var/adm:/sbin/nologin
          --      7-shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
          8-halt:x:7:0:halt:/sbin:/sbin/halt
          9-mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
          10:operator:x:11:0:operator:/root:/sbin/nologin
          11-games:x:12:100:games:/usr/games:/sbin/nologin
          12-ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
          13-nobody:x:99:99:Nobody:/:/sbin/nologin
    
      ? -e :實現多個選項間的邏輯or 關系
          [root@localhost ~]# grep -n  -e root -e bash /etc/passwd
          1:root:x:0:0:root:/root:/bin/bash
          10:operator:x:11:0:operator:/root:/sbin/nologin
          38:mageedu:x:1000:1000:mageedu:/home/mageedu:/bin/bash
          39:user1:x:1001:1001::/home/user1:/bin/bash
          40:tom:x:1002:1002::/home/tom:/bin/bash
          41:user2:x:1003:1003::/home/user2:/bin/bash
    
              
      ? -w :整行匹配整個單詞
          
          [root@localhost ~]# grep -w UUID /etc/fstab 
          UUID=f4406f6a-e495-45a0-a85e-3b059c0d3130 /                       xfs     defaults        0 0
          UUID=7c25120e-2371-413d-b584-fdd695b96702 /boot                   xfs     defaults        0 0
          UUID=19470291-724c-4f01-b6e1-7109ad22be1b /usr                    xfs     defaults        0 0
          UUID=c3460309-9e8c-4037-8684-4c6bdcabbacb swap                    swap    defaults        0 0
    
          
      ? -E :使用ERE

正則表達式

REGEXP :由一類特殊字符及文本字符所編寫的模式,其中有些字符(元字符)不表示字符字面意義,而表示控制或通配的功能
    程序支持:grep, vim, less,nginx等
分兩類:
    基本正則表達式:BRE
    擴展正則表達式:ERE    grep -E = egrep
正則表達式引擎:
    采用不同算法,檢查處理正則表達式的軟件模塊 PCRE(Perl Compatible Regular Expressions)
    元字符分類:字符匹配、匹配次數、位置錨定、分組

基本正則表達式元字符

  • 字符匹配:

    . :   匹配任意單個字符;
    [] :   匹配指定范圍內的任意單個字符
    [^]  :匹配指定范圍外的任意單個字符
    [:digit:] 、[:lower:] 、[:upper:] 、[:alpha:] 、[:alnum:] 、[:punct:] 、[:space:]
  • 匹配次數:

    用在要制定次數的字符后面,用于指定前面的字符要出現的次數    
    * :匹配前面的字符任意次,包括0次貪婪模式:盡可能長的匹配
        .* :任意長度的任意字符
        \? :匹配其前面的字符0 或1次
        \+ :匹配其前面的字符至少1次
        \{m\} :匹配前面的字符m次
        \{m,n\} :匹配前面的字符至少m 次,至多n次
        \{,n\} :匹配前面的字符至多n次 
        \{m,\} :匹配前面的字符至少m次
  • 位置錨定

^ :行首錨定,用于模式的最左側

        [root@localhost ~]# grep ^root /etc/passwd
        root:x:0:0:root:/root:/bin/bash
        rooter:x:1010:1010:rpc:/home/rooter:/bin/bash

    $ :行尾錨定,用于模式的最右側
        [root@localhost ~]# grep bash$ /etc/passwd
        root:x:0:0:root:/root:/bin/bash
        mageedu:x:1000:1000:mageedu:/home/mageedu:/bin/bash
        user1:x:1001:1001::/home/user1:/bin/bash
        tom:x:1002:1002::/home/tom:/bin/bash
        user2:x:1003:1003::/home/user2:/bin/bash

    ^PATTERN$:  用于模式匹配整行
        ^$:  空行
        ^[[:space:]]*$  :空白行
    \< 或 或 \b :詞首錨定,用于單詞模式的左側
    \> 或 或 \b :詞尾錨定;用于單詞模式的右側
    \<PATTERN\> :匹配整個單詞
  • 分組

    \(\) :將一個或多個字符捆綁在一起,當作一個整體進行處理,如:\(root\)\+
     分組括號中的模式匹配到的內容會被正則表達式引擎記錄于內部的變量中,這些變量的命名方式為: \1, \2, \3, ...
     \1:  從左側起,第一個左括號以及與之匹配右括號之間的模式所匹配到 的 字符
     實例: \(string1\+\(string2\)*\)
         \1: string1\+\(string2\)*
         \2: string2
         后向引用:引用前面的分組括號中的模式所匹配字符(而非模式本身)

egrep及擴展的正則表達式

egrep = grep -E
    egrep [OPTIONS] PATTERN [FILE...]
    擴展正則表達式的元字符:
    字符匹配:
        .  任意單個字符
        []  指定范圍的任意單個字符
        [^] 不在指定范圍內的任意單一字符
        
    次數匹配:        * :匹配前面字符任意次
        ?: 0 或1次
        + :1 次或多次
        {m} :匹配m次
        {m,n} :至少m ,至多n次
    
    位置錨定:
        ^ : 行首
        $ : 行尾
        \<, \b : 語首
        \>, \b : 語尾
    分組:
        ()
        后向引用:\1, \2, ...
    或者:
        a|b
        C|cat: C 或cat
        (C|c)at:Cat 或cat

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

(0)
dxkbokedxkboke
上一篇 2016-08-08
下一篇 2016-08-08

相關推薦

  • 第8天:腳本基礎,btrf,raid,

    http://note.youdao.com/yws/public/redirect/share?id=61b38adef24ced4dc646a5ef8e68df18&type=false

    Linux干貨 2016-08-16
  • 啟動和內核管理2

    五、自制linux系統     分區并創建文件系統         fdisk /dev/sdb         分兩個必要的分區   &n…

    Linux干貨 2016-09-18
  • Shell腳本編程3

    Shell腳本編程3  一、雜項知識整理 1、select與case select是個無限循環,因此要記住用break命令退出循環,或用exit命令終止腳本。也可以按ctrl+c退出循環,自動生成數字的選擇項,適合創建菜單。 select經常和case聯合使用。與for循環類似,可以省略in LIST,此時使用位置參量$1、$2等傳遞參數。 cas…

    Linux干貨 2016-08-19
  • Linux手動編譯源代碼文件

    一、簡介     Linux系統上程序包安裝的方式多種多樣,一般來說可通過rpm包安裝、yum在線安裝等方式實現。但有時候為了實現某種功能,而這種功能在通常情況下又不是太常用或是因為其他原因,不能通過yum下載安裝或在網上沒有找到rpm包,只是找到了程序安裝包的源代碼的話,這種情況下我們就可以對源代碼直接進行編譯安裝,以便解決這種臨時需求…

    Linux干貨 2015-09-26
  • N24_星空學習宣言

    學習是一種投資,既然投資了就不能虧損,努力學習,做一個學習界的巴菲特!

    Linux干貨 2016-10-24
  • 基礎命令

    1,對于Linux的實驗環境我們要用到虛擬機,往往每次上線都要登錄,這時候如果把用戶設為自動登錄就會方便許多,接下來就介紹下設置虛擬機的自動登錄 , /etc/gdm/custom.conf 這個就是設置自動登錄的一個配置文件,我們先來使用nano打開這個文件,如下 # GDM configuration storage 這是GDM的一個配置存儲 [daem…

    2017-11-19
欧美性久久久久