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 use Virtual device File in Linux system

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces how to use the virtual device file in the Linux system, which has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, let the editor take you to understand it.

Under Linux, everything is a file, and the same is true for device files. We often see this thing / dev/null in the process of work, so what on earth is it?

Technically, / dev/null is a virtual device file. For programs, these virtual device files are treated as real files. The program can request data from this data source, and the resulting data will be provided by the operating system. However, these data are not read from the disk, but are dynamically generated by the operating system. A typical example of a virtual device file is / dev/zero.

However, when you want to write data to / dev/null, no matter what data you write to / dev/null, it will eventually be discarded, just like being thrown into a black hole.

In that case, what is the use of / dev/null? To understand this problem, you must first have a basic understanding of the standard output and standard error output of Linux systems or Unix and Unix-like systems.

Standard output file and standard error output file

A command line program can generate two types of output: standard output and standard error output. The standard output is recorded to the standard output file stdout, while the standard error output is recorded to the standard error output file stderr.

By default, both standard output files and standard error output files are associated with your terminal window (or console). This means that any information sent to a standard output file or standard error output file will be displayed on your screen.

However, through redirection in Shell, you can change this behavior. For example, you can redirect standard output to a file. In this way, the standard output will not be displayed on the screen, it will be saved to that file for later reference, or you can redirect the standard output to another physical device, such as a digital LED or LCD monitor.

Some common redirects are as follows:

Use 2 > you can redirect standard error output information. For example: 2 > / dev/null or 2 > / home/user/error.log.

Using 1 > you can redirect standard output information.

Using & > you can redirect both standard error output and standard output information.

Use / dev/null to clear unnecessary output

Because there are two types of output: standard output and standard error output, the first use of / dev/null is to filter out one of these types of output. The use of the above can be more easily understood through the following practical examples.

If you want to find a file about power settings by looking for a file with a power string in the file name under the / sys folder, you can write the command:

$grep-r power / sys/

Because there are many files under the / sys folder that are not accessible to non-root users, this will cause a lot of Permission denied errors and be output to the screen.

These error messages will make the screen cluttered and some important information will be washed away. Since Permission denied error messages are part of the standard error output, you can redirect them to / dev/null.

$grep-r power / sys/ 2 > / dev/null

In this way, the information displayed is much cleaner and refreshing.

Sometimes, we may not want to see the content of standard output, but rather want to see the content of standard error, so we can redirect the standard output to / dev/null.

$ping baidu.com 1 > / dev/null

The screenshot above shows that without redirected output, the ping instruction shows its normal output when the packet can reach the target host. In the first command, the screen shows nothing when the network is connected, but once it is disconnected from the target machine, the screen displays only an error message. Of course, you can also redirect standard output and standard error output to two different files.

$ping baidu.com 1 > / dev/null 2 > error.log

In this example, the standard output information will not be displayed, and the error message will be saved to a file named error.log.

Redirect all output to / dev/null

Sometimes, we may not want to look at any output, so there are two ways to do this.

$grep-r power / sys/ > / dev/null 2 > & 1

The string > / dev/null redirects the standard output to / dev/null, and then the second part, 2 > & 1, redirects the standard error to the standard output file. Here you must write the standard output file as & 1, not simply as 1. Writing as 2 > 1 will only redirect the standard output to a file named 1.

The important thing to note here is that order is also important. If you change the position of the redirected parameters to the following:

$grep-r power / sys/ 2 > & 1 > / dev/null

It doesn't work the way you expect it to. That's because once 2 > & 1 is interpreted by the interpreter, the standard error output is redirected to the default standard output file stdout and displayed on the screen. Next, when standard output is redirected to / dev/null, the standard output information is cleared. The end result is that you will see an error message displayed on the screen. If you forget the correct order, there is a simpler one:

$grep-r power / sys/ & > / dev/null

In this example, & > / dev/null is equivalent to redirecting standard output and standard error output to / dev/null.

Other utility examples for redirecting output to / dev/null

If you want to know how fast your disk can read sequence data, you can use the dd command to do the test. But the dd instruction either outputs to a standard output file or specifies output to a file. To eliminate the effect of writing data on the result, we use the parameter of=/dev/null so that the dd output can be written to the virtual file instead of to the real disk. You don't even need to use Shell redirection to do this here.

For the following command, the parameter if= specifies the file name used for input, and the parameter of= specifies the file name used for output, that is, where the output is written.

The following tests are not very accurate, but they are telling enough.

$dd if=debian-disk.qcow2 of=/dev/null status=progress bs=1M iflag=direct

The above is a practical scenario. Let's introduce another scene.

In some cases, you may wonder how fast you can download something from a server. But you don't want to write anything unnecessary to disk, so at this point, you can write to / dev/null.

$wget-O / dev/null http://ftp.halifax.rwth-aachen.de/ubuntu Thank you for reading this article carefully. I hope the article "how to use Virtual device Files in Linux system" shared by the editor will be helpful to you. At the same time, I hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

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

Development

Wechat

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

12
Report