Linux data stream redirection

In our daily Linux command operations, the input of command execution is generally keyboard input, and the output is generally output to the screen, but sometimes, we may read the file but use the data as input, or save the output to a file (such as log file), data stream redirection is required at this time.

The so-called data stream redirection is: 1) Transfer the data that should appear on the screen after a command is executed to other places, such as files or devices such as printers; 2) Change the data originally input from the keyboard to file content instead


Standard input (stdin): file descriptor 0, usually the keyboard input. Use the symbol < or <<
standard output (stdout): file descriptor 1, usually refers to the correct information returned by the command execution, default output to the screen u. Use the symbol > or >>
standard error (stderr): file descriptor 2, usually refers to the information returned after the command execution fails, which is also output to the screen by default. Try the symbol 2> or 2>>

By default > and >> represent 1> or 1>> respectively, and < and << are equivalent to 0< and 0<<

When do you need to redirect:

  1) When the information output on the screen is very important, and we need to save it.

  2) When the program is executing in the background, you don't want it to interfere with the normal output of the screen.

  3) Some routine commands of the system. (for example, a file written in /etc/crontab), I hope it can be saved.

  4) Some possible known error messages when executing the command, want to throw it away with "2>dev/null"

  5) When error information and correct information need to be output separately

1. Standard output redirection

#ls

Indicates to list the current directory entry and output the result to the screen.

#ls 1>file1

The above command will redirect the standard output of the command to a file file instead of displaying it on the screen. If the file identifier is not specified, the system defaults to 1, so 1 can be omitted.

i.e. this command is equivalent to

#ls >file1

If file1 does not exist, the system will automatically create it. If it already exists, the system will first empty the file, and then write the data to the file. That is to say > output to an existing file, then this file will be overwritten. If you don't want to overwrite, you can use >>. Indicates that the redirected data is appended to the end of the file1 file.

2. Standard error redirection

#ls -qw 2>errorfile

Indicates that the error information is not output to the screen, but written to errorfile. Note that 2 cannot be omitted here. Because > is equivalent to 1>, the default is standard output redirection. So here it is written as 2>, which means standard error redirection. -qw is used to generate error messages.

3. Redirect stderr and stdout to different files respectively

#find /home -name .bashrc > list_right 2>list_error

At this time, no information will be displayed on the screen, because after executing this command, the error information of the lines with permission will be output to the list_error file, and the correct information will be output to the list_right file.

4, stderr, stdout write to the same file

#./a.out &>outfile

This command redirects the standard output and standard error of ./a.out to outfile. & here means standard error and standard output. Here is another way of writing:

#./a.out > outfile  2>&1

Here is an example of an error:

#./a.out > outfile 2>outfile //The reason for the error is because when this command is executed, two pieces of data may be cross-written to cause disorder, while the other two writing methods can avoid this problem

5. Shield standard output or standard error

Sometimes we can block standard output or standard error when we don't need to display or store correct or incorrect information:

./a.out > /dev/null #equivalent to ./a.out 1>dev/null means to block standard output

./a.out  2>/dev/null     #表示屏蔽标准错误

./a.out > /dev/null 2>/dev/null    #表示同时屏蔽标准输出和标准错误

6、标准输入重定向

cat > catfile < ~/.bashrc

创建新文件catfile并用~/.bashrc中的内容作为catfile文件的内容

cat > catfile <<"eof" 

在这里,则是将<<右侧的字符“eof”作为控制字符,用于终止输入,而不用键入ctrl+D来终止输入

7.使用tee命令同时重定向到多个文件

经常你可能还有这样的需求,除了将需要将输出重定向到文件之外也需要将信息打印在终端,那么你可以使用tee命令来实现:

$ echo 'hello shiyanlou' | tee hello

8、使用双向重定向命令(tee)将数据流同时输出到文件与屏幕 
这里写图片描述 
示例:last | tee test.list|cut -d’ ’ -f1 
若加了 -a 则内容会加在文件之后,若无,则会重写该文件。

9、永久重定向

你应该可以看出我们前面的重定向操作都只是临时性的,即只对当前命令有效,那如何做到“永久”有效呢,比如在一个脚本中,你需要某一部分的命令的输出全部进行重定向,难道要让你在每个命令上面加上临时重定向的操作嘛,当然不需要,我们可以使用exec命令实现“永久”重定向。exec命令的作用是使用指定的命令替换当前的 Shell,及使用一个进程替换当前进程,或者指定新的重定向:

# 先开启一个子 Shell
$ zsh
# 使用exec替换当前进程的重定向,将标准输出重定向到一个文件
$ exec 1>somefile
# 后面你执行的命令的输出都将被重定向到文件中,直到你退出当前子shell,或取消exec的重定向(后面将告诉你怎么做)
$ ls
$ exit
$ cat somefile


参考:

《鸟哥的私房菜》

https://www.cnblogs.com/vincently/p/4641098.html

https://blog.csdn.net/jijerry/article/details/53535123





Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325933171&siteId=291194637