In-depth introduction to shell command output redirection

Source: Internet
Author: User

When learning shell commands, you may encounter the shell output redirection problem. Here we will introduce the solution to the shell command output redirection problem. Here we will share it with you. Most shell commands specify the input file name on the command line in the form of parameters, So input redirection is not often used. However, if you want to use a shell command that does not accept the file name as the input parameter, and the required input content is in another file, you can use the input redirection to solve the problem.

Shell output redirection

Output redirection refers to redirecting the standard output or standard error output of shell commands or executable programs to a specified file. In this way, the output of this command is not displayed on the screen, but written to the specified file.

Output redirection is more commonly used than input redirection, which can be used in many cases. For example, if a command has a lot of output and cannot be fully displayed on the screen, redirect the output to a file and open the file with a text editor to view the output information; you can use this method to save the output of a shell command. Also, the output redirection can be used to treat the output of a command as the input of another command. Another simpler method is to use pipelines, which will be described below ).

The general format of output redirection is: Command> file name. For example:
$ Ls> directory. out
$ Cat directory. out
Ch1.doc ch2.doc ch3.doc chimp config mail/test/

Save the ls command output as a file named directory. out.
Note: If the file behind the symbol already exists, the file will be overwritten.
To prevent the specified file in the output redirection from storing only the output redirection content of the current command, shell provides an append Method for output redirection. The output append redirection function is very similar to the output redirection function. The difference is that the output result of the shell command or executable program is appended to the end of the specified file, the original content of the file is not damaged.

If you want to append the output result of a shell command to the end of a specified file, you can use the append redirection operator>. Format: Command> file name. Example: $ ls *. doc> directory. out
$ Cat directory. out
Ch1.doc ch2.doc ch3.doc chimp config mail/test/
Ch1.doc ch2.doc ch3.doc

Like the standard output redirection of a program, the program's error output can also be reoriented. Use symbol 2> or append symbol 2>) to redirect the error output device. For example, the following shell command: $ ls/usr/tmp 2> err. file
You can see the normal output results of the program on the screen, but send any program error information to the file err. file for future checks.
You can also use another output redirection operator &> to send both standard output and error output to the same file. Example: $ ls/usr/tmp &> output. file
By combining commands with redirection, You can implement new functions that a single system command cannot provide. For example, use the following shell command sequence:
$ Ls/usr/bin>/tmp/dir
$ Wc-w </tmp/dir 459 counts the number of files in the/usr/bin directory.

  1. Brief Introduction to the Collection programming language of Linux shell commands
  2. Linux script Shell command
  3. Prerequisites for getting started with shell commands
  4. Windows 7 Powershell commands
  5. Shell Command for viewing file calls in Linux

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.