- BASH Shell Redirect Output and Errors To /dev/null
- Syntax to redirect error and output messages to /dev/null
- Redirect both standard error and standard out messages to a log file
- Want to close stdout and stderr for the command being executed on a Linux/Unix/BSD/OSX bash shell?
- How can I redirect all output to /dev/null?
- 3 Answers 3
- Unix and Linux: Redirect Error Output To null Command
- What is a null (/dev/null) file in a Linux or Unix-like systems?
- Syntax: Standard Error (stderr -2 no) to a file or /dev/null
- Linux and Unix redirect all output and error to 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.
Источник
How can I redirect all output to /dev/null?
I want to run a program ( google-chrome ) in the background, but prevent it from outputting any messages to the terminal.
I tried doing this:
However, the terminal still fills up without messages like:
[5746:5746:0802/100534:ERROR:object_proxy.cc(532)] Failed to call method: org.chromium.Mtpd.EnumerateStorag.
What am I doing wrong? How do I redirect all the output to /dev/null ?
3 Answers 3
Redirection operators are evaluated left-to-right. You wrongly put 2>&1 first, which points 2 to the same place, as 1 currently is pointed to which is the local terminal screen, because you have not redirected 1 yet. You need to do either of the following:
The placement of the redirect operators in relation to the command does not matter. You can put them before or after the command.
In the section Redirection, Bash’s reference manual says:
The operator [n]>&word is used [. ] to duplicate output file descriptors
To redirect both standard error and standard output to file you should use the form
With regard to your case, that means substitute
Источник
Unix and Linux: Redirect Error Output To null Command
- stdin – 0 – Standard Input (usually keyboard or file)
- stdout – 1 – Standard Output (usually screen)
- 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.
Источник