3.2Linux文本處理工具及bash重定向

mv命令:
NAME
       mv - move files  移動文件php

SYNOPSIS
       mv [-fi] source_file target_filelinux

       mv [-fi] source_file... target_file   源文件有多個,則目標必須存在且爲目錄
        mv SRC... DESTshell

        -i: interactive
OPTIONS
       The mv utility shall conform to the Base Definitions volume of IEEE Std 1003.1-2001, Section 12.2, Utility Syntax Guidelines.bash

       The following options shall be supported:
       目標存在是不提示的直接覆蓋
       -f     Do not prompt for confirmation if the destination path exists. Any previous occurrence of the -i option is ignored.
[root@linux_basic tmp]# mv test.txt free/
mv: overwrite `free/test.txt'? n
[root@linux_basic tmp]# mv -f test.txt free/
       -i     Prompt for confirmation if the destination path exists. Any previous occurrence of the -f option is ignored.
       目標存在是提示是否覆蓋
       Specifying more than one of the -f or -i options shall not be considered an error. The last option specified shall  determine
       the behavior of mv.
Linux下並不之後綴名來區分文件的類型。
移動文件、目錄、重命名、同名文件來操做,查看別名、查看mv屬於那些章節、怎麼來查看手冊、是內置仍是外部命令 type
[root@linux_basic ~]# alias
alias cp='cp -i'
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
alias mv='mv -i'
[root@linux_basic tmp]# cp test.txt free/
[root@linux_basic tmp]# mv test.txt free/
mv: overwrite `free/test.txt'? n   由於cp別名是'mv -i'
新建一個目錄
[root@linux_basic tmp]# mkdir test.txt
[root@linux_basic tmp]# mv test.txt free/
mv: overwrite `free/test.txt'? n
[root@linux_basic tmp]# ls free/test.txt -l
-rw-r--r--. 1 root root 0 Dec 21 14:45 free/test.txt
[root@linux_basic tmp]# mv test.txt free/test.txt   把目錄移動到同名文件處
mv: overwrite `free/test.txt'? y
mv: cannot overwrite non-directory `free/test.txt' with directory `test.txt'
[root@linux_basic tmp]# ls -l free/test.txt
-rw-r--r--. 1 root root 0 Dec 21 14:45 free/test.txt
[root@linux_basic tmp]# ls -ld test.txt/
drwxr-xr-x. 2 root root 4096 Dec 21 14:46 test.txt/
[root@linux_basic tmp]# cd free/
[root@linux_basic free]# ls
link  links  ok  other  othero  other_to  other_too  test.txt  too
[root@linux_basic free]# ls test.txt -l
-rw-r--r--. 1 root root 0 Dec 21 14:45 test.txt
[root@linux_basic free]# mv test.txt ../test.txt   把文件移動到同名目錄處
[root@linux_basic free]# ls ../test.txt/
test.txtless


    rm命令:
NAME
       rm - remove files or directories編輯器

SYNOPSIS
       rm [OPTION]... FILE...
OPTIONS
       The rm utility shall conform to the Base Definitions volume of IEEE Std 1003.1-2001, Section 12.2, Utility Syntax Guidelines.ide

       The following options shall be supported:工具

       -f     Do  not prompt for confirmation. Do not write diagnostic messages or modify the exit status in the case of nonexistent
              operands.  Any previous occurrences of the -i option shall be ignored.post

       -i     Prompt for confirmation as described previously. Any previous occurrences of the -f option shall be ignored.測試

       -R     Remove file hierarchies. See the DESCRIPTION.

       -r     Equivalent to -R.
           --no-preserve-root
           --no-preserve-root  指定此項是不保護根,刪除後所有文件都沒了
              do not treat ‘/’ specially
[root@linux_basic ~]# rm -r /
rm: it is dangerous to operate recursively on `/'
rm: use --no-preserve-root to override this failsafe             
       --preserve-root
              do not remove ‘/’ (default)   默認是保護根的     
特別注意此條命令要謹慎使用,在生產環境中是通常用普通用戶來操做的,否則刪除了就一去不復返了
[root@linux_basic tmp]# ls
A  free  hello  mylinux  mylog  system-release  test  test.txt  yoA  yoH  you  yoU  you.txt
[root@linux_basic tmp]# rm yoA
rm: remove regular empty file `yoA'? y
[root@linux_basic tmp]# ls
A  free  hello  mylinux  mylog  system-release  test  test.txt  yoH  you  yoU  you.txt
[root@linux_basic tmp]# rm free/
link       links      ok         other      othero     other_to   other_too  too/      
[root@linux_basic tmp]# rm free/too
rm: cannot remove `free/too': Is a directory
[root@linux_basic tmp]# rm free/too -f
rm: cannot remove `free/too': Is a directory
[root@linux_basic tmp]# rm free/too -r
rm: descend into directory `free/too'? n
[root@linux_basic tmp]# rm free/too -rf


文本編輯命令:nano
    全屏編輯器:
NAME
       nano - Nano’s ANOther editor, an enhanced free Pico clone

SYNOPSIS
       nano [OPTIONS] [[+LINE,COLUMN] FILE]...   
nano不跟文件,則是直接打開編輯器的
使用方法能夠根據下部提示去操做,  ^是指 Ctrl
                                                                  [ Read 1 line ]
^G Get Help             ^O WriteOut             ^R Read File            ^Y Prev Page            ^K Cut Text             ^C Cur Pos
^X Exit                 ^J Justify              ^W Where Is             ^V Next Page            ^U UnCut Text           ^T To Spell


文本處理類命令:  文本統計工具
    wc: Word Count
NAME
       wc - print newline, word, and byte counts for each file

SYNOPSIS
       wc [OPTION]... [FILE]...
       wc [OPTION]... --files0-from=F

DESCRIPTION
       Print  newline,  word,  and byte counts for each FILE, and a total line if more than one FILE is specified.  With no FILE, or
       when FILE is -, read standard input.
   
        -l: 僅顯示行數
        -l, --lines
              print the newline counts  打印行數
        -w:
        -w, --words
              print the word counts   打印單詞數
        -c:
        -c, --bytes
              print the byte counts  打印字節數
[root@linux_basic tmp]# cat you.txt
hello,how are you.
I love you.
[root@linux_basic tmp]# wc -l you.txt    '-l'選項是比較經常使用的
2 you.txt
[root@linux_basic tmp]# wc you.txt
2  6 31 you.txt
 

    cut:
NAME
       cut - remove sections from each line of files

SYNOPSIS
       cut OPTION... [FILE]...

DESCRIPTION
       Print selected parts of lines from each FILE to standard output.
        -d: 指定分隔符
        -d, --delimiter=DELIM
              use DELIM instead of TAB for field delimiter
        -f: 指定要顯示的字段
        -f, --fields=LIST
              select only these fields;  also print any line that contains no delimiter character, unless the -s option is specified
       字段格式的寫法:
             m: 第m列
             m,n: 第m和n列
             m-n: 第m到第n列
[root@linux_basic you]# cut -d: -f1 passwd
[root@linux_basic you]# cut -d: -f1,5 passwd
[root@linux_basic you]# cut -d: -f1-5 passwd
           

對文本的內容進行排序
    sort:
NAME
       sort - sort lines of text files

SYNOPSIS
       sort [OPTION]... [FILE]...
       sort [OPTION]... --files0-from=F
DESCRIPTION
       Write sorted concatenation of all FILE(s) to standard output.
            -f: 忽略字符大小寫           
      -f, --ignore-case
              fold lower case to upper case characters
            -t: 指定分隔符
            -t, --field-separator=SEP
              use SEP instead of non-blank to blank transition
            -k: 指定分隔以後要進行排序比較的字段
            -k, --key=POS1[,POS2]
              start a key at POS1 (origin 1), end it at POS2 (default end of line)
            -n: 以數值大小進行排序
            -n, --numeric-sort
              compare according to string numerical value
      排序後能夠有重複的行       
            -u: 排序後去重,去除重複的行
            -u, --unique
              with -c, check for strict ordering; without -c, output only the first of an equal run
對顯示的以':'分隔符的第二列進行排序
[root@linux_basic you]# cut -d: -f3,4,7 passwd | sort -t: -k2 -n
[root@linux_basic you]# cut -d: -f7 passwd | sort -u

 

    uniq:  在不連續相同內容的時候是不去重的,測試一下
NAME
       uniq - report or omit repeated lines

SYNOPSIS
       uniq [OPTION]... [INPUT [OUTPUT]]

DESCRIPTION
       Filter adjacent matching lines from INPUT (or standard input), writing to OUTPUT (or standard output).
    
        -d  僅顯示重複的行
        -d, --repeated
              only print duplicate lines
        -u  僅顯示不重複的行
        -u, --unique
              only print unique lines
        -c: 統計行出現的次數
        -c, --count
              prefix lines by the number of occurrences
[root@linux_basic you]# cut -d: -f7 passwd | uniq  -c
      1 /bin/bash
      4 /sbin/nologin
      1 /bin/sync
      1 /sbin/shutdown
      1 /sbin/halt
     17 /sbin/nologin
      2 /bin/bash
[root@linux_basic you]# cut -d: -f7 passwd
/bin/bash
/sbin/nologin
/sbin/nologin
/sbin/nologin
/sbin/nologin
/bin/sync
/sbin/shutdown
/sbin/halt
/sbin/nologin
/sbin/nologin
/sbin/nologin
/sbin/nologin
/sbin/nologin
/sbin/nologin
/sbin/nologin
/sbin/nologin
/sbin/nologin
/sbin/nologin
/sbin/nologin
/sbin/nologin
/sbin/nologin
/sbin/nologin
/sbin/nologin
/sbin/nologin
/sbin/nologin
/bin/bash
/bin/bash
[root@linux_basic you]# cut -d: -f7 passwd | uniq
/bin/bash
/sbin/nologin
/bin/sync
/sbin/shutdown
/sbin/halt
/sbin/nologin
/bin/bash
[root@linux_basic you]# cut -d: -f7 passwd | uniq -d
/sbin/nologin
/sbin/nologin
/bin/bash
[root@linux_basic you]# cut -d: -f7 passwd | uniq -u
/bin/bash
/bin/sync
/sbin/shutdown
/sbin/halt
[root@linux_basic you]# cut -d: -f7 passwd | uniq -c
      1 /bin/bash
      4 /sbin/nologin
      1 /bin/sync
      1 /sbin/shutdown
      1 /sbin/halt
     17 /sbin/nologin
      2 /bin/bash


    練習:  只用sort 和 uniq試一下
        一、顯示當前系統上每一個用戶的shell;
            # cut -d: -f1,7 /etc/passwd

        二、顯示當前系統上全部用戶使用的各類shell;
            # cut -d: -f7 /etc/passwd | sort | uniq

        三、取出/etc/inittab文件的第7行;
            # head -n 7 /etc/inittab | tail -n 1

        四、取出/etc/passwd文件中第7個用戶的用戶名;
            # head -n 7 /etc/passwd | tail -n 1 | cut -d: -f1

        五、統計/etc目錄下以大小寫p開頭的文件的個數;
            # ls -d /etc/[pP]* | wc -l

bash特性之輸入、輸出重定向和管道

    程序:
        數據來源:輸入流
        數據目標:輸出流
程序輸入輸出有三種
        標準輸入
        標準輸出
        標準錯誤輸出

        一切皆文件

        文件:文件系統(內核)   文件描述符 0 1 2
            標準輸入:0 
                默認是鍵盤
            標準輸出:1
                默認是監視器
            錯誤輸出:2
                默認是監視器

        重定向意味着:
            改變其標準位置

        輸出重定向:
            COMMAND > POSITION:覆蓋輸出
            COMMAND >> POSITION: 追加輸出

        錯誤重定向:
            COMMAND 2> POSITION:把錯誤信息覆蓋輸出
            COMMAND 2>> POSITION: 把錯誤信息追加輸出

        合併重定向:
            COMMAND &> POSITION  把錯誤輸出和標準輸出都重定向到指定位置
            COMMAND > POSITION 2> &1

        分別重定向
            COMMAND > POSTIION 2> POSTION2  輸出時,會先清空文件在輸出,無論文件是否爲空都會清空

    輸入重定向:
         COMMAND < POSITION

         <<:Here Document  生成文檔的
[root@linux_basic you]# cat > /tmp/you.txt << EOF
> hello,how are you.
> I love you.
> EOF
[root@linux_basic you]# cat /tmp/you.txt
hello,how are you.
I love you.
[root@linux_basic you]# cat << EOF
> me too.
> This is my book.
> EOF
me too.
This is my book.
[root@linux_basic you]#[root@linux_basic you]# cat write.out
Hollo
[root@linux_basic you]# cat write.err
world
[root@linux_basic you]# ls > write.out 2>write.err
[root@linux_basic you]# cat write.out
are
pam.d
pango
passwd
passwd-
pcmcia
php.d
php.ini
pinforc
pki
plymouth
pm
pm-utils-hd-apm-restore.conf
popt.d
postfix
ppp
prelink.cache
prelink.conf
prelink.conf.d
printcap
profile
profile.d
protocols
test
too.txt
write.err
write.out
[root@linux_basic you]# cat write.err
    文本處理命令:tr
NAME
       tr - translate or delete characters

SYNOPSIS
       tr [OPTION]... SET1 [SET2]

DESCRIPTION
       Translate, squeeze, and/or delete characters from standard input, writing to standard output.   
        tr 'SET1' 'SET2'
            -d: 刪除指定字符集合中的全部字符
      -d, --delete
              delete characters in SET1, do not translate
[root@linux_basic you]# tr 12 df
1234
df34
2123df
fdf3df
^C
[root@linux_basic you]#
[root@linux_basic you]# tr -d 'ab1'
12abdf12
2df2
abaddb123
dd23
^C

    多道輸出:
        COMMAND | tee POSITION
NAME
       tee - read from standard input and write to standard output and files
從標準輸入寫到標準輸出和文件中
SYNOPSIS
       tee [OPTION]... [FILE]...

DESCRIPTION
       Copy standard input to each FILE, and also to standard output.
       


    練習: 
        一、統計當前系統上全部已經登陸的用戶會話數;
        # who | wc -l

        二、列出當前系統上全部已經登陸的用戶的用戶名;
        # who | cut -d' ' -f 1 | sort -u

        三、取出最後登陸到當前系統的用戶的用戶名;  sort -k 3,4 指定分隔符後,列出進行排序比較的字段
        # who | sort -k 3,4 | cut -d' ' -f 1 | tail -1

        四、取出當前系統上被使用的次數最多的shell;(從/etc/passwd中取)
        # cut -d: -f7 /etc/passwd | sort | uniq -c | sort -n | tail -1

        五、將/etc/passwd中第三個字段數據最大的後10個用戶的信息全改成大寫字符後保存到/tmp/mypasswd.txt文件中;         # sort -t: -k3 -n /etc/passwd | tail | tr 'a-z' 'A-Z' > /tmp/mypasswd.txt

相關文章
相關標籤/搜索