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

The basic explanation of the redirection of Linux I Pot O

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

Share

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

This article mainly introduces "Linux I/O redirection basic explanation", in daily operation, I believe many people have doubts on Linux I/O redirection basic explanation, Xiaobian consulted all kinds of information, sorted out simple and easy to use operation methods, hope to answer "Linux I/O redirection basic explanation" doubts helpful! Next, please follow the small series to learn together!

All the commands we run basically produce two kinds of output:

Command results-data generated by the program, and

Program status and error messages are used to inform users of program execution details.

On Linux and other Unix-like systems, there are three default files (named as follows), which are also identified by the shell using file descriptors:

stdin or 0 -It connects to the keyboard, and most programs read input from this file.

stdout or 1 -it connects the screen and all programs send their results to this file

stderr or 2 -The program sends status/error messages to this file, which is also connected to the screen.

Therefore, I/O redirection allows you to change the input source of commands and send output and error messages elsewhere. this can be done by

< 和 >

Redirect operator to implement.

How to redirect standard output to files in Linux

As shown in the following example, you can redirect standard output, where we store the output of the top command for later inspection:

$ top -bn 5 >top.log

Meaning of the logo:

-b -Let top run in batch mode so that you can redirect its output to a file or another command.

-n -Specifies the number of iterations before the command terminates.

You can use the cat command to view the contents of the top.log file:

$ cat top.log

To append command output to a file, use the>> operator.

For example, to append the output of the top command to the top.log file above, especially in a script (or command line), enter the following line:

$ top -bn 5 >>top.log

Note: File descriptor numbers can also be used, and the redirection command above is equivalent to:

$ top -bn 5 1>top.log

How to redirect standard errors to files in Linux

To redirect the standard error of a command, you need to specify file descriptor 2 explicitly so that the shell knows what you are trying to do.

For example, the following ls command will generate an error when executed by a normal system user without root privileges:

$ ls -l /root/

You can redirect standard errors into files:

$ ls -l /root/ 2>ls-error.log$ cat ls-error.log

Redirect standard errors to files

To append standard error to a file, use the following command:

$ ls -l /root/ 2>>ls-error.log

How to redirect standard output and standard error into a file

You can also capture all the output of a command, including standard output and standard error, into a single file. This can be done in two possible ways by specifying file descriptors:

1. *** is a relatively old method that works as follows:

$ ls -l /root/ >ls-error.log 2>&1

The above command means that the shell first sends the output of the ls command to the file ls-error.log (using> ls-error.log), and then redirects all error messages written to file descriptor 2(standard error) to the file ls-error.log (using 2>&1). (LCTT The original text is incorrect, and the original text is corrected.) This means that standard errors are also sent to the same file as standard output.

The second and more direct method is:

$ ls -l /root/ &>ls-error.log

You can also append standard output and standard error to a single file like this:

$ ls -l /root/ &>>ls-error.log

How to redirect standard input to a file

Most, if not all, commands get their input from standard input, and standard input is connected to the keyboard by default.

To redirect standard input from a file other than the keyboard, use the

$ cat

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