shell advanced techniques -8 common shell example explanation

1. Output current occupies most of the system memory five commands:

# 1) lists all processes currently running on the computers by the ps command.
# 2) normal ordered (ascending) based on a numerical value according to the fifth field.
# 3) displays only the last five output.

   [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
2. Find high cpu utilization rate of 20 processes:
#1) 通过ps命令输出所有进程的数据,-o选项后面的字段列表列出了结果中需要包含的数据列。
#2) 将ps输出的Title行去掉,grep -v PID表示不包含PID的行。
#3) 基于第一个域字段排序,即pcpu。n表示以数值的形式排序。
#4) 输出按cpu使用率排序后的最后20行,即占用率最高的20行。
   [root@xieqichao ~]# ps -e -o pcpu,pid,user,sgi_p,cmd | grep -v PID | sort -k 1n | tail -20
3. Get the current physical memory size of the system:
#1) 以兆(MB)为单位输出系统当前的内存使用状况。
#2) 通过grep定位到Mem行,该行是以操作系统为视角统计数据的。
#3) 通过awk打印出该行的第二列,即total列。
    [root@xieqichao ~]# free -m | grep "Mem" | awk '{print $2, "MB"}'
    1007 MB
4. Get the current directory or subdirectory specified disk space occupied, and outputs the result in descending order:
#1) 输出/usr的子目录所占用的磁盘空间。
#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
5. Modify Batch File Name:
#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
   [root@xieqichao ~]# find ./ -name "*.output" -print  | sed -e 's/.\///g' -e 's/20110311\(.*\)/mv & 20110310\1/g' | bash
6. count the number of the current directory files and directories:
#1) ls -l命令列出文件和目录的详细信息。
#2) ls -l输出的详细列表中的第一个域字段是文件或目录的权限属性部分,如果权限属性部分的第一个字符为d,
#    该文件为目录,如果是-,该文件为普通文件。
#3) 通过wc计算grep过滤后的行数。
   [root@xieqichao ~]# ls -l * | grep "^-" | wc -l
   [root@xieqichao ~]# ls -l * | grep "^d" | wc -l
7. kill all processes designated terminal:
#1) 通过ps命令输出终端为pts/1的所有进程。
#2) 将ps的输出传给grep,grep将过滤掉ps输出的Title部分,-v PID表示不包含PID的行。
#3) awk打印输出grep查找结果的第一个字段,即pid字段。
#4) 上面的三个组合命令是在反引号内被执行的,并将执行的结果赋值给数组变量${K}。
#5) kill方法将杀掉数组${K}包含的pid。
   [root@xieqichao ~]# kill -9 ${K}=`ps -t pts/1 | grep -v PID | awk '{print $1}'`    
8. find the package and copy the file to the specified directory:
#1) 通过find找到当前目录下(包含所有子目录)的所有*.txt文件。
#2) tar命令将find找到的结果压缩成test.tar压缩包文件。
#3) 如果&&左侧括号内的命令正常完成,则可以执行&&右侧的shell命令了。
#4) 将生成后的test.tar文件copy到/home/.目录下。
  [root@xieqichao ~]# (find . -name "*.txt" | xargs tar -cvf test.tar) && cp -f test.tar /home/.
#1) cpio从find的结果中读取文件名,将其打包压缩后发送到./dest/dir(目标目录)。
#2) cpio的选项介绍:
#    -d:创建需要的目录。
#    -a:重置源文件的访问时间。
#    -m:保护新文件的修改时间。
#    -p:将cpio设置为copy pass-through模式。
  [root@xieqichao ~]# find . -name "*" | cpio -dampv ./dest/dir
Published 350 original articles · won praise 52 · views 30000 +

Guess you like

Origin blog.csdn.net/xie_qi_chao/article/details/105039238