Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to read and write data arbitrarily in Linux terminal by redirection

2025-03-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly shows you "how to read and write data arbitrarily in the Linux terminal through redirection", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn this article "how to read and write data arbitrarily in the Linux terminal through redirection".

Redirection is a very efficient way of data flow, it can help you reduce a lot of mouse and keyboard operations.

Input and output redirection is a natural function for any programming or scripting language. Strictly speaking, when you use a computer, the data is naturally focused. The input is read from stdin (standard input, usually your keyboard or mouse), the input is sent to stdout (standard output, a piece of text or data stream), and the error message is sent to stderr. If you use shell such as Bash or Zsh, understanding these data streams will give you better control over the flow of information.

Standard input, standard output, and standard error output all exist in the Linux file system. You can check it at / dev:

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

You may not be able to use them directly, but it helps to think of them as meta-locations where you can pass data.

The basis for redirection is simple: redirect the output with some > symbols, and then use some other list.txt

You can't see the ls output as usual, because instead of being sent to the screen, it's written to a list.txt file, which is so useful that, in fact, you can even use it to copy the contents of a file to another file. It doesn't have to be a text file, but you can also use redirection for binary data:

$cat image.png > picture.png

(you may wonder why this is done, which is sometimes indirectly useful for file permission information. )

Redirect input

You can also redirect input to a command. It can be said that it is not as useful as redirecting output, because many commands have been hard-coded to receive input only from your parameters. However, if a command requires a series of parameters, and you write them in a file, it helps when you want to quickly "copy and paste" to the terminal (unless you don't want to copy and paste).

$sudo dnf install $(bar > baz > EOF

Output result:

Foobarbaz

Scripters using Bash often use this technique to write lines of text to a file or print them to the screen at once. As long as you don't forget the end tag at the end of the file, this will be a good way to help you avoid a lot of tedious echo or printf statements.

An embedded string is similar to an embedded document, but it contains only one string (or a few strings wrapped in quotation marks, which are also treated as a string).

$cat output.log sends the data to / dev/null

Since standard input, standard output, and error output all have their own place, "empty" should also have a place in the Linux file system. Yes, it's called null and is located at / dev. Frequent users don't bother to say "slash dev slash null", so they simply call it "devnull".

Through redirection, you can send the data to / dev/null. For example, the find command often outputs a lot of specific information and reports everything when a permission conflict is encountered in the search file:

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

The find command treats those as errors, so you can just redirect the error message to / dev/null:

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

In Bash, redirection is an effective way to transfer data. You may not use redirection frequently, but learning how to use it can save you a lot of unnecessary time in opening files, copying and pasting data, such as moving the mouse and pressing a lot of keys. Don't do such a waste of time. Use redirection to enjoy life.

The above is all the contents of the article "how to read and write data arbitrarily in the Linux terminal through redirection". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report