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

Detailed explanation of the usage of setUp and reactive functions in vue3

1. When to execute setUp We all know that vue3 ca...

How to install and deploy ftp image server in linux

Refer to the tutorial on setting up FTP server in...

Teach you step by step to develop a brick-breaking game with vue3

Preface I wrote a few examples using vue3, and I ...

Use of Linux chkconfig command

1. Command Introduction The chkconfig command is ...

Javascript closure usage scenario principle detailed

Table of contents 1. Closure 2. Closure usage sce...

Master-slave synchronous replication configuration of MySQL database under Linux

The advantage of the master-slave synchronization...

What is ssh port forwarding? What's the use?

Table of contents Preface 1. Local port forwardin...

Detailed explanation of the integer data type tinyint in MySQL

Table of contents 1.1Tinyint Type Description 1.2...

A brief analysis of controlled and uncontrolled components in React

Table of contents Uncontrolled components Control...

Detailed explanation of how to create an updateable view in MySQL

This article uses an example to describe how to c...

An article teaches you JS function inheritance

Table of contents 1. Introduction: 2. Prototype c...

Practice of Vue global custom instruction Modal drag

Table of contents background Implementation ideas...

Example code for css flex layout with automatic line wrapping

To create a flex container, simply add a display:...