shell高级技巧-8个常用的shell实例详解

1. 输出当前系统中占用内存最多的5条命令:

#1) 通过ps命令列出当前主机正在运行的所有进程。
#2) 按照第五个字段基于数值的形式进行正常排序(由小到大)。
#3) 仅显示最后5条输出。

   [root@xieqichao ~]# ps aux | sort -k 5n | tail -5
   stephen   1861  0.2  2.0  96972 21596  ?  S     Nov11   2:24 nautilus
   stephen   1892  0.0  0.4 102108  4508  ?  S<sl Nov11   0:00 /usr/bin/pulseaudio
   stephen   1874  0.0  0.9 107648 10124 ?  S     Nov11   0:00 gnome-volume
   stephen   1855  0.0  1.2 123776 13112 ?  Sl     Nov11   0:00 metacity
   stephen   1831  0.0  0.9 125432  9768  ?  Ssl   Nov11   0:05 /usr/libexec/gnome

    
    
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
2. 找出cpu利用率高的20个进程:
#1) 通过ps命令输出所有进程的数据,-o选项后面的字段列表列出了结果中需要包含的数据列。
#2) 将ps输出的Title行去掉,grep -v PID表示不包含PID的行。
#3) 基于第一个域字段排序,即pcpu。n表示以数值的形式排序。
#4) 输出按cpu使用率排序后的最后20行,即占用率最高的20行。

    
    
  • 1
  • 2
  • 3
  • 4
   [root@xieqichao ~]# ps -e -o pcpu,pid,user,sgi_p,cmd | grep -v PID | sort -k 1n | tail -20

    
    
  • 1
3. 获取当前系统物理内存的总大小:
#1) 以兆(MB)为单位输出系统当前的内存使用状况。
#2) 通过grep定位到Mem行,该行是以操作系统为视角统计数据的。
#3) 通过awk打印出该行的第二列,即total列。

    
    
  • 1
  • 2
  • 3
    [root@xieqichao ~]# free -m | grep "Mem" | awk '{print $2, "MB"}'
    1007 MB

    
    
  • 1
  • 2
4. 获取当前或指定目录下子目录所占用的磁盘空间,并将结果按照从大到小的顺序输出:
#1) 输出/usr的子目录所占用的磁盘空间。
#2) 以数值的方式倒排后输出。

    
    
  • 1
  • 2
  [root@xieqichao ~]# du -s /usr/* | sort -nr
  1443980 /usr/share
  793260   /usr/lib
  217584   /usr/bin
  128624   /usr/include
  60748    /usr/libexec
  45148    /usr/src
  21096    /usr/sbin
  6896      /usr/local
  4           /usr/games
  4           /usr/etc
  0           /usr/tmp

    
    
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
5. 批量修改文件名:
#1) find命令找到文件名扩展名为.output的文件。
#2) sed命令中的-e选项表示流编辑动作有多次,第一次是将找到的文件名中相对路径前缀部分去掉,如./aa改为aa。
#    流编辑的第二部分,是将20110311替换为mv & 20110310,其中&表示s命令的被替换部分,这里即源文件名。
#    \1表示被替换部分中#的\(.*\)。
#3) 此时的输出应为
#    mv 20110311.output 20110310.output
#    mv 20110311abc.output 20110310abc.output
#    最后将上面的输出作为命令交给bash命令去执行,从而将所有20110311*.output改为20110311*.output

    
    
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
   [root@xieqichao ~]# find ./ -name "*.output" -print  | sed -e 's/.\///g' -e 's/20110311\(.*\)/mv & 20110310\1/g' | bash

    
    
  • 1
6. 统计当前目录下文件和目录的数量:
#1) ls -l命令列出文件和目录的详细信息。
#2) ls -l输出的详细列表中的第一个域字段是文件或目录的权限属性部分,如果权限属性部分的第一个字符为d,
#    该文件为目录,如果是-,该文件为普通文件。
#3) 通过wc计算grep过滤后的行数。

    
    
  • 1
  • 2
  • 3
  • 4
   [root@xieqichao ~]# ls -l * | grep "^-" | wc -l
   [root@xieqichao ~]# ls -l * | grep "^d" | wc -l

    
    
  • 1
  • 2
7. 杀掉指定终端的所有进程:
#1) 通过ps命令输出终端为pts/1的所有进程。
#2) 将ps的输出传给grep,grep将过滤掉ps输出的Title部分,-v PID表示不包含PID的行。
#3) awk打印输出grep查找结果的第一个字段,即pid字段。
#4) 上面的三个组合命令是在反引号内被执行的,并将执行的结果赋值给数组变量${K}。
#5) kill方法将杀掉数组${K}包含的pid。

    
    
  • 1
  • 2
  • 3
  • 4
  • 5
   [root@xieqichao ~]# kill -9 ${K}=`ps -t pts/1 | grep -v PID | awk '{print $1}'`    

    
    
  • 1
8. 将查找到的文件打包并copy到指定目录:
#1) 通过find找到当前目录下(包含所有子目录)的所有*.txt文件。
#2) tar命令将find找到的结果压缩成test.tar压缩包文件。
#3) 如果&&左侧括号内的命令正常完成,则可以执行&&右侧的shell命令了。
#4) 将生成后的test.tar文件copy到/home/.目录下。

    
    
  • 1
  • 2
  • 3
  • 4
  [root@xieqichao ~]# (find . -name "*.txt" | xargs tar -cvf test.tar) && cp -f test.tar /home/.

    
    
  • 1
#1) cpio从find的结果中读取文件名,将其打包压缩后发送到./dest/dir(目标目录)。
#2) cpio的选项介绍:
#    -d:创建需要的目录。
#    -a:重置源文件的访问时间。
#    -m:保护新文件的修改时间。
#    -p:将cpio设置为copy pass-through模式。

    
    
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  [root@xieqichao ~]# find . -name "*" | cpio -dampv ./dest/dir

    
    
  • 1
                                </div>
            <link href="https://csdnimg.cn/release/phoenix/mdeditor/markdown_views-b6c3c6d139.css" rel="stylesheet">
                                            <div class="more-toolbox">
            <div class="left-toolbox">
                <ul class="toolbox-list">
                    
                    <li class="tool-item tool-active is-like "><a href="javascript:;"><svg class="icon" aria-hidden="true">
                        <use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#csdnc-thumbsup"></use>
                    </svg><span class="name">点赞</span>
                    <span class="count"></span>
                    </a></li>
                    <li class="tool-item tool-active is-collection "><a href="javascript:;" data-report-click="{&quot;mod&quot;:&quot;popu_824&quot;}"><svg class="icon" aria-hidden="true">
                        <use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#icon-csdnc-Collection-G"></use>
                    </svg><span class="name">收藏</span></a></li>
                    <li class="tool-item tool-active is-share"><a href="javascript:;" data-report-click="{&quot;mod&quot;:&quot;1582594662_002&quot;}"><svg class="icon" aria-hidden="true">
                        <use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#icon-csdnc-fenxiang"></use>
                    </svg>分享</a></li>
                    <!--打赏开始-->
                                            <!--打赏结束-->
                                            <li class="tool-item tool-more">
                        <a>
                        <svg t="1575545411852" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5717" xmlns:xlink="http://www.w3.org/1999/xlink" width="200" height="200"><defs><style type="text/css"></style></defs><path d="M179.176 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5718"></path><path d="M509.684 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5719"></path><path d="M846.175 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5720"></path></svg>
                        </a>
                        <ul class="more-box">
                            <li class="item"><a class="article-report">文章举报</a></li>
                        </ul>
                    </li>
                                        </ul>
            </div>
                        </div>
        <div class="person-messagebox">
            <div class="left-message"><a href="https://blog.csdn.net/xie_qi_chao">
                <img src="https://profile.csdnimg.cn/B/F/6/3_xie_qi_chao" class="avatar_pic" username="xie_qi_chao">
                                        <img src="https://g.csdnimg.cn/static/user-reg-year/1x/2.png" class="user-years">
                                </a></div>
            <div class="middle-message">
                                    <div class="title"><span class="tit"><a href="https://blog.csdn.net/xie_qi_chao" data-report-click="{&quot;mod&quot;:&quot;popu_379&quot;}" target="_blank">解启超</a></span>
                                        </div>
                <div class="text"><span>发布了354 篇原创文章</span> · <span>获赞 52</span> · <span>访问量 3万+</span></div>
            </div>
                            <div class="right-message">
                                        <a href="https://im.csdn.net/im/main.html?userName=xie_qi_chao" target="_blank" class="btn btn-sm btn-red-hollow bt-button personal-letter">私信
                    </a>
                                                        <a class="btn btn-sm attented bt-button personal-watch" data-report-click="{&quot;mod&quot;:&quot;popu_379&quot;}">已关注</a>
                                </div>
                        </div>
                </div>
</article>
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

尹汇川

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值