zoukankan      html  css  js  c++  java
  • 文件查看、命令查找、字符处理命令

    练习作业
    1.在用户目录下创建6个文件song1.mp3~song6.mp3
    touch song{1..6}.mp3

    2.把上面创建的songX.mp3文件移动到/Music目录下(不存在则创建)
    mkdir -p /Music
    mv /root/song?.mp3 /Music/

    3.在用户家目录下创建三个目录,分别为friends,family,work
    mkdir friends family work

    4.切换到friends目录下,把/Music目录下的song1.mp3~song3.mp3拷贝到当前目录
    cd ./friends/
    cp /Music/song{1..3}.mp3 /root/friends/
    cp /Music/song{1..3}.mp3 .

    5.切换到family目录下,把/Music目录下的song4.mp3~song6.mp3移动到当前目录
    cd ../family/
    mv /Music/song{4..6}.mp3 .

    6.切换到用户主目录下,删除family目录
    cd
    rm -rf family/

    7.切换到friends,把目录下的所有文件删除
    cd friends/
    rm -rf * #不能删除隐藏文件
    rm -rf .* #删除隐藏文件

    8.切换到主目录,把friends目录删除
    cd
    rm -rf friends/

    文件查看命令
    cat
         cat >xiao.txt<<EOF //新建或覆盖文件数据
         cat >>xiao.txt <<EOF //追加文件内容 

    将文件内容打印到显示器
    [root@oldgirl ~]# cat /etc/hosts
    127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
    ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6

    more
          分页浏览
                 回车一次滚动一行;空格翻页
    less
          分页浏览
                 可反复查看
                 q退出
    head
           从文件头部看起,默认10行
           -n:表示查看前n行
    tail
           从文件尾部看起,默认10行,适合查看文件的更新信息(/etc/passwd,日志文件)
           -n:表示查看后n行
           -f:跟踪
           ctrl + c 退出
    [root@oldgirl ~]# tail /var/log/messages #查看公共日志文件

    命令查找命令
    locate //查找文件或者目录
    yum install -y mlocate.x86_64
    updatedb
    locate /root/1 //搜索root目录下所有以1开头的文件
    locate -i /root/2 //搜索root目录下所有以2开头的文件,忽略大小写

    which //查找系统PATH变脸目录下的命令(绝对路径)
    示例:which ls
    [root@oldgirl ~]# echo $PATH
    /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

    [root@oldgirl ~]# which ls
    alias ls='ls --color=auto'
    /usr/bin/ls
    [root@oldgirl ~]# rpm -qf /usr/bin/ls
    coreutils-8.22-24.el7.x86_64

    whereis //查找文件索引数据库下的命令、源文件、man文件、非PATH变量查找,所以查找的面比which要广-b,-m
    示例: whereis ls
    示例: whereis -b ls

    字符处理命令
    sort [OPTION]...[FILE]...
    -r:倒序
    -n:按数字排序
    -t:指定分隔符(默认空格)
    -k:指定第几列,指定几列几字符


    //演示sort用法 首先创建一个test.txt文件
    [root@oldgirl ~]# cat >demo.txt<<EOF
    > g:2
    > a:13
    > e:6
    > c:9
    > s:8
    > i:7
    > EOF

    //下面对输出内容排序:
    [root@oldgirl ~]# sort demo.txt
    a:13
    c:9
    e:6
    g:2
    i:7
    s:8

    //可观察到,demo文件具有一个特点,第一个字符是字母,第三个字符是数字,中间用冒号隔开
    //这样我们可以使用-t,使用-k指定用于排序的列
    [root@oldgirl ~]# sort -t ":" -k2 demo.txt
    a:13 //第1行为什么是13?不应该按照顺序排列
    g:2
    e:6
    i:7
    s:8
    c:9

    //按照排序的方式,只会看到第一个字符,13的第一个字符是1,按照字符来排序确实比2小
    //如果想按照数字的方式来进行排序,需要使用-n参数
    [root@oldgirl ~]# sort -t: -k2 -n demo.txt
    [root@oldgirl ~]# sort -t: -n -k2 demo.txt
    g:2
    e:6
    i:7
    s:8
    c:9
    a:13

    [root@oldgirl ~]# sort -t: -k2 -nr demo.txt
    [root@oldgirl ~]# sort -t: -k2nr demo.txt
    a:13
    c:9
    s:8
    i:7
    e:6
    g:2

    //sort测试案例,对下面内容排序
    cat >test.txt<<EOF
    192.168.1.11 00:15:25:85:AE:12
    192.168.11.251 00:0F:AF:85:5B:41
    192.168.71.224 00:0F:AF:85:5E:21
    192.168.1.11 00:15:25:85:AE:12
    192.168.1.11 00:15:25:85:AE:12
    192.168.23.16 00:71:0F:B2:E3:F4
    192.168.0.1 00:51:0F:85:47:21
    192.168.111.16 00:55:AF:B2:A1:A2
    192.168.84.121 00:A4:2B:C9:30:76
    192.168.121.5 00:0F:AE:AF:12:13
    192.168.116.11 00:0F:E7:2C:56:37
    192.168.116.11 00:0F:E7:2C:56:37
    192.168.23.16 00:71:0F:B2:E3:F4
    192.168.1.12 00:0F:0E:51:58:22
    EOF

    //针对第三列的第一个字符,第四列的第一个字符到第三个字符排序才是正确
    -k
    start_first.last,end_first.last
    00-99
    [root@oldgirl ~]# sort -t. -k3.1,3.3n -k4.1,4.3n test.txt | uniq -c
    1 192.168.0.1 00:51:0F:85:47:21
    3 192.168.1.11 00:15:25:85:AE:12
    1 192.168.1.12 00:0F:0E:51:58:22
    1 192.168.11.251 00:0F:AF:85:5B:41
    2 192.168.23.16 00:71:0F:B2:E3:F4
    1 192.168.71.224 00:0F:AF:85:5E:21
    1 192.168.84.121 00:A4:2B:C9:30:76
    1 192.168.111.16 00:55:AF:B2:A1:A2
    2 192.168.116.11 00:0F:E7:2C:56:37
    1 192.168.121.5 00:0F:AE:AF:12:13
    使用uniq删除重复内容希望能删除重复的行,同时还可以统计出完全相同的行出现的总次数
    那么就可以使用uniq命令解决这个问题(但必须配合sort使用)

    uniq [OPTION]...[FILE]...
    -c 计算重复的行

    统计一下自己使用的命令的排序
    提示:history
    history | awk "{print $2}" | sort | uniq -c | sort -rn

    使用cut命令截取某一个字段
    cut [OPTION]...[FILE]...
    -d 指定分隔符
    -f 数字,取第几列 -f3,6三列和六列
    -c 按字符取(空格也算

  • 相关阅读:
    ES6的新特性(18)——async 函数
    ES6的新特性(17)——Generator 函数的异步应用
    ES6的新特性(16)——Generator 函数的语法
    ES6的新特性(15)——Promise 对象
    ES6的新特性(14)——Iterator 和 for...of 循环
    ES6的新特性(13)——Symbol
    ES6的新特性(12)——Set 和 Map 数据结构
    ES6的新特性(11)——Class 的继承
    我的游戏学习日志22——游戏元素的解析(6)
    我的游戏学习日志21——游戏元素的解析(5)
  • 原文地址:https://www.cnblogs.com/xmtxh/p/11681748.html
Copyright © 2011-2022 走看看