zoukankan      html  css  js  c++  java
  • Linux命令实践(三)

    1.file检查并显示文件类型(determine file type)

    一般用法就是file 后面接要查看的文件 可以一个或多个

    [root@test test]# ll
    total 140
    -rw-r--r-- 2 root root     18 Oct 17 16:05 ascii.txt
    lrwxrwxrwx 1 root root      9 Oct 17 16:06 ascii.txt.link -> ascii.txt
    -rw-r--r-- 2 root root     18 Oct 17 16:05 ascii_hardlink.txt
    -rwxr-xr-x 1 root root 123364 Oct 17 16:05 cp
    -rwxr-xr-x 1 root root   4534 Oct 17 16:04 sshd
    [root@test test]# file ascii_hardlink.txt 
    ascii_hardlink.txt: ASCII text               
    [root@test test]# file ascii.txt ascii.txt.link 
    ascii.txt:      ASCII text
    ascii.txt.link: symbolic link to `ascii.txt'
    [root@test test]# file ascii.txt ascii.txt.link ascii_hardlink.txt cp sshd 
    ascii.txt:          ASCII text
    ascii.txt.link:     symbolic link to `ascii.txt'
    ascii_hardlink.txt: ASCII text
    cp:                 ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.18, stripped
    sshd:               Bourne-Again shell script text executable
    

    -b:不显示文件名,只显示文件类型说明

    [root@test test]# ll
    total 140
    -rw-r--r-- 2 root root     18 Oct 17 16:05 ascii.txt
    lrwxrwxrwx 1 root root      9 Oct 17 16:06 ascii.txt.link -> ascii.txt
    -rw-r--r-- 2 root root     18 Oct 17 16:05 ascii_hardlink.txt
    -rwxr-xr-x 1 root root 123364 Oct 17 16:05 cp
    -rwxr-xr-x 1 root root   4534 Oct 17 16:04 sshd
    [root@test test]# file ascii.txt cp sshd
    ascii.txt: ASCII text
    cp:        ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.18, stripped
    sshd:      Bourne-Again shell script text executable
    [root@test test]# file -b ascii.txt cp sshd
    ASCII text
    ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.18, stripped
    Bourne-Again shell script text executable
    [root@test test]# 
    

    -L:显示链接文件所链接的文件的类型说明

    [root@test test]# ll
    total 140
    -rw-r--r-- 2 root root     18 Oct 17 16:05 ascii.txt
    lrwxrwxrwx 1 root root      9 Oct 17 16:06 ascii.txt.link -> ascii.txt
    -rw-r--r-- 2 root root     18 Oct 17 16:05 ascii_hardlink.txt
    -rwxr-xr-x 1 root root 123364 Oct 17 16:05 cp
    lrwxrwxrwx 1 root root      2 Oct 17 16:27 pc -> cp
    -rwxr-xr-x 1 root root   4534 Oct 17 16:04 sshd
    [root@test test]# file -L pc
    pc: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.18, stripped
    [root@test test]# file -L ascii.txt.link 
    ascii.txt.link: ASCII text
    

    2.cat 连接并显示文件内容

    语法:cat [OPTION]... [FILE]...

    通常不接任何选项就是把指定的文件 的内容倾倒到在标准输出

    [root@test test]# cat /etc/issue.net 
    CentOS release 6.5 (Final)
    Kernel 
     on an m
    

    -n:显示行号

    [root@test test]# cat -n /etc/issue.net 
         1	CentOS release 6.5 (Final)
         2	Kernel 
     on an m
    

    -E:显示行结束符$

    [root@test test]# cat -E /etc/issue.net 
    CentOS release 6.5 (Final)$
    Kernel 
     on an m$
    

    -A:显示所有字符

    [root@test test]# cat -E test.txt 
    $aaaaaaaaaaaaaaaaaaaa
    $
    $ggggggggggggggggg
    $
    $
    $
    $
    $
    $dddddddddddddddda
    $
    $sssssssssssss
    $
    [root@test test]# cat -A test.txt 
    aaaaaaaaaaaaaaaaaaaaa^M$
    ^M$
    gggggggggggggggggg^M$
    ^M$
    ^M$
    ^M$
    ^M$
    ^M$
    ddddddddddddddddda^M$
    ^M$
    ssssssssssssss^M$
    ^M$
    

    3.tac连接并显示文件内容(倒序显示)

    [root@test test]# cat test.txt 
    1
    2
    3
    4
    5
    [root@test test]# tac test.txt 
    5
    4
    3
    2
    1
    

    4.head:显示文件前n行,默认前10行

    语法:head (选项) (参数)

    -n:指定显示多少行

    [root@test test]# head /etc/init.d/sshd 
    #!/bin/bash
    #
    # sshd		Start up the OpenSSH server daemon
    #
    # chkconfig: 2345 55 25
    # description: SSH is a protocol for secure remote shell access. 
    #              This service starts up the OpenSSH server daemon.
    #
    # processname: sshd
    # config: /etc/ssh/ssh_host_key
    [root@test test]# head -n 3 /etc/init.d/sshd 
    #!/bin/bash
    #
    # sshd		Start up the OpenSSH server daemon
    

      提示:在Linux里head可以直接要显示的行数目,比如我要看/etc/init.d/sshd 这个文件的前3行  可以写成head -3 /etc/init.d/sshd

    head -n -数字:显示除开指定倒数几行以外的其他所有行的内容(不显示倒数几行的内容)

    [qiuhom@test ~]$ cat mycron 
    # this is test work delete *.log file in work dir 
     
    
    
    #*/10 * * * * /bin/bash /work/test/script.sh >/dev/null 2>&1
    #*/05 * * * * /bin/bash /work/test/createfile.sh >/dev/null 2>&1
    [qiuhom@test ~]$ head -n -1 mycron 
    # this is test work delete *.log file in work dir 
     
    
    
    #*/10 * * * * /bin/bash /work/test/script.sh >/dev/null 2>&1
    [qiuhom@test ~]$ head -n -2 mycron 
    # this is test work delete *.log file in work dir 
     
    
    
    [qiuhom@test ~]$ head -n -5 mycron 
    # this is test work delete *.log file in work dir
    

      提示:当然这里的-n就不能省略。

    5.tail:显示文件后n行,默认显示10行

    语法:tail (选项) (参数)

    -n:指定要显示多少行

    [root@test test]# tail /etc/init.d/sshd     
    		RETVAL=$?
    		if [ $RETVAL -eq 3 -a -f $lockfile ] ; then
    			RETVAL=2
    		fi
    		;;
    	*)
    		echo $"Usage: $0 {start|stop|restart|reload|force-reload|condrestart|try-restart|status}"
    		RETVAL=2
    esac
    exit $RETVAL
    [root@test test]# tail -n 5  /etc/init.d/sshd 
    	*)
    		echo $"Usage: $0 {start|stop|restart|reload|force-reload|condrestart|try-restart|status}"
    		RETVAL=2
    esac
    exit $RETVAL
    

    -f:显示文件尾部,不退出,等待显示新追加到文件里的内容

    提示:这个选项很重要,我们常用在监控某些日志文件。这个选项可以很清除的看到一个文件里增量的数据。和tailf命令一样的作用。

    6.cut:以某种方式从文本中提取一段文字并输出

    语法:cut (选项) (参数)

    -d:指定分割符

    -f:指定切割后要显示的字段

    -f1:显示第一个字段

    -f1,3:显示第一个字段和第三个字段

    -f1-3:显示第一个字段到第三个字段

    [root@test test]# cat ascii.txt
    this is test file
    test tail -f command
    [root@test test]# cut -d' ' -f2 ascii.txt
    is
    tail
    [root@test test]# cut -d' ' -f1 ascii.txt 
    this
    test
    [root@test test]# cut -d' ' -f1,4 ascii.txt 
    this file
    test command
    [root@test test]# cut -d' ' -f1-3 ascii.txt  
    this is test
    test tail -f
    

      提示:通常-d 和 -f 都是一起使用。

    -b:按照字节来切割

    [root@test test]# cat ascii.txt
    this is test file
    test tail -f command
    提示你好 n
    [root@test test]# cut -b5 ascii.txt
     
     
    
    [root@test test]# cut -b7 ascii.txt 
    s
    a
    
    [root@test test]# cut -b1,7,9 ascii.txt
    tst
    tal
    
    
    [root@test test]# cut -b1-7,9 ascii.txt 
    this ist
    test tal
    提示
    

    -c:按照字符来切割

    [root@test test]# cat ascii.txt
    this is test file
    test tail -f command
    提示你好 n
    [root@test test]# cut -c3 ascii.txt
    i
    s
    
    [root@test test]# cut -c3,9 ascii.txt
    it
    sl
    
    
     [root@test test]# cut -c3-9 ascii.txt 
    is is t
    st tail
    你
    
    [root@test test]# cut -c4-9 ascii.txt 
    s is t
    t tail
    示你
    

    7.join:按两个文件的相同字段合并

    语法:join (选项) (文件1) (文件2)

    join命令针对每一对具有相同内容的输入行,整合为一行输出到标准输出,默认情况下是把输入的第一个字段当作连接字段,字段之间用空格隔开。

    [root@test test]# cat file1
    a1 b1 c1
    12 13 14
    a b c
    [root@test test]# cat file2
    a1 b1 c2
    aa bb cc
    11 22 33
    [root@test test]# sort file1>file3
    [root@test test]# sort file2>file4
    [root@test test]# cat file3
    12 13 14
    a b c
    a1 b1 c1
    [root@test test]# cat file4
    11 22 33
    a1 b1 c2
    aa bb cc
    [root@test test]# join file3 file4
    a1 b1 c1 b1 c2
    

      提示:使用join 合并文件的要求是2个文件必须是用sort排序后的,否则会提示我们not in sorted order 的字样

    8.sort对文件内容按指定的规则排序,然后将排序后的结果输出

    语法:sort (选项) (文件)

    默认比较的原则上从首字符向后,依次按ASCII码值进行比较,输出默认按照升序进行排序。

    [root@test test]# cat file1
    a1 b1 c1
    12 13 14
    a b c
    [root@test test]# sort file1
    12 13 14
    a b c
    a1 b1 c1
    

    -n:按照数字大小进行排序(从小到大的顺序排序)

    [root@test test]# cat xxx 
    1
    5
    3
    2
    4
    7
    8
    6
    9
    4aa
    3dd
    6cc
    8bb
    0hh
    7ee
    1ff
    2gg
    [root@test test]# sort -n xxx
    0hh
    1
    1ff
    2
    2gg
    3
    3dd
    4
    4aa
    5
    6
    6cc
    7
    7ee
    8
    8bb
    9
    

    -r:倒序输出排序结果

    [root@test test]# sort -nr xxx
    9
    8bb
    8
    7ee
    7
    6cc
    6
    5
    4aa
    4
    3dd
    3
    2gg
    2
    1ff
    1
    0hh
    

    -u:去除重复行

    [root@test test]# cat xxx 
    abc
    123
    456
    789
    123
    456
    abcdef
    ab
    abc
    
    [root@test test]# sort -u xxx
    
    123
    456
    789
    ab
    abc
    abcdef
    

    -t -k:指定列进行排序

    默认按照第一列排序

    [root@test test]# cat xxx 
    abc--5
    123--8
    456--0
    789--1
    123--3
    456--4
    abcdef--7
    ab--6
    abc--2
    
    [root@test test]# sort xxx 
    
    123--3
    123--8
    456--0
    456--4
    789--1
    ab--6
    abc--2
    abc--5
    abcdef--7
    

    -t指定分割符-k选项指定分割后按第几列进行排序

    [root@test test]# cat xxx 
    abc--5
    123--8
    456--0
    789--1
    123--3
    456--4
    abcdef--7
    ab--6
    abc--2
    
    [root@test test]# sort -t "-" -k2 xxx  
    
    456--0
    789--1
    abc--2
    123--3
    456--4
    abc--5
    ab--6
    abcdef--7
    123--8
    

      提示:-t后面跟的分割符只能是单个字符的分割符 不能给两个和两个以上的,否则会报错sort: multi-character tab 

    分组排序案例:以aa-cd-dd-xx中的最后一列xx进行分组,在对每组中的“ip2.2.3.xxx”的最后一列进行排序

    [root@test test]# cat ip
    ab-cd-ef-gh 12.0.10.2
    bc-de-fg-jk 21.11.33.155
    aa-cc-dd-ef 192.168.11.5
    cc-ee-ac-ad 110.121.234.65
    cc-ee-ac-ad 110.121.234.165
    0f-8e-jj-t2 10.0.0.11
    22-5h-9k-8e 172.16.1.25
    0f-8e-jj-t2 10.0.0.11
    uu-cc-uc-df 127.0.0.11
    ab-cd-ef-gh 12.0.10.2
    bc-de-fg-jk 21.11.33.55
    aa-cc-dd-ef 192.168.11.25
    aa-cc-dd-ef 192.168.11.45
    0f-8e-jj-t2 10.0.0.11
    uu-cc-uc-df 127.0.0.122
    ab-cd-ef-gh 12.0.10.2
    bc-de-fg-jk 21.11.33.55
    aa-cc-dd-ef 192.168.11.5
    aa-cc-dd-ef 192.168.11.15
    22-5h-9k-8e 172.16.1.65
    0f-8e-jj-t2 10.0.0.11
    uu-cc-uc-df 127.0.0.111
    ab-cd-ef-gh 12.0.10.2
    bc-de-fg-jk 21.11.33.55
    [root@test test]# sort -t "." -k1.10,1.11 -k4 ip
    22-5h-9k-8e 172.16.1.25
    22-5h-9k-8e 172.16.1.65
    cc-ee-ac-ad 110.121.234.165
    cc-ee-ac-ad 110.121.234.65
    uu-cc-uc-df 127.0.0.11
    uu-cc-uc-df 127.0.0.111
    uu-cc-uc-df 127.0.0.122
    aa-cc-dd-ef 192.168.11.15
    aa-cc-dd-ef 192.168.11.25
    aa-cc-dd-ef 192.168.11.45
    aa-cc-dd-ef 192.168.11.5
    aa-cc-dd-ef 192.168.11.5
    ab-cd-ef-gh 12.0.10.2
    ab-cd-ef-gh 12.0.10.2
    ab-cd-ef-gh 12.0.10.2
    ab-cd-ef-gh 12.0.10.2
    bc-de-fg-jk 21.11.33.155
    bc-de-fg-jk 21.11.33.55
    bc-de-fg-jk 21.11.33.55
    bc-de-fg-jk 21.11.33.55
    0f-8e-jj-t2 10.0.0.11
    0f-8e-jj-t2 10.0.0.11
    0f-8e-jj-t2 10.0.0.11
    0f-8e-jj-t2 10.0.0.11

      提示:sort排序是按照指定列的第一个数字来排序的,不会按照数字大小排序。所有我们可以看到我们ip最后一位都按照的第一个数字排序的。

    9.uniq:去除文件内容中的重复内容行

    语法:uniq (选项) (文件或标准输入)

    -c:去除虫重复行,并统计重复行出现的次数

    [root@test test]# cat ip
    ab-cd-ef-gh 12.0.10.2
    ab-cd-ef-gh 12.0.10.2
    ab-cd-ef-gh 12.0.10.2
    bc-de-fg-jk 21.11.33.155
    aa-cc-dd-ef 192.168.11.5
    cc-ee-ac-ad 110.121.234.65
    cc-ee-ac-ad 110.121.234.165
    0f-8e-jj-t2 10.0.0.11
    ab-cd-ef-gh 12.0.10.2
    ab-cd-ef-gh 12.0.10.2
    22-5h-9k-8e 172.16.1.25
    0f-8e-jj-t2 10.0.0.11
    uu-cc-uc-df 127.0.0.11
    uu-cc-uc-df 127.0.0.11
    uu-cc-uc-df 127.0.0.11
    ab-cd-ef-gh 12.0.10.2
    bc-de-fg-jk 21.11.33.55
    aa-cc-dd-ef 192.168.11.25
    aa-cc-dd-ef 192.168.11.45
    aa-cc-dd-ef 192.168.11.45
    [root@test test]# uniq -c ip
          3 ab-cd-ef-gh 12.0.10.2
          1 bc-de-fg-jk 21.11.33.155
          1 aa-cc-dd-ef 192.168.11.5
          1 cc-ee-ac-ad 110.121.234.65
          1 cc-ee-ac-ad 110.121.234.165
          1 0f-8e-jj-t2 10.0.0.11
          2 ab-cd-ef-gh 12.0.10.2
          1 22-5h-9k-8e 172.16.1.25
          1 0f-8e-jj-t2 10.0.0.11
          3 uu-cc-uc-df 127.0.0.11
          1 ab-cd-ef-gh 12.0.10.2
          1 bc-de-fg-jk 21.11.33.55
          1 aa-cc-dd-ef 192.168.11.25
          2 aa-cc-dd-ef 192.168.11.45
    [root@test test]# 
    

    -d:只显示重复的行

    [root@test test]# uniq -c ip
          3 ab-cd-ef-gh 12.0.10.2
          1 bc-de-fg-jk 21.11.33.155
          1 aa-cc-dd-ef 192.168.11.5
          1 cc-ee-ac-ad 110.121.234.65
          1 cc-ee-ac-ad 110.121.234.165
          1 0f-8e-jj-t2 10.0.0.11
          2 ab-cd-ef-gh 12.0.10.2
          1 22-5h-9k-8e 172.16.1.25
          1 0f-8e-jj-t2 10.0.0.11
          3 uu-cc-uc-df 127.0.0.11
          1 ab-cd-ef-gh 12.0.10.2
          1 bc-de-fg-jk 21.11.33.55
          1 aa-cc-dd-ef 192.168.11.25
          2 aa-cc-dd-ef 192.168.11.45
    [root@test test]# uniq -d ip
    ab-cd-ef-gh 12.0.10.2
    ab-cd-ef-gh 12.0.10.2
    uu-cc-uc-df 127.0.0.11
    aa-cc-dd-ef 192.168.11.45
    [root@test test]# 
    

    -u:只显示唯一的行

    [root@test test]# uniq -c ip
          3 ab-cd-ef-gh 12.0.10.2
          1 bc-de-fg-jk 21.11.33.155
          1 aa-cc-dd-ef 192.168.11.5
          1 cc-ee-ac-ad 110.121.234.65
          1 cc-ee-ac-ad 110.121.234.165
          1 0f-8e-jj-t2 10.0.0.11
          2 ab-cd-ef-gh 12.0.10.2
          1 22-5h-9k-8e 172.16.1.25
          1 0f-8e-jj-t2 10.0.0.11
          3 uu-cc-uc-df 127.0.0.11
          1 ab-cd-ef-gh 12.0.10.2
          1 bc-de-fg-jk 21.11.33.55
          1 aa-cc-dd-ef 192.168.11.25
          2 aa-cc-dd-ef 192.168.11.45
    [root@test test]# uniq -u ip
    bc-de-fg-jk 21.11.33.155
    aa-cc-dd-ef 192.168.11.5
    cc-ee-ac-ad 110.121.234.65
    cc-ee-ac-ad 110.121.234.165
    0f-8e-jj-t2 10.0.0.11
    22-5h-9k-8e 172.16.1.25
    0f-8e-jj-t2 10.0.0.11
    ab-cd-ef-gh 12.0.10.2
    bc-de-fg-jk 21.11.33.55
    aa-cc-dd-ef 192.168.11.25
    [root@test test]# 

    和sort结合使用

    [root@test test]# cat ip
    ab-cd-ef-gh 12.0.10.2
    bc-de-fg-jk 21.11.33.155
    ab-cd-ef-gh 12.0.10.2
    bc-de-fg-jk 21.11.33.155
    ab-cd-ef-gh 12.0.10.2
    bc-de-fg-jk 21.11.33.155
    aa-cc-dd-ef 192.168.11.5
    [root@test test]# uniq -c ip
          1 ab-cd-ef-gh 12.0.10.2
          1 bc-de-fg-jk 21.11.33.155
          1 ab-cd-ef-gh 12.0.10.2
          1 bc-de-fg-jk 21.11.33.155
          1 ab-cd-ef-gh 12.0.10.2
          1 bc-de-fg-jk 21.11.33.155
          1 aa-cc-dd-ef 192.168.11.5
    [root@test test]# sort ip |uniq -c
          1 aa-cc-dd-ef 192.168.11.5
          3 ab-cd-ef-gh 12.0.10.2
          3 bc-de-fg-jk 21.11.33.155
    [root@test test]# 
    

      提示:因为uniq只能对相邻的重复行进行去重,所以先sort排序,后去重,这样比较准确。

    10.wc统计文件的行数、单词数量或字节数量

     语法:wc (选项) (文件)

    -c:统计字节数

    [root@test test]# cat ip
    ab-cd-ef-gh 12.0.10.2
    bc-de-fg-jk 21.11.33.155
    ab-cd-ef-gh 12.0.10.2
    bc-de-fg-jk 21.11.33.155
    ab-cd-ef-gh 12.0.10.2
    bc-de-fg-jk 21.11.33.155
    aa-cc-dd-ef 192.168.11.5
    [root@test test]# wc -c ip
    166 ip
    

    -l:统计文件的行数

    [root@test test]# wc -l ip
    7 ip
    

    -m:统计字符数

    [root@test test]# wc -m ip
    166 ip
    

    -w:统计单词数

    [root@test test]# wc -w ip
    14 ip
    

    -L:统计最长行的字符长度

    [root@test test]# wc -L ip
    24 ip

     不加任何选项查看文件

    [root@test test]# wc ip
      7  14 166 ip
    

      提示:不加选项默认分别统计显示文件的行数、单词数、字符数

    11.tr替换或删除字符

    语法:tr (选项) (字符1) (字符2)

    -d:删除指定字符

    [root@test test]# cat ip
    ab-cd-ef-gh 12.0.10.2
    bc-de-fg-jk 21.11.33.155
    ab-cd-ef-gh 12.0.10.2
    bc-de-fg-jk 21.11.33.155
    ab-cd-ef-gh 12.0.10.2
    bc-de-fg-jk 21.11.33.155
    aa-cc-dd-ef 192.168.11.5
    [root@test test]# tr -d "ab" <ip
    -cd-ef-gh 12.0.10.2
    c-de-fg-jk 21.11.33.155
    -cd-ef-gh 12.0.10.2
    c-de-fg-jk 21.11.33.155
    -cd-ef-gh 12.0.10.2
    c-de-fg-jk 21.11.33.155
    -cc-dd-ef 192.168.11.5
    

    -s:删除重复的其他字符,保留指定连续字符的第一个字符。

    [root@test test]# echo "aaaabbbbbccccccddddeeefffggg" |tr -s abcdefg
    abcdefg
    [root@test test]# echo "aaaabbbbbccccccddddeeefffggg" |tr -s abcd   
    abcdeeefffggg
    

    -c:处理除开指定字符以外的字符(对指定的字符取反操作)

    [root@test test]# cat ip 
    ab-cd-ef-gh 12.0.10.2
    bc-de-fg-jk 21.11.33.155
    ab-cd-ef-gh 12.0.10.2
    bc-de-fg-jk 21.11.33.155
    ab-cd-ef-gh 12.0.10.2
    bc-de-fg-jk 21.11.33.155
    aa-cc-dd-ef 192.168.11.5
    [root@test test]# tr -c "ab
    " "**" <ip
    ab*******************
    b***********************
    ab*******************
    b***********************
    ab*******************
    b***********************
    aa**********************
    [root@test test]# 

    不加选项把字符1替换成字符2

    [root@test test]# cat ip
    ab-cd-ef-gh 12.0.10.2
    bc-de-fg-jk 21.11.33.155
    ab-cd-ef-gh 12.0.10.2
    bc-de-fg-jk 21.11.33.155
    ab-cd-ef-gh 12.0.10.2
    bc-de-fg-jk 21.11.33.155
    aa-cc-dd-ef 192.168.11.5
    [root@test test]# tr "ab" "AB" <ip
    AB-cd-ef-gh 12.0.10.2
    Bc-de-fg-jk 21.11.33.155
    AB-cd-ef-gh 12.0.10.2
    Bc-de-fg-jk 21.11.33.155
    AB-cd-ef-gh 12.0.10.2
    Bc-de-fg-jk 21.11.33.155
    AA-cc-dd-ef 192.168.11.5
    [root@test test]# tr '[a-z]' '[A-Z]' < ip
    AB-CD-EF-GH 12.0.10.2
    BC-DE-FG-JK 21.11.33.155
    AB-CD-EF-GH 12.0.10.2
    BC-DE-FG-JK 21.11.33.155
    AB-CD-EF-GH 12.0.10.2
    BC-DE-FG-JK 21.11.33.155
    AA-CC-DD-EF 192.168.11.5
    

    12.tee多重定向,将数据重定向到文件的同时提供一份数据副本作为后续命令的标准输入。简单说就是把数据从定向到文件和屏幕上。

    语法:tee (选项) (文件)

    -a:向文件中追加内容,不覆盖。

    [root@test test]# ll
    total 136
    -rwxr-xr-x 1 root root 123364 Oct 17 16:05 cp
    -rw-r--r-- 1 root root    166 Oct 17 21:28 ip
    lrwxrwxrwx 1 root root      2 Oct 17 16:27 pc -> cp
    -rwxr-xr-x 1 root root   1565 Oct 17 16:04 sshd.gz
    -rw-r--r-- 1 root root     66 Oct 17 20:16 xxx
    [root@test test]# cat ip|tee cat_ip.txt
    ab-cd-ef-gh 12.0.10.2
    bc-de-fg-jk 21.11.33.155
    ab-cd-ef-gh 12.0.10.2
    bc-de-fg-jk 21.11.33.155
    ab-cd-ef-gh 12.0.10.2
    bc-de-fg-jk 21.11.33.155
    aa-cc-dd-ef 192.168.11.5
    [root@test test]# ll
    total 140
    -rw-r--r-- 1 root root    166 Oct 17 22:09 cat_ip.txt
    -rwxr-xr-x 1 root root 123364 Oct 17 16:05 cp
    -rw-r--r-- 1 root root    166 Oct 17 21:28 ip
    lrwxrwxrwx 1 root root      2 Oct 17 16:27 pc -> cp
    -rwxr-xr-x 1 root root   1565 Oct 17 16:04 sshd.gz
    -rw-r--r-- 1 root root     66 Oct 17 20:16 xxx
    [root@test test]# cat cat_ip.txt 
    ab-cd-ef-gh 12.0.10.2
    bc-de-fg-jk 21.11.33.155
    ab-cd-ef-gh 12.0.10.2
    bc-de-fg-jk 21.11.33.155
    ab-cd-ef-gh 12.0.10.2
    bc-de-fg-jk 21.11.33.155
    aa-cc-dd-ef 192.168.11.5
    [root@test test]# ls |tee -a cat_ip.txt 
    cat_ip.txt
    cp
    ip
    pc
    sshd.gz
    xxx
    [root@test test]# cat cat_ip.txt 
    ab-cd-ef-gh 12.0.10.2
    bc-de-fg-jk 21.11.33.155
    ab-cd-ef-gh 12.0.10.2
    bc-de-fg-jk 21.11.33.155
    ab-cd-ef-gh 12.0.10.2
    bc-de-fg-jk 21.11.33.155
    aa-cc-dd-ef 192.168.11.5
    cat_ip.txt
    cp
    ip
    pc
    sshd.gz
    xxx
    [root@test test]# 

    默认不加选项会覆盖文件里的内容

    [root@test test]# cat cat_ip.txt 
    ab-cd-ef-gh 12.0.10.2
    bc-de-fg-jk 21.11.33.155
    ab-cd-ef-gh 12.0.10.2
    bc-de-fg-jk 21.11.33.155
    ab-cd-ef-gh 12.0.10.2
    bc-de-fg-jk 21.11.33.155
    aa-cc-dd-ef 192.168.11.5
    cat_ip.txt
    cp
    ip
    pc
    sshd.gz
    xxx
    [root@test test]# 
    [root@test test]# ls |tee cat_ip.txt 
    cat_ip.txt
    cp
    ip
    pc
    sshd.gz
    xxx
    [root@test test]# cat cat_ip.txt 
    cat_ip.txt
    cp
    ip
    pc
    sshd.gz
    xxx
    [root@test test]# 
  • 相关阅读:
    最大子串和
    [USACO1.5]数字金字塔 Number Triangles
    数字金字塔
    台阶问题
    取余运算
    数列分段pascal程序
    Java 集合-Collection接口和迭代器的实现
    Java 集合-集合介绍
    Java IO流-File类
    Git学习记录
  • 原文地址:https://www.cnblogs.com/qiuhom-1874/p/9805888.html
Copyright © 2011-2022 走看看