LinuxShell? Some are rarely used? S useful commands
Source: Internet
Author: User
Article title: LinuxShell? Some are rarely used? S is a useful command. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
Edit responsibility: NicaLiu have forgotten whether you can use the command column to complete the work, or you never know whether it can save you a lot of time and trouble?
There is a concept called the 80/20 rule that can be associated with the software system. it is found that 80% of the user groups will always use only 20% of the system functions. I don't need to go to the big expense weekly chapter to confirm this statistical value. my more than 20 years of experience in establishing software architecture and using software systems tells me that such assumptions are likely to be correct. A bunch of Linux command-column programs have no exception. Linux provides many Shell-level commands, and only 10 of them can be widely understood and used, while most of the remaining commands are ignored.
What are the inconspicuous things in Linux Shell that have the value to be discussed? Here I will briefly introduce a few Linux Shell commands that are not commonly used but are quite useful. these programs have been used for several years and can withstand the test of time. In particular, I chose to focus on the syntax parsing and formatting of text content commands.
In these examples, I assume that you are familiar with the syntax of command columns, and have some simple Shell commands that are not so common in Linux. Even so, the example of the command column is still attached with appropriate annotations and it is very direct. For each application, an example is provided below the execution of each command column.
The following shows the syntax, format, and text content. although not all examples provide specific instructions, you must be aware that if the following commands do not specify file parameters, then it reads the standard input.
Head/Tail
The head and tail are just as easy to understand as their names. they are used to display a certain number of text blocks at the beginning or end, and the head is used to display the beginning of the file to the standard output, and tail simply looks at the end of the file ??, Take a look at the following example:
# (1) displays the first 6 lines of a file
Head-6 readme.txt
# (2) displays the last 25 lines of a file
Tail-25 mail.txt
Example 1 shows the first six rows of an object, and example 2 shows the last 25 rows of the object.
The following example combines the head and tail commands to display rows 11th to 20th of the archive:
# (3)
Head-20 file | tail-10
The tail user manual page displays more available parameters than the head. one of the useful parameters is "-f". When this parameter is used, tail does not return the end signal, unless we interrupt it on our own; on the contrary, it will wait for a while until he finds that the data is added to a new row after it is last read:
# (4) display ongoing updates to the given
# Log file
Tail-f/usr/tmp/logs/daemon_log.txt
The above example can dynamically display the dynamic updates of the log file.
Assuming that the service program keeps adding dynamic data to the log file/usr/adm/logs/daemon_log.txt, use tail-f in the command column control window, it will track all updates of this file in real time at a certain time. (-F can only be used when it is input as an archive ).
If you have multiple file parameters after tail, can you track several logs in the same window at a time? N:
# Track the mail log and the server error log
# At the same time.
Tail-f/var/log/mail. log/var/log/apache/error_log
Tac -- connect them in turn ?!
How does cat work together? Yes !! What is the tac function? ~ It concatenates the ordered content of an archive ~ Under what conditions is it used? Any work that requires components to be re-arranged in the FIFO order can use it! In the following command, the three most newly created user accounts are listed in the order from the last one to the first one:
# (5) last 3/etc/passwd records-in reverse
$ Tail-3/etc/passwd | tac
Curly: x: 1003: 100: 3rd Stooge:/homes/curly:/bin/ksh
Larry: x: 1002: 100: 2nd Stooge:/homes/larry:/bin/ksh
Mo E: x: 1001: 100: 1st Stooge:/homes/moe:/bin/ksh
Nl -- add a number to the output column
Nl is a simple but easy-to-use numbering filtering tool. I set the parameter in the input place to make the encoding of all columns depend on the left. nl is used by many parameters, you can define a very fine program to define the output of the serial number. Below are some examples of parameter usage:
# (6) Display the first 4 entries of the password
# File-numbers to be three columns wide and
# Padded by zeros.
$ Head-4/etc/passwd | nl-nrz-w3
001 root: x: 0: 1: Super-User: // bin/ksh
002 daemon: x: 1: 1 ::/:
003 bin: x: 2: 2:/usr/bin:
004 sys: x: 3: 3 ::/:
#
# (7) Prepend ordered line numbers followed by
# '= 'Sign to each line -- start at 101.
$ Nl-s =-v101 Data.txt
101 = 1st Line...
102 = 2nd Line...
103 = 3rd Line...
104 = 4th Line...
105 = 5th Line...
.......
Fmt -- Format
Fmt is a simple text formatting tool. it focuses on keeping the text data consistent with the maximum supported width. it is accomplished by combining and truncating the blank rows and columns. The premise is that you need to maintain the text content generated by the file processor. The text output may contain various columns of different lengths, if such text is made by a text editor (such as vi), fmt can convert the original text into a better maintained format. In the first example below, the fmt command is displayed to recreate the file format so that it can contain at least 80 characters in a line:
# (8) No more than 60 char lines
$ Fmt-w 60 README.txt> NEW_README.txt
#
# (9) Force uniform spacing:
#1 space between words, 2 between sentences
$ Echo "Hello World. Hello Universe." |
Fmt-u-w80
Hello World. Hello Universe.
Fold -- segment input
Fold is similar to fmt, but it is usually used to format and not to increase text readability. The following is a simple example:
# (10) Format text in 3 column width lines
$ Echo oxoxoxoxo | fold-w3
Oxo
Xox
Oxo
# (11) Parse by triplet-char strings-
# Search for 'xox'
$ Echo oxoxoxoxo | fold-w3 | grep "xox"
Xox
# (12) One way to iterate through a string of chars
$ For I in $ (echo 12345 | fold-w1)
> Do
#### Perform some task...
> Print $ I
> Done
1
2
3
4
5
Tr
Tr is a simple word group converter. its actual application is a bit repetitive with some complicated programs, such as sed and awk [larger binary program code]. Tr is useful in text replacement, deletion, and addition. Its command mode is "slave (placed in the first parameter )... To (second parameter )... "Mode; the general syntax is as follows:
# (13) tr usage
Tr [options] "set1" ["set2"]
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