Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand Linux File Redirection and File filter

2025-04-03 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article shows you how to understand Linux file redirection and file filter, the content is concise and easy to understand, can definitely brighten your eyes, through the detailed introduction of this article, I hope you can get something.

The Linux system manages everything in the form of files, so it is necessary for us to have some understanding of the operation of the files.

File filter

The commands used to read and perform operations on the contents of a file are sometimes called filters. The sed and awk commands are examples of two filters, because they were discussed in detail in previous articles and will be skipped here.

Commands such as cat, more, and less allow you to view the contents of a text file from the command line without invoking the editor. Cat, which stands for "concatenate", will display the contents of the file on the standard output (display screen) by default. One of the most interesting options provided with cat is the-n option, which displays the contents of the file with numbered output lines.

$cat-n test.out

1 This is a test.

Because cat outputs all the lines in the file at once, you may prefer to use more and less because they both output the contents of the file one screen at a time. Less is an enhanced version of more that allows you to enhance file viewing with key commands from the vi text editor. For example, d tumbles forward and b tumbles N lines backward (if N is specified before d or b). The value entered for N becomes the default value for the subsequent d command. The man page utility uses less to display the contents of the instructions.

Linux file redirection and plumbing

Linux file redirection allows command output to be redirected to a file instead of standard output, or similarly, input can be redirected. The standard symbol for Linux file redirection > creates a new file. The symbol adds the output to an existing file:

$more test2.out

Another test.

$cat test.out > > test2.out

$cat test2.out

Another test.

This is a test.

Standard input to the file can be used

< 符号来重定向: $ cat < test2.out 错误消息用 2>

And 2 > > to redirect and add, in the following format:

$command 2 > name_of_error_file

To avoid inadvertently overwriting an existing file, use the command set built into BASH:

$set-o noclobber

You can use > between commands and output files! Symbol to overload this feature. To turn off this feature, replace-o with + o.

Linux file redirection works between a command and a file or between a file and a file. One item of the Linux file redirection statement must be a file.

Pipes use the | symbol and work between commands. For example, you can send the output of a command directly to the printer in the following ways:

$ls-l * | lpr

You can quickly find a command in the history list in the following ways:

$history | grep cat

More filters

Grep, fgrep, and egrep all display rows that match a pattern. All three commands search for the specified pattern in the file, which is useful if you can't remember the name of a desired file. The basic format is:

Grep [options] PATTERN [FILE...]

$grep-r 'Subject' nsmail

CTRL-Z will terminate the above or any other command.

Probably the most useful option for grep is-s. If you search for system files as anything other than the root user, an error message will be generated for each file that you do not have access to. This order forbids those messages.

Fgrep (also called with grep-F) looks only for fixed strings, not regular expressions received by grep. Egrep, on the other hand, receives a pattern that contains a wider range of special characters, such as "|", which indicates the conditional OR operator.

$egrep 'Subject | mailto' *

The above is how to understand Linux file redirection and file filter. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report