Linux output to dev null

How to Redirect Output to /dev/null in Linux

In Linux, programs are very commonly accessed using the command line and the output, as such, is displayed on the terminal screen. The output consists of two parts: STDOUT (Standard Output), which contains information logs and success messages, and STDERR (Standard Error), which contains error messages.

Many times, the output contains a lot of information that is not relevant, and which unnecessarily utilizes system resources. In the case of complex automation scripts especially, where there are a lot of programs being run one after the other, the displayed log is huge.

In such cases, we can make use of the pseudo-device file ‘/dev/null‘ to redirect the output. When the output is redirected to /dev/null, it is immediately discarded, and thus, no resource utilization takes place.

Let’s see how to redirect output to /dev/null in Linux.

Let us take an example of the mv command trying to move two files to a particular directory, where one file is moved successfully (displays STDOUT) and the other gives an error while moving (displays STDERR).

Move Multiple Files in Linux

Note: The ‘-v’ argument to mv displays the success log when a file is moved. Without the flag, nothing is printed to STDOUT.

Now, to redirect only the STDOUT to /dev/null, i.e. to discard the standard output, use the following:

Redirect STDOUT to /dev/null

What does ‘1>/dev/null’ mean?

Here ‘1’ indicates STDOUT, which is a standard integer assigned by Linux for the same. The operator ‘>’ , called a redirection operator, writes data to a file. The file specified in this case is /dev/null. Hence, the data is ultimately discarded.

If any other file were given instead of /dev/null, the standard output would have been written to that file.

Similarly, to redirect only the STDERR to /dev/null, use the integer ‘2’ instead of ‘1’ . The integer ‘2’ stands for standard error.

Redirect STDERR Output to /dev/null

As you can see, the standard error is not displayed on the terminal now as it is discarded in /dev/null.

Finally, to discard both STDOUT and STDERR at the same time, use the following:

Redirect STDOUT and STDERR output to /dev/null

The ampersand character (‘&’) denotes that both the standard output and standard error has to be redirected to /dev/null and discarded. Hence, in the last screenshot, we do not see any output printed on the screen at all.

Conclusion

In this article, we have seen how to redirect output to /dev/null. The same can be used in either command or in complex Bash scripts which generate a heavy amount of log. In case if the script needs debugging, or has to be tested; in such cases, the log is required to view and analyze the errors properly.

However in cases when the output is already known and anticipated, it can be discarded with /dev/null. If you have any questions or feedback, let us know in the comments below!

Источник

What Is /dev/null in Linux?

In technical terms, “/dev/null” is a virtual device file. As far as programs are concerned, these are treated just like real files. Utilities can request data from this kind of source, and the operating system feeds them data. But, instead of reading from disk, the operating system generates this data dynamically. An example of such a file is “/dev/zero.”

Читайте также:  Как самим заправлять тонер картридж

In this case, however, you will write to a device file. Whatever you write to “/dev/null” is discarded, forgotten, thrown into the void. To understand why this is useful, you must first have a basic understanding of standard output and standard error in Linux or *nix type operating systems.

stdout and stder

A command-line utility can generate two types of output. Standard output is sent to stdout. Errors are sent to stderr.

By default, stdout and stderr are associated with your terminal window (or console). This means that anything sent to stdout and stderr is normally displayed on your screen. But through shell redirections, you can change this behavior. For example, you can redirect stdout to a file. This way, instead of displaying output on the screen, it will be saved to a file for you to read later – or you can redirect stdout to a physical device, say, a digital LED or LCD display.

A full article about pipes and redirections is available if you want to learn more.

  • With 2> you redirect standard error messages. Example: 2>/dev/null or 2>/home/user/error.log .
  • With 1> you redirect standard output.
  • With &> you redirect both standard error and standard output.

Use /dev/null to Get Rid of Output You Don’t Need

Since there are two types of output, standard output and standard error, the first use case is to filter out one type or the other. It’s easier to understand through a practical example. Let’s say you’re looking for a string in “/sys” to find files that refer to power settings.

There will be a lot of files that a regular, non-root user cannot read. This will result in many “Permission denied” errors.

These clutter the output and make it harder to spot the results that you’re looking for. Since “Permission denied” errors are part of stderr, you can redirect them to “/dev/null.”

As you can see, this is much easier to read.

In other cases, it might be useful to do the reverse: filter out standard output so you can only see errors.

The screenshot above shows that, without redirecting, ping displays its normal output when it can reach the destination machine. In the second command, nothing is displayed while the network is online, but as soon as it gets disconnected, only error messages are displayed.

You can redirect both stdout and stderr to two different locations.

In this case, stdout messages won’t be displayed at all, and error messages will be saved to the “error.log” file.

Redirect All Output to /dev/null

Sometimes it’s useful to get rid of all output. There are two ways to do this.

The string >/dev/null means “send stdout to /dev/null,” and the second part, 2>&1 , means send stderr to stdout. In this case you have to refer to stdout as “&1” instead of simply “1.” Writing “2>1” would just redirect stdout to a file named “1.”

What’s important to note here is that the order is important. If you reverse the redirect parameters like this:

it won’t work as intended. That’s because as soon as 2>&1 is interpreted, stderr is sent to stdout and displayed on screen. Next, stdout is supressed when sent to “/dev/null.” The final result is that you will see errors on the screen instead of suppressing all output. If you can’t remember the correct order, there’s a simpler redirect that is much easier to type:

In this case, &>/dev/null is equivalent to saying “redirect both stdout and stderr to this location.”

Читайте также:  Kali linux usb install linux

Other Examples Where It Can Be Useful to Redirect to /dev/null

Say you want to see how fast your disk can read sequential data. The test is not extremely accurate but accurate enough. You can use dd for this, but dd either outputs to stdout or can be instructed to write to a file. With of=/dev/null you can tell dd to write to this virtual file. You don’t even have to use shell redirections here. if= specifies the location of the input file to be read; of= specifies the name of the output file, where to write.

In some scenarios, you may want to see how fast you can download from a server. But you don’t want to write to your disk unnecessarily. Simply enough, don’t write to a regular file, write to “/dev/null.”

Conclusion

Hopefully, the examples in this article can inspire you to find your own creative ways to use “/dev/null.”

Know an interesting use-case for this special device file? Leave a comment below and share the knowledge!

Fell in love with computers when he was four years old. 27 years later, the passion is still burning, fueling constant learning. Spends most of his time in terminal windows and SSH sessions, managing Linux desktops and servers.

Our latest tutorials delivered straight to your inbox

Источник

What is ‘> /dev/null 2>&1’ in Linux

/dev/null is a pseudo-device file in Linux, which is used to discard output coming from programs, especially the ones executed on the command line. This file behaves like a sink, i.e. a target file which can be written, however as soon as any stream of data is written to this file, it is immediately deleted.

This is useful to get rid of the output that is not required by the user. Programs and processes can generate output logs of huge length, and it gets messy at times to analyze the log.

For example, sometimes there are two errors being thrown by a program, but the error logs are lost somewhere in between hundreds of information message logs. In such a case we can dump the information message logs to /dev/null.

In the above example, the error output by a program is called STDERR (Standard Error) in Linux and is represented by the integer ‘2’, while the success and information output is called STDOUT (Standard Output), and is represented by the integer ‘1’.

Now, you might have seen the following command line statement very often, which discards both STDERR and STDOUT to /dev/null.

Let us try to understand what this means.

The operator ‘>’ is called a redirection operator. It redirects the output to the file specified after it, which in this case is /dev/null, instead of printing it on the terminal.

Does this mean ALL the output of the command is redirected to /dev/null with the ‘>’ operator? No. When used as shown above, the operator only redirects the STDOUT.

Hence, we have the last part of the command. ‘2>&1’ means: redirect STDERR (2) to STDOUT (1). The reason we have to specify ‘&1’ and not simply ‘1’ is that simply writing ‘1’ will redirect the output to a text file with the name ‘1’. With the ampersand (&) the system recognizes that the 1 is not a filename, but the file descriptor STDOUT.

Thus, we redirect STDOUT to /dev/null and STDERR to STDOUT and in such a way, the complete output of the program(s) is discarded in /dev/null.

For example, executing mv command with and without ‘>/dev/null 2>&1’.

Without redirection:

With redirection of STDOUT:

With redirection of STDOUT and STDERR:

Conclusion

In this article, we tried to understand what is the meaning of the popularly used command phrase in Linux: ‘> /dev/null 2>&1’ . If you want to learn about ‘/dev/null‘ in more depth, you can do so here.

Читайте также:  Epson epl 5500 картридж

If you have any questions or comments, let us know in the comments below!

Источник

What is /dev/null in Linux

The ‘/dev‘ directory in Linux and Unix based systems contains files corresponding to devices attached to the system. For example, as seen in the screenshot below, the CD drive is accessed using ‘cdrom‘, DVD drive with ‘dvd‘, hard drives are accessed using ‘sda1‘, ‘sda2‘, etc.

All these files communicate with the Linux system through the respective files in ‘/dev‘. The input/output processing of the devices takes place through these files. This is due to an important feature of the filesystem in Linux: everything is either a file or a directory.

The /dev directory contains device files

Apart from these files which represent actual physical devices, there are some files in ‘/dev‘ which are known as ‘virtual device files‘ or ‘pseudo-device files‘. These files do not handle any devices and they are used for providing various functions required by the operating system.

For example, the file ‘/dev/random‘ when read, returns a set of random bytes generated by the random number generator.

One such file which is very popularly used is ‘/dev/null‘. Today we’ll try to learn about this file and for what purpose it is present in Linux.

/dev/null: What is it, and Why is it there?

In Linux, you might know that primarily the command line is used to run not only administrative processes or server management processes but also day to day processes. The command line (also called the Terminal) executes a program and prints the output (also known as log).

Two types of output are printed by the terminal:

  1. STDOUT, or ‘Standard Output’ is the output containing general information about the program run and success messages of the program run.
  2. STDERR, or ‘Standard Error‘ is the output containing error messages of the program run.

The larger the process, the larger is the log produced by the process. Many times, most of the log is not even relevant for the user. This even utilizes unnecessary resources of the machine.

In such cases, ‘/dev/null‘ comes in handy. This file is used as a ‘sink‘, i.e. a target file to write the log. Except that as soon as the log is written to this file, it is immediately discarded/deleted by the operating system.

List /dev Files

Highlighted in yellow above is the file ‘null‘ in the directory ‘dev‘ filesystem.

Thus, due to the data being discarded immediately, unnecessary memory consumption is prevented. This is especially useful in the case of complex automation scripts that contain the output of 10s and 20s of programs.

To direct the output of any program to /dev/null, simply use the redirection operator in the Linux shell (‘>’) as follows.

Redirect Output to /dev/null

As you can see, when ‘ls‘ is run normally it displays the contents of the directory as output, however, when it is redirected to /dev/null, the complete output is suppressed.

Conclusion

In this article, we learned about /dev/null and its use in Linux. You can learn more about redirecting output to /dev/null with various combinations of STDOUT and STDERR here.

If you have any questions or feedback, let us know in the comments below!

Источник

Поделиться с друзьями
КомпСовет
Adblock
detector