Linux redirect output to null

Shell Redirect Output And Errors To The Null Device In Bash

How to redirect output and errors to /dev/null in Bash

Overview

In shell scripting, you often want to avoid printing error messages to the console or discard any possible output the script could generate. Capturing program output can be done with I/O Redirection in Linux.

Concepts

In Linux, everything is a file, even data streams and devices are treated like ordinary files.

There are always three default files open:

stdin the keyboard stdout the screen stderr error messages output to the screen

Output from these files can be captured and sent as input to another file, command, program or script. This is called redirection.

How do you choose what file to handle? With file descriptors.

File descriptors

A file descriptor is simply a number that the operating system assigns to an open file to keep track of it. Consider it a simplified type of file pointer. It is analogous to a file handle in C.

Kowing the file descriptors of each file, we can select with which one we want to work and process redirection between them.

The file descriptors for our open files are:

Then we can use M>N in each command to redirect the file descriptor M to N:

  • M: file descriptor, which defaults to 1, if not explicitly set.

N can be:

  • N: filename,
  • &N: another file descriptor

Null device

Linux has a special device file that discards all data written to it. In Linux this device is /dev/null.

Redirect to the null device.

The way to discard commands output and error messages is to redirect them to the null device.

This can be done explicitely redirecting both output and/or errors in many ways:

Redirect stdout to the null device:

Redirect stderr to the null device

Redirect both stdout and stderr to the null device

Redirect stderr to the same file of stdout

Examples

Let’s see a simple example with the ls command:

stdout file descriptor isn’t present so it defaults to 1, any error would be echoed to the screen.

we only redirect stdout but we still see the error generated by the command.

we produce an error with ls but redirects the error output to the null device, so we don’t see any output in the screen.

we avoid outputting errors and the standard command output.

Summary

Understanding file descriptors is key to handle commands output and errors.

This is a common practice to avoid outputting anything in cronjobs where the output would be sent to the users email.

Читайте также:  You have new mail linux

Additionally, it is very useful to work with bash redirection not only to the null device.

Источник

Unix and Linux: Redirect Error Output To null Command

  1. stdin – 0 – Standard Input (usually keyboard or file)
  2. stdout – 1 – Standard Output (usually screen)
  3. stderr – 2 – Standard Error (usually screen)

[donotprint]

Tutorial details
Difficulty level Easy
Root privileges No
Requirements None
Est. reading time 1m

[/donotprint]

What is a null (/dev/null) file in a Linux or Unix-like systems?

/dev/null is nothing but a special file that discards all data written to it. The length of the null device is always zero. In this example, first, send output of date command to the screen and later to the /dev/null i.e. discards date command output:

Syntax: Standard Error (stderr -2 no) to a file or /dev/null

The syntax is as follows:

In this example, send output of find command to /dev/null:
$ find /etc -type f -name ‘*’ 2>/dev/null
The following example will cause the stderr ouput of a program to be written to a file called errors.txt:
$ find /etc/ -type f -name «*» 2> errors.txt

  • No ads and tracking
  • In-depth guides for developers and sysadmins at Opensourceflare✨
  • Join my Patreon to support independent content creators and start reading latest guides:
    • How to set up Redis sentinel cluster on Ubuntu or Debian Linux
    • How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
    • How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
    • A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
    • How to protect Linux against rogue USB devices using USBGuard

Join Patreon

Linux and Unix redirect all output and error to file

If you want both stderr and stdout in same file, try:

Use cat command to display log.txt on screen:
cat log.txt

See man pages for more information – ksh(1).

🐧 Get the latest tutorials on Linux, Open Source & DevOps via

Category List of Unix and Linux commands
Documentation help • mandb • man • pinfo
Disk space analyzers df • duf • ncdu • pydf
File Management cat • cp • less • mkdir • more • tree
Firewall Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04
Linux Desktop Apps Skype • Spotify • VLC 3
Modern utilities bat • exa
Network Utilities NetHogs • dig • host • ip • nmap
OpenVPN CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04
Package Manager apk • apt
Processes Management bg • chroot • cron • disown • fg • glances • gtop • jobs • killall • kill • pidof • pstree • pwdx • time • vtop
Searching ag • grep • whereis • which
Shell builtins compgen • echo • printf
Text processing cut • rev
User Information groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w
WireGuard VPN Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04

Comments on this entry are closed.

Your command will send output to both screen and file.

Источник

BASH Shell Redirect Output and Errors To /dev/null

H ow do I redirect output and errors to /dev/null under bash / sh shell scripting? How do I redirect the output of stderr to stdout, and then redirect this combined output to /dev/null device? In Unix, how do I redirect error messages to /dev/null?

You can send output to /dev/null, by using command >/dev/null syntax. However, this will not work when command will use the standard error (FD # 2).

Tutorial details
Difficulty level Easy
Root privileges No
Requirements bash/ksh
Est. reading time 1m

So you need to modify >/dev/null as follows to redirect both output and errors to /dev/null.

Syntax to redirect error and output messages to /dev/null

The syntax discussed below works with Bourne-like shells, such as sh, ksh, and bash:

You can also use the same syntax for all your cronjobs to avoid emails and output / error messages:
@hourly /scripts/backup/nas.backup >/dev/null 2>&1
OR
@hourly /scripts/backup/nas.backup &>/dev/null

  • No ads and tracking
  • In-depth guides for developers and sysadmins at Opensourceflare✨
  • Join my Patreon to support independent content creators and start reading latest guides:
    • How to set up Redis sentinel cluster on Ubuntu or Debian Linux
    • How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
    • How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
    • A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
    • How to protect Linux against rogue USB devices using USBGuard

Join Patreon

Redirect both standard error and standard out messages to a log file

You can always redirect both standard error (stdin) and standard out (stdout) text to an output file or a log file by typing the following command:

Want to close stdout and stderr for the command being executed on a Linux/Unix/BSD/OSX bash shell?

Try the following syntax:

See man pages: ksh(1)

🐧 Get the latest tutorials on Linux, Open Source & DevOps via

Category List of Unix and Linux commands
Documentation help • mandb • man • pinfo
Disk space analyzers df • duf • ncdu • pydf
File Management cat • cp • less • mkdir • more • tree
Firewall Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04
Linux Desktop Apps Skype • Spotify • VLC 3
Modern utilities bat • exa
Network Utilities NetHogs • dig • host • ip • nmap
OpenVPN CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04
Package Manager apk • apt
Processes Management bg • chroot • cron • disown • fg • glances • gtop • jobs • killall • kill • pidof • pstree • pwdx • time • vtop
Searching ag • grep • whereis • which
Shell builtins compgen • echo • printf
Text processing cut • rev
User Information groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w
WireGuard VPN Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04

Comments on this entry are closed.

Another way to do it:

Or you can close stdout and stderr for the command being executed:

Remember to add an additional & at the end of the statement to run the command in the background. Thank you Giuseppe for the tip.

What do you mean by “close stdout and stderr” ?

Thanks! I was searching how resolve this problem, and your solution work perfect for me!

need a command in my bash script to remove some (not all) of the contents of directory2.
The script does NOT run as root, which works because it removes the correct files but not the root-level stuff in directory2 (that I don’t want to remove).
Problem is users get confused by the “permission denied” msgs output by the “rm”. So…
I tried to redirect the stderror & stdout to /dev/null this way:
rm * /directory1/directory2/ > 2&>1 /dev/null
kept changing /dev/null form a special file & other users need crw-rw-rw-
Will the recommended approaches allow me to redirect to /dev/null without messing it up for others?

You could use find instead to filter out the files you don’t want to delete, or only delete files matching a patter:

Delete all files except those with “attachments” in the name:
# find . ! -name ‘*attachments*’ -exec rm -v <> \;

Delete all files with “attachments” in the name:
# find . -name ‘*attachments*’ -exec rm -v <> \;

Find is very versitile, it’s pretty cool what you can acheive with find.

how does one redirect output from text file processing to a script file that uses the command line variable $1.

file iplist has a long list of IP’s on the network and i need to send this to a script that creates a file with the ping info.

script says: ping $1 > $1
Please assist if possible

How reliable, if that’s the word I’m looking for, is ending a particular command in a script with a redirect like “2>/dev/null” ? What have folks’ experiences been with the different commands and bash/sh versions when trying it this way?

I know it’s not recommended, but for someone like myself, with scripts they either run daily or don’t run for months and then go through a spate of executing them two and three times a day (only to go back to seldom running them until the next time it happens), it would be very convenient and not too too anxiety-producing to run a script and know that whatever passable or critical errors it comes up with are being suppressed.

I’m much more inclined to put up with circumstances after the fact, and I seldom write anything that’s too destructive (on the system or OS/hardware install and performance level, at any rate) for a little error like Exiv2 complaining about some JPG file’s Photoshop IFD entry being out of bounds.

Источник

Читайте также:  Как удалить гугл хром линукс
Оцените статью