Jun 01, 2021 Article blog
We usually need to copy sticker data, if you open the file to copy sticking operation, then there is tedious, then can you save these steps?
Of course, redirection is an efficient method that does not require a lot of mouse and keyboard manipulation to transfer data. R edirects can be divided into input redirects and output redirects. Because all programs have inputs or outputs, redirection of inputs and outputs comes with functionality in any programming or scripting language.
Redirection is bound to occur whenever you interact with your computer. L
earning to use redirection not only allows you to interact better with your computer, but also increases your productivity.
Let's take a look at common usage of redirection in
Linux
systems:
When it comes to
Linux
redirection, we have to mention the following three streams:
stdin
(standard input, usually keyboard or mouse).
stdout
(standard output, a text file, or data stream).
stderr
Knowing the existence of these data streams gives you more control over where your data flows when you use
Shell
In
Linux
systems, standard inputs, standard outputs, and standard errors exist as files.
You can see them in the
/dev
directory:
$ ls /dev/std* /dev/stderr /dev/stdin /dev/stdout
In
Linux
system, the redirect output is represented by the > character.
For example, redirect the output of the
ls
command to a file:
$ ls > list.txt
After you execute the above command, the output information for the
ls
command is not displayed on the screen because the output information has been redirected to
list.txt
file.
In addition, redirection has many uses, it can also be used to copy the contents of a file, and not limited to copying text files, 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, simply replace
>
character with
>>
string, like this:
$ cat lxlinux >> alvin
In contrast to redirecting the output, the redirect input uses
<
character.
Input redirection redirects input information to the command for use as an argument. This feature may be less useful, but it comes in handy when the command requires a list of parameters that exist in a file and then you want to quickly copy and paste them from the file to the terminal.
For example,
package.list
records a list of packages that you need to install, and if you want to install all packages quickly, you can install all the packages in
package.list
at once by executing the following command:
$ sudo dnf install $(<package.list)
The common uses for input redirection are
Here-document
and
Here-string
Here-doc
redirects the input block of text to the standard input stream until a special file end tag is encountered (the file end marker can be any unique string, but most people use
EOF
by default).
You can try entering the following command at the terminal (until the end of the second
EOF
string):
$ cat << EOF
alvin lxlinux.net EOF
The expected output should look like this:
alvin lxlinux.net
Here-doc
is a common technique used by
Bash
script writers to dump multiple lines of text onto a file or screen.
Here-string
is similar to
Here-doc
but it has only one string, or a few strings enclosed in quotation marks:
$ cat <<< alvin alvin $ cat <<< "alvin lxlinux.net" alvin lxlinux.net
The error message enters the stream called
stderr
by default and can be redirected using
2>
For example, redirect an error message to a file named
output.log
$ ls /nope 2> output.log
Just like standard inputs, standard outputs, and standard errors, in
Linux
file system, there is an empty file corresponding to it, called
null
which is placed in the
/dev
directory.
For ease of reading, slashes are often omitted and read directly as
dev null
/dev/null
does not save data, and the data written to
/dev/null
is eventually lost as if it were thrown into the void air. T
herefore, you can use redirection to deliver unwanted data to
/dev/null
For example, the output of
find
command is often lengthy, and errors with conflicting permissions are often reported when searching for files, such as 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 point, you can redirect the error message to
/dev/null
to filter out unnecessary information, like this:
$ find ~ -type f 2> /dev/null /home/seth/actual.file /home/seth/zzz.file
In
Bash
redirection is an efficient way to transfer data. Y
ou may not always use redirection, but learning how to use redirection saves you a lot of unnecessary copy-and-paste operations when you need them, and therefore saves you a lot of time working with the mouse and keyboard.
Please don't stick to copy and paste, using redirection can improve your productivity, isn't it?
The above is about
Linux
system redirection explanation, I hope to help you work, want to learn more Linux knowledge students can look at the tutorial:
Linux tutorial: https://www.w3cschool.cn/linux/
Linux Microsyscope: https://www.w3cschool.cn/minicourse/play/linuxcourse
Linux should learn this: https://www.w3cschool.cn/linuxprobe/
Source: www.toutiao.com/a6854146447089074695/