Common Linux Shell skills (10)

Source: Internet
Author: User

20. Use the shell command in the pipeline to obtain system running data:

1. output the five commands that occupy the most memory in the current system :
#1) run the ps command to list all processes running on the current host.
#2) The fifth field is sorted (from small to large) in the numerical form ).
#3) Only the last five outputs are displayed.
/> PS aux | sort-K 5N | tail-5
Stephen 1861 0.2 2.0 96972 21596? S nov11 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 20 processes with high CPU utilization :
#1) output the data of all processes through the ps command. The field list after the-O option lists the data columns to be included in the result.
#2) Remove the title line output by PS. grep-v pid indicates the line without PID.
#3) sort by the first domain field, that is, pcpu. N indicates that values are sorted.
#4) output the last 20 rows after sorting by CPU usage, that is, the 20 rows with the highest usage.
/> PS-e-o pcpu, PID, user, sgi_p, CMD | grep-v pid | sort-K 1N | tail-20

3. Obtain the total physical memory size of the current system:
#1) output the current memory usage of the system in MB.
#2) locate the mem row through grep, Which is statistical data from the operating system perspective.
#3) print the second column of the row through awk, that is, the total column.
/> Free-M | grep "mem" | awk '{print $2, "MB "}'
1007 MB

21. system management by using shell commands combined with pipelines:

1. Obtain the disk space occupied by the subdirectories in the current or specified directory, and output the results in ascending order:
#1) disk space occupied by the output/usr subdirectory.
#2) inverted output in numerical mode.
/> 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

2. Modify file names in batches :
#1) Find the file with the file name extension. output.
#2) the "-e" option in the SED command indicates that the stream editing action has been performed for multiple times. The first time is to remove the relative path prefix in the file name, for example, change./AA to AA.
# The Second part of stream editing is to replace 20110311 with MV & 20110310, where & represents the replacement part of the S command, which is the source file name.
# \ 1 indicates the \ (. * \) of # In the replaced part \(.*\).
#3) the output at this time should be
# Mv 20110311. Output 20110310. Output
# Mv 20110311abc. Output 20110310abc. Output
# Finally, give the above output as a command to the bash command for execution, so that all 20110311 *. Change output 20110311 *. Output
/> Find. /-name "*. output "-print | sed-E's /. \ // G'-E's/20110311 \(. * \)/mv & 20110310 \ 1/G' | bash

3. count the number of files and directories in the current directory :
#1) the LS-l command lists detailed information about files and directories.
#2) The first domain field in the LS-L output detail list is the permission attribute section of the file or directory. If the first character of the permission attribute section is d,
# This file is a directory. If it is-, this file is a normal file.
#3) calculate the number of rows filtered by grep through WC.
/> Ls-L * | grep "^-" | WC-l
/> Ls-L * | grep "^ d" | WC-l

4. Kill all processes on the specified terminal:
#1) Output all processes whose terminal is pts/1 through the ps command.
#2) Pass the PS output to grep, and grep filters out the title part of the PS output.-v pid indicates the line that does not contain the PID.
#3) awk prints the first field in the grep search result, that is, the PID field.
#4) the preceding three combined commands are executed in the back quotes and assigned the execution result to the array variable $ {k }.
#5) the kill method will kill the PID contained in the array $ {k.
/> Kill-9 $ {k} = 'ps-T pts/1 | grep-v pid | awk '{print $1 }''

5. Package the searched files and copy them to the specified directory. :
#1) Find all *. txt files in the current directory (including all subdirectories) through find.
#2) the tar command compresses the findfound result into the test.tar compressed package file.
#3) if the & commands in the left-side brackets are normal, run the & shell command on the right.
#4) copy the generated test.tar file to the/home/. directory.
/> (Find.-Name "*. txt" | xargs tar-CVF test.tar) & CP-F test.tar/home /.

#1) cpio reads the file name from the find result, compress it, and sends it to./DEST/Dir (target directory ).
#2) cpio options:
#-D: create the required directory.
#-A: reset the access time of the source file.
#-M: Specifies the modification time of the new file.
#-P: Set cpio to copy pass-through mode.
/> Find.-Name "*" | cpio-dampv./DEST/Dir

It should be noted that the vast majority of examples in this blog are from the Internet and are filtered out after a day or so, the comments are added later to facilitateWeUnderstanding during reading. If you find a better and more clever Shell Combination command in the future, I will continue to update this blog. If you have a really good shell command combination and are willing to share it with us, you can directly put it in the reply. I will always pay attention to this blog.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.