Detailed explanation of Linux redirection usage

Detailed explanation of Linux redirection usage

I believe that everyone needs to copy and paste data sometimes. If you open a file to copy and paste, it will inevitably require more mouse and keyboard operations, which will be cumbersome. So is there a way to copy and paste without these tedious operations?

The answer is yes, it is redirection. Redirection is an efficient method that can complete data transfer without a lot of mouse and keyboard operations. Redirection can be divided into two types: input redirection and output redirection. Since all programs have input or output, redirection of input and output is a native function of any programming or scripting language.

Whenever you interact with a computer, redirects are bound to occur. Learning to use redirection not only allows you to interact better with your computer, but also improves your work efficiency. Therefore, please let Liang Xu explain to you the common usage of redirection in Linux system:

Data Flow in Linux

When talking about Linux redirection, we have to mention the following three data streams:

  • Input is read from stdin (standard input, usually the keyboard or mouse).
  • Output is sent to stdout (standard output, a text file or data stream).
  • Error messages are output to stderr.

Knowing the existence of these data flows, you can better control the flow of data when you use Shell.

In Linux systems, standard input, standard output, and standard error all exist as files. You can see them in the /dev directory:

$ ls /dev/std* 
/dev/stderr /dev/stdin /dev/stdout

Redirecting Output

In Linux systems, use the > character to redirect output. For example, to redirect the output of the ls command to a file:

$ ls > list.txt

After executing the above command, the output information of the ls command will not be displayed on the screen because the output information has been redirected to the list.txt file.

In addition, redirection has many uses. It can also be used to copy the contents of files. It is not limited to copying text files, but binary files can also be copied:

$ cat image.png > picture.png

If you want to copy the contents of one file to the end of another file, you can just replace the > character with the >> string, like this:

$ cat lxlinux >> alvi

Redirecting Input

In contrast to redirecting output, redirecting input uses the < character.

Input redirection can redirect input information to the command as a parameter. This feature may be rarely used, but when a command requires a list of parameters, and these parameters are all in a file, and you want to quickly copy and paste them from the file into the terminal, this feature can come in handy.

For example, package.list contains a list of packages you need to install, and if you want to quickly install all packages, you only need to execute the following command to install all packages in package.list at once:

$ sudo dnf install $(<package.list)

Common uses of input redirection are Here-document (Here-doc for short) and Here-string.

Here-doc redirects a block of input text to the standard input stream until a special end-of-file marker is encountered (the end-of-file marker can be any unique string, but most people use EOF by default).

You can try entering the following command in the terminal (until the second EOF string ends):

$ cat << EOF 
> alvin 
> lxlinux.net 
> EOF

The expected output should be something like this:

alvin
lxlinux.net

Here-doc is a common trick used by Bash scripters to dump multiple lines of text to a file or the screen.

Here-string is similar to here-doc, but it only takes one string, or several strings enclosed in quotes:

$ cat <<< alvin 
alvin 
$ cat <<< "alvin lxlinux.net" 
alvin lxlinux.net

Redirecting Error Messages

Error messages go to a stream called stderr by default, which can be redirected using 2>. For example, to redirect error messages to a file called output.log:

$ ls /nope 2> output.log

Redirect data to /dev/null

Just like standard input, standard output, and standard error, in the Linux file system, there is also a file corresponding to it, it is called null and is placed in the /dev directory. For ease of reading, people often omit the slash and read it directly as dev null.

/dev/null does not store data, and data written to /dev/null will eventually be lost, just like being thrown into the void. Therefore, you can use redirection to pipe unwanted data to /dev/null. For example, the output of the find command is often very verbose and often reports permission conflicts when searching for files, like this:

$ find ~ -type f 
/home/seth/actual.file 
find: `/home/seth/foggy': Permission denied 
find: `/home/seth/groggy': Permission denied 
find: `/home/seth/soggy': Permission denied 
/home/seth/zzz.file

At this time, you can redirect the error information to /dev/null to filter out unnecessary information, like this:

$ find ~ -type f 2> /dev/null 
/home/seth/actual.file 
/home/seth/zzz.file

Make good use of redirects

Redirection is an efficient way to move data in Bash. You may not always use redirection, but knowing how to use it when you need it can save you a lot of unnecessary copying and pasting operations, thereby saving a lot of time operating the mouse and keyboard. Please don't be obsessed with copying and pasting. Using redirection can improve your work efficiency. Isn't it great?

This is the end of this article on the detailed usage of Linux redirection. For more relevant content on the usage of Linux redirection, please search for previous articles on 123WORDPRESS.COM or continue to browse the related articles below. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Detailed instructions for using Linux input and output redirection
  • Linux shell pipe command usage and difference from shell redirection
  • Detailed analysis of linux shell data redirection (input redirection and output redirection)
  • Detailed explanation of Linux base shell redirection
  • Detailed explanation of Linux input and output redirection
  • Analysis of 301 redirection code for linux URL

<<:  Use js to write a simple snake game

>>:  js to implement a simple bullet screen system

Recommend

A brief discussion on the specific use of viewport in mobile terminals

Table of contents 1. Basic Concepts 1.1 Two kinds...

Native js encapsulation seamless carousel function

Native js encapsulated seamless carousel plug-in,...

Getting started with JavaScript basics

Table of contents 1. Where to write JavaScript 2....

How to install and configure SSH service in Ubuntu 18.04

Install ssh tool 1. Open the terminal and type th...

Introduction to reactive function toRef function ref function in Vue3

Table of contents Reactive Function usage: toRef ...

Example of horizontal arrangement of li tags in HTMl

Most navigation bars are arranged horizontally as...

A brief discussion on VUE uni-app custom components

1. Parent components can pass data to child compo...

Detailed installation and use of RocketMQ in Docker

To search for RocketMQ images, you can search on ...

Two ways to install the Linux subsystem in Windows 10 (with pictures and text)

Windows 10 now supports Linux subsystem, saying g...

Clean XHTML syntax

Writing XHTML demands a clean HTML syntax. Writing...

Detailed explanation of linux nslookup command usage

[Who is nslookup?] 】 The nslookup command is a ve...

CocosCreator learning modular script

Cocos Creator modular script Cocos Creator allows...