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

mv命令:
NAME
       mv - move files  移动文件

SYNOPSIS
       mv [-fi] source_file target_file

       mv [-fi] source_file... target_file   源文件有多个,则目标必须存在且为目录
        mv SRC... DEST

        -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.

       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.txt


    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.

       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.

       -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试一下
        1、显示当前系统上每个用户的shell;
            # cut -d: -f1,7 /etc/passwd

        2、显示当前系统上所有用户使用的各种shell;
            # cut -d: -f7 /etc/passwd | sort | uniq

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

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

        5、统计/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.
       


    练习: 
        1、统计当前系统上所有已经登录的用户会话数;
        # who | wc -l

        2、列出当前系统上所有已经登录的用户的用户名;
        # who | cut -d' ' -f 1 | sort -u

        3、取出最后登录到当前系统的用户的用户名;  sort -k 3,4 指定分隔符后,列出进行排序比较的字段
        # who | sort -k 3,4 | cut -d' ' -f 1 | tail -1

        4、取出当前系统上被使用的次数最多的shell;(从/etc/passwd中取)
        # cut -d: -f7 /etc/passwd | sort | uniq -c | sort -n | tail -1

        5、将/etc/passwd中第三个字段数据最大的后10个用户的信息全改为大写字符后保存到/tmp/mypasswd.txt文件中;
        # sort -t: -k3 -n /etc/passwd | tail | tr 'a-z' 'A-Z' > /tmp/mypasswd.txt

转载于:https://my.oschina.net/u/1762991/blog/662943

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值