Linux Shell Common Tips (10) pipeline combination

Source: Internet
Author: User

Linux Shell Common Tips (10) pipeline combination

Twenty. To get the system run data through the pipeline Combination shell command :


1. Output the 5 most memory-intensive commands in the current system:
#1) Lists all the processes that are running on the current host through the PS command.
#2) The normal sort (from small to large) in the form of a numeric value in the Fifth field.
#3) shows only the last 5 outputs.
/> 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. Identify 20 processes with a high CPU utilization:
#1) outputs data from all processes through the PS command, and the field list following the-O option lists the columns of data that need to be included in the results.
#2) The title line of the PS output is removed, and the Grep-v PID represents a line that does not contain a PID.
#3) is sorted based on the first field field, Pcpu. n means sorting in numeric form.
#4) Outputs the last 20 rows sorted by CPU utilization, or 20 rows with the highest occupancy.
/> ps-e-o pcpu,pid,user,sgi_p,cmd | grep-v pid | sort-k 1n | tail-20

3. Get the total size of the current system's physical memory:
#1) outputs the current memory usage of the system in megabytes (MB).
#2) locates the mem line through grep, which is statistically based on the operating system.
#3) prints out the second column of the row, the total column, through awk.
/> Free-m | grep "Mem" | awk ' {print $, ' MB '} '
1007 MB

21. System management through the pipeline combination shell command:

1. Get the disk space occupied by the subdirectory under the current or specified directory, and output the results in order from the largest to the smaller:
#1) The disk space consumed by the output/USR sub-directory.
#2) The output is inverted in numerical form.
/> 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. Batch modification of file names:
#1) The Find command finds a file with an. output file name extension.
#2) The-e option in the SED command indicates that the stream edit action is repeated several times, and the first time is to remove the relative path prefix portion of the found file name, such as./aa to AA.
# The second part of stream editing is to replace 20110311 with MV & 20110310, where & represents the replaced part of the S command, here is the source file name.
# \1 represents 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, the above output is given as a command to the Bash command to execute, thus all20110311*.output instead20110311*.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 the file and directory details.
#2) The first field field in the detailed list of ls-l outputs is the permission attribute portion of the file or directory, if the first character in the Permission Properties section is D,
# The file is a directory, if yes-, the file is a normal file.
#3) calculates the number of rows after grep filtering through the WC.
/> Ls-l * | grep "^-" | wc-l
/> Ls-l * | grep "^d" | wc-l

4. Kill all processes in the specified terminal:
#1) Outputs all processes of the terminal to PTS/1 through the PS command.
#2) The output of PS to Grep,grep will filter out the title part of PS output, and the-V PID indicates the line without PID.
#3) awk prints out the first field of the grep lookup result, the PID field.
#4) The three combined commands above are executed in the inverted quotation marks, and the result of the execution is assigned 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 '} '

5. Package and copy the found file to the specified directory:
#1) finds all the *.txt files in the current directory (including all subdirectories) through find.
#2) The tar command compresses the results found by find into a Test.tar compressed package file.
#3) If the command inside the && left bracket is completed properly, you can execute && the shell command on the right side.
#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 results of find, packages it compressed, and sends it to./dest/dir (destination directory).
#2) Cpio options are described in:
#-D: Create the desired directory.
#-A: Resets the access time of the source file.
#-M: protects the new file from modification time.
#-P: Sets the Cpio to copy pass-through mode.
/> Find-name "*" | cpio-dampv./dest/dir


Linux Shell Common Tips (10) pipeline combination

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.