Some of the Linux Shell is very useful but useful instructions

zhaozj2021-02-16  43

Some of the Linux Shell is very useful but useful instructions

You have forgotten you can use the command column to complete your work, or you never know that he can save you a lot of time and worry?

There is a concept called 80/20 rules that can be associated with the software system? A Observed the founder, the 80% of the user group, fixed only 20% of the system functionality. I don't need to confirm such a statistical value, I have built a soft architecture and the experience of using the software system, and I have it to be correct. A pile of Linux commands have no exception to such universal phenomena. Linux provides a number of monolith-level commands, which may only have ten universal understandings, and the remaining part is almost ignored.

These small things in the Linux Shell are what is the value of being discussed? I will introduce a few unused uses, but quite good Linux shell commands, these programs I use After several years, it depends on time test. In particular, I chose to focus on the command of grammar analysis and formatting text content.

In these examples, I assume that everyone has basic familiarity on the syntax of the command, and some simple shell concepts are not so popular Linux commands. Even so, the example of the command column still attached an appropriate annotation and it is very straightforward. On each application, there will be a use example under the execution of each command column.

The following is the presentation of these grammar, format, and text content, although not every example provides specific instructions, but it must be known that if the following commands do not specify file parameters, it will read standard inputs.

HEAD / TAIL

Head! With TAIL is just like its name, it is easy to understand, it is used to display the 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 thinks that it is to see the file. Take a look at the example below:

## (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 First, the first 6 lines of the display of the file are the first 25 lines that display the files.

The following range, combined with the Head and Tail instructions, showing the 11th line of the file to 20th line:

# (3)

HEAD -20 file | tail -10

In the TAIL's man page, it shows more available parameters than the HEAD, which has a very good parameter "-f". When using this parameter, TAIL will not return the end signal, unless we go to interrupt it. Conversely, it will wait for a while, until he found that the information is added to a new line after the last time it is read, and it is added to a new line:

## (4) Display ONGOING Updates to the given

## log file

Tail -f /usr/tmp/logs/daemon_log.txt

The above examples can dynamically display dynamic updates of the log file.

Suppose the service program is constantly adding dynamic information to the log file of /usr/adm/logs/daemon_log.txt, using tail! -F in the command control window, it will track the file at a certain time All updates. (-f only can be used when it is input to the file).

If you have a plurality of file parameters after tail, you can track several log files in the same window:

## 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 - Continvenation?!

How to fight Cat? Yes !! This is the function of TAC ~ It is in the order of the order of the file, then ~ Is it under what condition? Any order that must be advanced later The work of rearrangement is used to use it! The following instructions are used to list three newly built user accounts with the first sequence established by the last established.

# (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

MoE: x: 1001: 100: 1st stooge: / homes / moe: / bin / ksh

NL - to the output column plus number

NL is a simple but very useful number filtering tool. I am in the place where I entered, I'm going to stand on the left, NL is for a lot of parameters, which can be defined fine Very thin? Bear {sequence to make it a good number of output, the following is an example description of some parameters:

# 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 AN

# '=' 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 - formatting

FMT is a simple text format tool that focuses on the width of the text and the maximum support, it is accomplished using the combination and cutouts. The premise is that you need to maintain the text content generated by the document processor. The text it output may contain a variety of different lengths. If such text is made by the text editor (like Vi), FMT can transform this original text into better maintenance format. The first example below shows that the FMT command is displayed to re-create the file format, let it first? 璊ㄜ n More than 80 words:

# (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 - Segmentation

Fold is very similar to FMT, but he is usually used to format data with non-increasing text readability. There is a simple example below: # (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 Itereate 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 group converter, its practical application is a bit repeated with some more complex programs, like SED and AWK [larger binary code code]. Tr is very easy to use by the replacement of text. Its instruction mode is "! From (placed in the first parameter) ... to the mode (second parameter) ..." mode; below for its general use syntax:

# (13) TR USAGE

Tr [options] "set1" ["set2"] Output

It should be noted that it does not accept file parameters, it is read standard input and then writes the standard output. In the case of two parameters, TR will process the character in SET1, and then according to the character in Set2 Replace.

in conclusion

This article is the most important thing to remind everyone that there are many things that don't care in the system, in fact, it is very easy to use. My purpose is to let everyone know that there are some very few features and show that these tools are actually their value. Maybe you will feel that it is better to use x - when you write some Script, Or when you are caught in a place, maybe it, but I suggest you find more information, O'Reilly Linux in a nutshell is a good reference source, it is very good, it can be I'm looking for something you want, of course, I also encourage you to check its installed manual (cash with manager) also with reference to the relevant information page (cash, you can use Info instruction Query the contents of each command), but not, instructions in all commands can be found.

转载请注明原文地址:https://www.9cbs.com/read-24761.html

New Post(0)