What does “> /dev/null 2>&1″ mean?
I remember being confused for a very long time about the trailing garbage in commands I saw in Unix systems, especially while watching compilers do their work. Nobody I asked could tell me what the funny greater-thans, ampersands and numbers after the commands meant, and search engines never turned up anything but examples of it being used without explanation. In this article I’ll explain those weird commands.
Here’s an example command:
wibble > /dev/null 2>&1
Output redirection
The greater-thans (>) in commands like these redirect the program’s output somewhere. In this case, something is being redirected into /dev/null, and something is being redirected into &1.
Standard in, out, and error
There are three standard sources of input and output for a program. Standard input usually comes from the keyboard if it’s an interactive program, or from another program if it’s processing the other program’s output. The program usually prints to standard output, and sometimes prints to standard error. These three file descriptors (you can think of them as “data pipes”) are often called STDIN, STDOUT, and STDERR.
Sometimes they’re not named, they’re numbered! The built-in numberings for them are 0, 1, and 2, in that order. By default, if you don’t name or number one explicitly, you’re talking about STDOUT.
Given that context, you can see the command above is redirecting standard output into /dev/null, which is a place you can dump anything you don’t want (often called the bit-bucket), then redirecting standard error into standard output (you have to put an & in front of the destination when you do this).
The short explanation, therefore, is “all output from this command should be shoved into a black hole.” That’s one good way to make a program be really quiet!


All the output, that is, except for error messages, right? So, one would use this structure to only listen to standard error?
Tim McCormack
6 Jun 06 at 11:29 pm
Error messages go to STDERR, which I’ve redirected to STDOUT, which is being shoved into a black hole. The program should not make a peep.
To listen only to STDERR, I can just redirect STDOUT into
/dev/null, like so:wibble > /dev/nullXaprb
7 Jun 06 at 8:26 am
My source for finding this information, since I usually forget how it works, is
man bashand search for the REDIRECTION section.Some of the man page is bash specific, but most is pretty generic.
Paul
10 Jul 06 at 2:44 pm
I’ve seen “2>&1 &” at the end of many tutorials, but I never knew what they were for. Thanks for the info guys.
chrisortiz
20 Jul 06 at 5:03 pm
Ah, I understand now — I misunderstood the nature of redirection, treating it as commands instead of pipes. That clears up a lot.
Tim McCormack
23 Jul 06 at 3:30 pm
So logically that means you could catch STDERR from the command line by appending “2>>/var/log/logfile” to a shell command.
So, where “2” is STDERR, “>>” is to append and “/var/log/logfile” is the where errors will be logged.
David
22 Nov 06 at 7:14 pm
Nice, Short, Sweet. So in short:
“> /dev/null†will redirect STDOUT to a BLACK HOLE
“2 > &1†will redirect STDERR to STDOUT which in turn is redirected to a BLACK HOLE
Right? Thank you for the explanation!
Tim
29 Nov 06 at 11:52 am
Yep, that’s right!
Xaprb
29 Nov 06 at 12:11 pm
Does it mean that
>/dev/null 2>&1is equivalent to
>/dev/null 2>/dev/null?Eric Dodemont
9 Jan 07 at 12:23 pm
Yes, that’s true.
Xaprb
9 Jan 07 at 1:04 pm
Excellent! Well explained.
Bobby Howie
12 Jan 07 at 10:58 am
Excellent Tutorial. Hats off Mr.Xaprb
Hari Ganesh
16 Jan 07 at 5:22 am
Hi,
This explanation helps clear a lot. Thanks and keep up the good work.
Raghu Balasubramanian
30 Jan 07 at 1:28 am
Good work friend :)
Nadeem
9 Feb 07 at 6:48 pm
Really excellent explanation, thanks!
Alex
21 Feb 07 at 7:40 am
So >/dev/null 2 > /var/log/logfile will redirect STDOUT to bit-bucket and STDERR to the log file. Am i right?
And if >/dev/null is given,will both STDOUT & STDERR be redirected to bit-bucket?
Hari Ganesh
8 Mar 07 at 12:56 am
Hari, your first sentence is right, but in the second, only STDOUT goes to the bit-bucket.
Xaprb
8 Mar 07 at 8:51 am
If &1,&2,&3 are used to refer to STDIN,STDOUT and STDERR respectively
Then why not “wibble > /dev/null &2>&1 “
instead of “wibble > /dev/null 2>&1″
Aman Jain
25 Mar 07 at 11:33 pm
just try: ” ;tryME >> /dev/null 2>&1 ” it outputs bash: syntax error near unexpected token `;’ :))
pulica
27 Mar 07 at 3:04 pm
to answer you Aman Jain I think that:
1) “wibble > /dev/null 2>1″ stdout is redirected to /dev/null and stderr is redirected to a file named “1″ (in the current directory)
2) “wibble > /dev/null 2>&1″ stdout is redirected to /dev/null and stderr is redirected to where stdout is redirected (bit bucket)
3) “wibble > /dev/null &2>1″ stdout is redirected to /dev/null , then character “&” comes, which means that this process will run in background(parallel with the shell u are using). Then comes 2>1 which will create an empt file with the name 1.
And FINALLY!!!
4) “wibble > /dev/null &2>&1 †stdout is redirected to /dev/null. “&” means that, that process(wibble) will run in background(with stdout redirected only).Then another process is started with 2>&1 which creates no file.
U can try it. Maybe im wrong somewhere, Hey im not Steve Bourne(author of sh) ;)
pulica
27 Mar 07 at 3:28 pm
a good job
tnt
24 Apr 07 at 8:51 pm
Thanks a lot….Your information really helped me a lot.
Arun
10 May 07 at 2:10 am
Thanks to xaprb for the nice explanation..
ajay
31 May 07 at 5:00 am
Hi,
This is an awsome sweet tutorial. I have the following:
find . -mtime 3 -regex ‘.*\.md5$’ | xargs sudo rm -f {} \; > /dev/null 2>&1
I am trying to find files that are modified past 3 days or files more than 3 days old, remove them and redirect them to >dev/null…
When I redirect > TO /dev/null 2>&1 the entire command didn’t work, and when I removed things started working.
My question is
1. Do we need to specify the /dev/null 2>&1?
2. Is this for what went wrong – post analysis kind of thing?
Thank you
Raj
Raj
7 Jun 07 at 7:01 pm
Great little article – thankyou.
Cameron
10 Jul 07 at 6:48 am
good explanation..was wonderin all these days as to y we ve de > /dev/null 2>&1 in all our cronjobs…… thx… :)
iris
8 Aug 07 at 7:06 am
echo “good work man, thx a lot” >/dev/null 2>&1;
lol
bilel
25 Aug 07 at 4:37 am
In the following statement there is a “”. Does that combination signify someting unique or is it just a input and output redirection symbol sitting side by side?
smf: :sysinit:/lib/svc/bin/svc.startd >/dev/msglog 2/dev/msglog “), then exactly what is this statement saying?
Mel Briggs
2 Sep 07 at 9:27 am
I just submitted a question and noticed that the main item I needed clarification on was stripped out. I wanted to know how the information flows within a signle command when there are multiple redirection symbols, and in this case, a input redirection symbol placed right next to an output redirection symbol. It looks like: “smf: :sysinit:/lib/svc/bin/svc.startd >/dev/msglog 2/dev/msglog /dev”. That puts the less-than and greater-than signs adjacent. (From Solaris 10 etc/initab.)
Mel Briggs
2 Sep 07 at 9:34 am
Av been using >/dev/null 2>&1 for as long as i can remmeber with no clue but now it’s clear.Thanks for the tutorial
Oscar Rombo
3 Sep 07 at 2:29 am
Its a very useful info, if somebody doubted so, no easy to understand even if it looks simple, thanks god, now it apprears first in the googe search result.\
Thank you once again,.
Ram
18 Sep 07 at 2:51 pm
If anyone is wondering how to refer to the greater-than sign (or angle bracket) when used in this context – some people call it a “hoinky” Probably my all-time favorite tech jargon term
jt
19 Sep 07 at 4:04 pm
[...] What does … mean? at Xaprb – [...]
My del.icio.us bookmarks for September 27th-- Tales of a Minnesota Geek
27 Sep 07 at 8:47 pm
grt job
remo
8 Oct 07 at 1:41 pm
Would anyone be able to explain why it wouldn’t work if you wrote it like this?
command 2>&1 >> file
Luke
8 Nov 07 at 12:57 am
This was a very helpful tutorial. Thanks again!
Mike
28 Nov 07 at 11:58 am
Why do you need the & symbol in from of 1?
Mark
8 Dec 07 at 5:31 pm
If I recall correctly, “>&” redirects handles vs. “>” redirecting output to a file. If you leave out the & it would create a file named “1″
jt
8 Dec 07 at 6:10 pm
Really well done. Thanks for being so clear and concise!
MattS
3 Jan 08 at 6:43 pm
Great job, thanks a lot!
Janbur
9 Jan 08 at 10:06 am
Please explain difference between 2>&1 and 2&>1
Neeraj
16 Jan 08 at 12:49 am
2>&1: it means 2(stderr) is redirected to &1. & tells the address (fd) of 1(stdout). so the stderr is redirected to stdout.
2&>1: it means 2 that is nothing will get executed in bg because of &. And the output will be redirected to a file named 1 not the stdout.
VJ
15 Feb 08 at 1:56 pm
Excellent tutorial, I’m glad people like you exist :)
Steven
21 Feb 08 at 12:47 pm
Good job! I never understood this!
Martin
25 Feb 08 at 2:33 pm
i feel agitated when i have to use something without knowing what its meant for. your info was a quick insight. thanks a million
sas
3 Mar 08 at 5:02 pm
Great stuff,
I have a question. Specailly on bash, if I connect to a computer with ssh and run a process in background with ./job & and try to quit the shell hangs or actually waits for the process to finish, which is not what I want. I want to quit and let the process run.
Now I have a solution to this problem, i.e, if I put ./job 2
Xrules
24 Mar 08 at 3:58 pm
I was trucncated..
Now I have a solution to this problem, i.e, if I put “./job 2
Xrules
24 Mar 08 at 3:58 pm
truncated again because of less than symbol
Now I have a solution to this problem, i.e, if I put ./job 2 [lessthan symbol instead of greaterthan]/dev/null (note the direction is inward and not outward) the shell happily exits and leaves the process in background.
Any ideas why this happens would be educating.
Thanks in advance and the article and the discussion are helpful
Xrules
24 Mar 08 at 3:59 pm
Xrules, look into the nohup command. Even better, though: use screen. It is one of the top 5 things I cannot live without.
Xaprb
24 Mar 08 at 6:19 pm
Thanks
Arun
2 Apr 08 at 2:42 am
I used redirectors all the time, but the 2>&1 always confused me, very well explained!
adam
5 Apr 08 at 11:54 am
Here’s a shortcut for directing stdout and stderr to /dev/null:
wibble >& /dev/null
Now I have a question: What if I want to redirect stdout and stderr to a file called “2″? “wibble >& 2″ doesn’t do it, of course.
Ryan
7 Apr 08 at 1:33 am
>/dev/null 2>&1
is equivalent to
>/dev/null 2>/dev/null?
Then can we say like, “&” is used to trace the direction where 1 (STDOUT) directed?
Dinesh
7 Apr 08 at 4:26 am
Also, Is it possible to redirect only the STDERR error message to any log file (without redirecting program output to STDOUT)
Dinesh
7 Apr 08 at 4:36 am
Ryan:
wibble >./2 2>&1
Dinesh – see VJ’s comment above for your first question.
your second question is also answered in the original post and comments:
ls 2>stderr.log
jt
7 Apr 08 at 12:10 pm
Thanks Jt…
Dinesh
8 Apr 08 at 12:27 am
thanks it helped me a lot
bheemboy
16 Jun 08 at 12:03 pm
And if you just want to hide STDERR output (for instance if you’re creating a bunch of system vars), you can say 2>/dev/null.
export VAR=`command that causes lots of error output 2>/dev/null`
Michael
23 Jul 08 at 8:49 pm
To Eric Dodemont and Dinesh:
It is not exactly correct to say that the command
Command 1: wibble >/mylogs/logfile 2>&1
is equivalent to the command
Command 2: wibble >/mylogs/logfile 2>/mylogs/logfile
99.99% of the times you treat them identically, the result will be interchangeable, but if the wibble program generates both normal output on STDOUT and error messages on STDERR, you might notice a difference.
This gets really nit-picky, but here’s the explanation. Command 1 sets up a situation in which the process running wibble has a single I/O channel to /mylogs/logfile which is shared cooperatively by the STDOUT and STDERR pipes. However, command 2 sets up a different situation in which there are TWO independent I/O channels to /mylogs/logfile, one used by the STDOUT pipe, and the other by the STDERR pipe.
In the single channel situation, there’s a single location that keeps track of how the file is being written – specifically the position in the file for the next write. In the dual channel case, there are two separate and uncoordinated locations tracking the position of the next write.
The end result is that in the single channel case, the logfile receives output and error messages in the sequence in which they are generated by the wibble program, but in the dual channel case, error messages tend to overwrite normal output and vice-versa.
So, if the wibble program would normally run like this:
wibble
This is a line of output.
error 1
This command:
wibble >/mylogs/logfile 2>&1
will generate a logfile that looks like this:
This is a line of output.
error 1
Whereas this command:
wibble >/mylogs/logfile 2>/mylogs/logfile
generates a logfile that looks like this:
error 1 a line of output.
wjs32246
24 Jul 08 at 8:05 pm
To Luke:
the reason that this command:
Command 1: wibble >/mylogs/logfile 2>&1
works differently than this one:
Command 2: wibble 2>&1 >/mylogs/logfiles
has to do with the way in which the shell processes the command line. The shell deals with the I/O redirection tokens not as a totality, but rather in the sequence in which they appear; thus, in command 1, STDOUT is directed to logfile, then STDERR is directed to the same place as STDOUT, whereas in command 2, STDERR is directed to the terminal (because that’s where STDOUT is directed at this time), *then* STDOUT is redirected to logfile.
wjs32246
24 Jul 08 at 8:18 pm
Thanks. I didn’t think I was going to get an answer for that :)
Luke
25 Jul 08 at 3:09 am
“> /dev/null 2>&1″ mean?
action 1: STDOUT is redirected to /dev/null
action 2: STDERR is redirected to STDOUT
question 1: is the above action 1 & action 2 is happening serial or parallel?
question 2: if the above actions happens in serial in which order do they happen? (action 1 -> action2) /or/ (action 2 -> action 1)
if the above questions are invalid then it means still I didn’t understand it. TIA.
smg
22 Aug 08 at 1:57 pm
simple and explain. I cannot leave this page without typing this comment. Thankyou.
vadivu
7 Sep 08 at 10:42 pm
I’ve been trying to keep a cron job from writing to the mail spooler for output and errors. Using the >/dev/null 2>&1 would work, but it suppresses all the file output, and these jobs write to a file for status reporting. Is there anything I can do to have the normal onscreen output written to dev/null but keep the other file outputs?
Bill
12 Sep 08 at 8:42 am
Thanks a lot…
its really clear my doubt…
Jyoti Mishra
24 Sep 08 at 7:36 am
Gr888 work…
Abdussami
7 Oct 08 at 2:26 am
Wow! simple things like this are sometimes overlooked but you’ve done well enough to explain this in detail.
Thanks!
Ang3L
8 Oct 08 at 4:16 am
Good and simple explanation..
Kishore KVR
24 Oct 08 at 4:55 am
Thanks a lot!
It really helps to be able to find such a comprehensive yet easily understandable tutorial.
Exlellent work!
vitaliy
25 Oct 08 at 7:03 am
1) 1> /dev/null 2>&1
2) 2> /dev/null 1>&2
Why 1) is different from 2)
Logu
26 Oct 08 at 1:44 pm
[...] http://www.xaprb.com/blog/2006/06/06/what-does-devnull-21-mean/ « Reset xorg.conf in Fedora [...]
What does “> /dev/null 2>&1″ mean? « Local Scratch
5 Nov 08 at 7:30 am
Thanks a lot! Googling “2>&1″ brought me here and definitely answered my question :)
Daniel
12 Nov 08 at 3:40 am
Xaprb and others,
I was also confused about this for a long time – Your explanations made it very clear to me now.
Thanks so much!
Also, I have a question about the less then redirect symbol as in the following example:
“wobble /dev/null 2>/dev/nullâ€
I have seen this in some *.ksh scripts and it works fine – but I don’t understand what the less-then redirect is doing.
choosakul
13 Nov 08 at 3:41 pm
some how the less-then symbol did not show up – it should look like this:
Also, I have a question about the less then redirect symbol as in the following example:
wobble [less-then-symbol]/dev/null 2>/dev/null
replace [less-then-symbol] with the less then symbol.
Thanks again! d:o)
choosakul
13 Nov 08 at 3:44 pm
thks! i’m an idiot to unix/linux, but getting this is 1 notch up, and millions more to go.
Ronald
14 Nov 08 at 4:44 am
/dev/null 2> error.txt &
that is only report STDERR message to error.txt??
if I need both STDERR and STDOUT message to error.txt
how should I modify it?
/dev/null 2>&1 error.txt & <<<<is correct??
yichu
16 Nov 08 at 11:47 pm
Thanks deude for the explanation!
Azhar
18 Nov 08 at 7:32 pm
I’m having a command to find
/test/bdump/*.trc -mtime +7 -exec rm -f {} \;
when I’m running this command manually without scheduling in cronjobs its working fine, but when i schedule it…it is not getting executed.
to make it work do i require to specify
/test/bdump/*.trc -mtime +7 -exec rm -f {} \; 2>&1
if so how abt /dev/null should that be specified too.
please suggest.
Rajiv
18 Nov 08 at 8:35 pm
yichu:
foo > error.txt 2>&1
snoz
18 Nov 08 at 9:27 pm
Rajiv, it could be that the file glob isn’t working from within the cron job and it’s looking for a file with an actual asterisk in the name. try it with a single filename instead of the glob.
what is your platform/shell? there might be a way to enable/disable file globbing?
snoz
18 Nov 08 at 9:40 pm
we have IBM AIX – 5.3 ML07.
Rajiv
18 Nov 08 at 11:15 pm
snoz
thanks your suggest.
I’ll try it later..
yichu
19 Nov 08 at 3:02 am
rajiv, I’m also on AIX 5.3 and I had to double the backslash before the semicolon:
find /test/bdump/*.trc -mtime +7 -exec rm -f {} \\;
I think that will work
as for the output, I think it all goes to the cron log file unless it was started with -Q. So you don’t have to mess with /dev/null
if you want stdin/out to append to a logfile, just put the redirect in your crontab file after the command
…. \\; >> output.log 2>&1
snoz
19 Nov 08 at 6:27 pm
as i understand .. a program has 2 outputs, both redirected to the console by default:
stdout = 1 (default)
stderr = 2
you can specify other redirection with symbols:
> = redirect to a file
>& = redirect to a pipe (is right?)
& = run the preceded command in background?
| = redirect to another program
>> = append to a file
file = redirects stdout to the file “file”
command >file = same
command >2 = redirects stdout to the file “2″
command >&2 = redirects stdout to pipe 2 (stderr)
command >file 2>&1 = redirects stdout to a file, and stderr to stdout ? (but stdout previously redirected to a file?, then both outputs redirected to the file?)
———
ANOTHER QUESTION:
what if i want to redirect stdout to a file and stderr to the console ?
———
(tips:
empty a file # echo “” > file
filter file lines with “word” # cat file|grep word
process file1 with command1; pass to command2, command3, and append in file2
# command1 > file2
)
AT-HE
23 Nov 08 at 1:57 pm
AT-HE
-what if i want to redirect stdout to a file and stderr to the console ?
stderr goes to the console by default, so just don’t do anything with it
Not sure if this is covered here, but &>file will send stdout and stderr to a file. (bash)
man bash for more, or whatever your shell is.
snoz
23 Nov 08 at 5:27 pm
Hi snoz,
Thanks it is working for me now. not sure why IBM is asking for one more “\” at the end.
anyways thanks a lot for your support.
Rajiv
24 Nov 08 at 12:33 am
great thread…
Santosh
2 Dec 08 at 9:07 pm
Thanks for the info.
In Linux
# command > test.log 2> test.log
is the same as
# command &> test.log
tacallah
5 Dec 08 at 5:18 pm
discard all output messages?
kevin
11 Dec 08 at 12:23 am
[...] Now if you use your php script in a cron task, don’t include the –verbose and make sure you check the $Verbose flag before printing any results. Don’t forget to add to the end of the command line ” > /dev/null 2>&1″ minus the quotes, this sends any std out and std error messages to a black hole. [...]
Integrating PHP Command Line Scripts with Existing Web Projects at Compiled Weekly
6 Jan 09 at 11:17 am
Thanks for this!! I think the answer is buried somewhere in the shell’s manpage, but that’s such a big file I never figured it out.
I wanted to redirect the output of a command to /dev/null. I put “2>1″ in one of my makefiles and I ended up with small “1″ files all over the place. Turns out I wanted “2>/dev/null”.
Sympleko
14 Jan 09 at 5:27 pm
Really appreciate, too many unix books ignore the explanation of this common usage.
jim
14 Jan 09 at 7:44 pm
i have a problem on AIX SO.
I want to execute a daemon with NOHUP:
nohup daemon > /dev/null 2>&1
But after this, my hard disk is full with the output file!!
Why? How?
PS: nohup command generates a file called nohup.out, and i don’t need it.
SaTanI
10 Feb 09 at 5:00 pm
This is repeating AT-HE’s question from above, and expanding it:
what if i want to redirect stdout to a file and stderr to the console?
AND
I am running a system command from a cgi script. By default the stderr output of cgi scripts are not directed to the screen. You get nothing in the way of errors when running system commands from cgi scripts. If you want to display errors, you have to add “2>&1″ to any commands you execute within a cgi-script being run from a browser. But what I want to do is surpress the normal stdout stuff but direct stderr to the screen. My command might look something like this:
rm /tmp/*.* > /dev/null
except I want any errors to come to the screen. What do I add to command to direct stderr to the screen?
lswote
10 Mar 09 at 10:22 am
Sounds good, but how about this
nohup myprogram > foo.out 2> foo.err < /dev/null &
what is this do? foo.err < /dev/null
Alex
17 Mar 09 at 1:15 pm
[...] And finally all the url with wget and feeding any response to the black hole that is /dev/null [...]
Adding a git post-receive hook to fire off Hudson CI server - Jason Meridth -
24 Mar 09 at 2:10 pm
Thank you very much for the explanation.. It was really helpfull.
Pradeep
25 Mar 09 at 4:20 am
complaints > /dev/null
Saeros
7 Apr 09 at 12:13 am
Thank you so much. That 2>&1 thing has been eating at me for a while.
darthmalis
1 May 09 at 10:41 am
Good explanation :)
cheers!
Anand
23 Jun 09 at 4:01 am
There now, That wasn’t so hard, was it? How come no-one else on the internet took the time to explain this? :-)
Many thanks mate!
Doubi
25 Jun 09 at 6:08 am
Thanks a lot , excellent explanation
srini
9 Jul 09 at 11:17 am
excellent
srini
9 Jul 09 at 11:18 am
excellent
Nageswara J
9 Jul 09 at 11:19 am
nice explanation!!
nayan
13 Jul 09 at 10:42 am
you could use &> /dev/null to achieve the same thing.
absolado
23 Jul 09 at 2:29 am
Hi all. I’ve been looking for that days now. But mine is a bit different. Any help appreciated and plz have in mind that I’m no linux geek.
1st of all I use the latest ubuntu 9.04 and I have this file LoginServer_loop.sh which i run with a bash script (think it is bash not sure) and looks like this.
#!/bin/bash
err=1
until [ $err == 0 ];
do
. ./setenv.sh
[ -f log/java0.log.0 ] && mv log/java0.log.0 “log/`date +%Y-%m-%d_%H-%M-%S`_java.log”
[ -f log/stdout.log ] && mv log/stdout.log “log/`date +%Y-%m-%d_%H-%M-%S`_stdout.log”
java -Dfile.encoding=UTF-8 -Xmx128m com.equal.loginserver.L2LoginServer > log/stdout.log 2>&1
err=$?
# /etc/init.d/mysql restart
sleep 10;
done
Well what i’m looking for is to display the process like in windows the .bat file starts it and has “echo” to display all in a java console window.
Hope i’ve explained as better as I could.
Thanks for any help
manos
9 Aug 09 at 4:50 pm
so shouldn’t this redirect 1 into 2 and
then 2 into /dev/null in which case you’d
think you’d see nothing, but you don’t
echo foo >&2 2> /dev/null
i must be missing something
polypus
15 Aug 09 at 8:03 pm
@polypus
I could be wrong, but I think what’s happening is you’re directing the output to wherever 2 is going (the console) Then you’re directing STDERR to dev/null after the fact. When it gets to 2>/dev/null, the output has already been sent to the console. Just a guess really.
nulldev
15 Aug 09 at 8:26 pm
Thank you for the information :-)
Priyanka
18 Aug 09 at 12:41 pm
I have trying to figure this out.. here is small example..
ls -d /etc /eta
ls: 0653-341 The file /eta does not exist.
/etc
if I give
ls -d /etc /eta >/dev/null
out put is
“ls: 0653-341 The file /eta does not exist.”
but if I give
ls -d /etc /eta >/dev/null 2>&1
no out put. where did the sterr go?? shoulnt it be same as above command?
Soham
1 Sep 09 at 5:08 am
That’s very well done. AWSOME
Mohamed
8 Sep 09 at 5:49 pm
Here’s a twist… Given the following line from a CRON file (that I did not write):
11 11 * * * ($HOME/bin/wibble 2>&1) >> $HOME/logs/wibble.log
(I understand the scheduling part)
Does this mean that:
1) Because nothing was specified, ‘normal’ output will still go to STDOUT
2) 2>&1 means that STDERR will go to STDOUT too
3) >> $HOME/logs/wibble.log means that STDOUT (which now includes STDERR) will now be redirected (and appended) to $HOME/logs/wibble.log
Would another way to do this be (in CRON format):
11 11 * * * ($HOME/bin/wibble >>$HOME/logs/wibble.log 2>&1)
Or is there something about CRON that would make this not work? (perhaps the >> append)
BTW, this is running on Fedora.
Thanks!
MontrealPaul
29 Sep 09 at 7:13 pm
Great explaination, i see it is explained in 2006, but still by googling we are directed to this site ….
Really good explanation abt the file descriptors !!!
Thanks !
Abdul Raheem
12 Oct 09 at 8:22 am
The mnemonic that I use when trying to remember the syntax “2>&1″ is “two is greater than one”, where “than” and “and/&” rhyme.
Bubba
9 Dec 09 at 6:36 pm
Bubba, what a great mnemonic! I am constantly doubting whether I’ve got it right when I type it. I think this’ll help me a lot.
Xaprb
9 Dec 09 at 6:52 pm
Be careful when using the “tee” command with 2>&1.
In the following examples, ONLY stderr output is triggered by calling “grep” for
pattern “hello” on a non-existent file “NO_FILE.txt” during an attempt to redirect
both stdout and stderr to file “./stdout_and_stderr”. Finally, I echo out the output
file name “./stdout_and_stderr” and dump the file contents to stdout using “cat”.
########################################
## WORKED: stderr was redirected to file
> grep hello NO_FILE.txt > ./stdout_and_stderr 2>&1 ; echo “cat ./stdout_and_stderr:” ; cat ./stdout_and_stderr
cat ./stdout_and_stderr:
grep: 0652-033 Cannot open NO_FILE.txt.
########################################
########################################
## FAILED: stderr was NOT redirected to file
> grep hello NO_FILE.txt 2>&1 > ./stdout_and_stderr ; echo “cat ./stdout_and_stderr:” ; cat ./stdout_and_stderr
grep: 0652-033 Cannot open NO_FILE.txt.
cat ./stdout_and_stderr:
########################################
########################################
## WORKED: stderr was “tee”-ed BOTH to stdout AND file
> grep hello NO_FILE.txt 2>&1 | tee ./stdout_and_stderr ; echo “cat ./stdout_and_stderr:” ; cat ./stdout_and_stderr
grep: 0652-033 Cannot open NO_FILE.txt.
cat ./stdout_and_stderr:
grep: 0652-033 Cannot open NO_FILE.txt.
########################################
########################################
## FAILED: stderr was “tee”-ed to stdout but NOT to the file.
## In this case, 2>&1 was applied to the “tee”, but NOT to the “grep”
## before the pipe. Therefore, when piping commands, make sure that the 2>&1
## appears immediately BEFORE the pipe processing the command whose
## stderr stream you want redirected to stdout.
> grep hello NO_FILE.txt | tee ./stdout_and_stderr 2>&1 ; echo “cat ./stdout_and_stderr:” ; cat ./stdout_and_stderr
grep: 0652-033 Cannot open NO_FILE.txt.
cat ./stdout_and_stderr:
########################################
D
23 Dec 09 at 3:44 pm
Goodone bro! thanks.
sojin
6 Jan 10 at 10:55 pm
Do you ever use 2>&- at the end of a command to temporarily turn off the stderr file descriptor?
Josh
12 Jan 10 at 3:14 pm
Thanks for clearing my confusion. Nice Info.
Krishna | PHP Programmer
1 Mar 10 at 5:57 am
[...] found a great article over at xaprb.com that does a pretty good job of explaining it. Given that context, you can see the command above is [...]
Figuring it out, what does “> /dev/null 2>&1? mean? | SilentGap
23 Mar 10 at 12:02 pm
what are STDIN,STDOUT,STDERR
sree
22 Apr 10 at 6:38 am
I have seen many script with the following line as the first line to generate log…what does exec do
exec 1> XXX_$(date +”%Y%m%d_%H%M%S”).log 2>&1
sree
22 Apr 10 at 7:08 am
According to http://www.softpanorama.org/Tools/exec.shtml , ‘exec’ simply runs the following command
within the current process, helping to reduce process clutter if you are running the command in the foreground.
D
22 Apr 10 at 9:35 am
HI,
I have 1000 line script.My question is from which line it will start execution.How can i say from this line my script starts execution,
Reply if know…
sudhir
18 May 10 at 12:02 pm
Wonderful article.
Rajeshwari Laxmanan
26 May 10 at 2:18 am
Mystery solved! Thanks for the clear explanation!
Tony Fardella
8 Jun 10 at 9:55 pm
Xaprb, Excellent information, it was indeed helpful !!!
Balaji Konar
9 Jun 10 at 10:45 pm
Thank, Xaprb. You’re still getting thannx, huh? good job.
itomatik
11 Jun 10 at 12:55 pm
Excellent, Thanks!!
Sushil
16 Jun 10 at 4:50 am
good explanation.. thank you!!
Prashanth S
6 Jul 10 at 1:00 am
Hi there.
I think I haven’t understood this issue quite well.
I’m using FreeBSD and just tried to do the following:
# ls > /dev/null 2>&1
Ambiguous output redirect.
Also, see what happens when I try doing this:
# ls 2> /dev/null
ls: 2: No such file or directory
Btw, I’m using cshell (csh).
Any ideas? Thanks in advance.
Rod Elias
8 Jul 10 at 10:44 am
@rod elias:
When you do “ls 2 > /dev/null” it thinks you’re trying to list a file or directory named “2″, then send the output of the command to dev/null.
What were you trying to accomplish exactly?
jt
13 Jul 10 at 5:47 pm
@jt:
I was trying to redirect error messages (if any) to /dev/null.
For example, suppose that file arq1.txt doesn’t exist in the current directory. So, if I do something like:
#ls arqu1.txt 2> /dev/null
ls: 2: No such file or directory
ls: arq1.txt: No such file or directory
However, if file new.pdf does exist and I try:
# ls new.pdf > /dev/null
everything happens fine.
Besides, I tried all the same examples here in GNU/Linux and they worked just fine.
Please remember that I’m using FreeBSD 7.0 with csh.
Thanks for your attention, dude!
Rod Elias
13 Jul 10 at 6:27 pm
@Rod Elias:
Interesting… I don’t know much about csh, but I did find this… maybe it’s just not possible?
“Other shells such as csh are notably limited in redirection capability, making them better suited to interactive use than to shell programming or other complex uses.”
jt
13 Jul 10 at 6:37 pm
@jt:
Hmm. There you have it, mate!
I think it’s some kind of limitation regarding csh then.
Thanks for posting that piece of information.
Btw, it would be cool to chat with you about some UNIX related issues.
Please, feel free to add me to your contact list on MSN (if you use it): rod at wgo dot com dot br
[ ]‘s
Rod Elias
13 Jul 10 at 6:51 pm
> /dev/null 2>&1
proves to be quite useful when cronjobs are run.
If we want a cronjob to run silently and not clutter our mailboxes with mails each time it runs then redirecting errors and output to /dev/null becomes really a good trick.
Pradeep
26 Aug 10 at 1:25 pm
Well explained. Cheers.
Jaroslaw Dobrzanski
2 Sep 10 at 3:57 am
man csh
>& name
>&! name
The file name is used as standard output. If the file does not exist then it is created; if the file exists, it is truncated, its previous contents being lost.
If the shell variable noclobber is set, then the file must not exist or be a character special file (e.g., a terminal or ‘/dev/null’) or an error results. This helps prevent accidental destruction of files. In this case the `!’ forms can used to suppress this check.
The forms involving `&’ route the diagnostic output into the specified file as well as the standard output. name is expanded in the same way as `<' input filenames are.
@Rod Elias
3 Sep 10 at 11:08 am
Thanks bro.
It helped clarify things.
Hugs.
Rod Elias
3 Sep 10 at 11:39 am
Very helpful. Thanks.
JK
12 Sep 10 at 10:09 am
excellent tutorial!!!! Thanks a lot
vamsi
29 Sep 10 at 3:07 am
Many thanks, that’s the best explanation I’ve ever seen for this.
westmorlander
7 Oct 10 at 10:37 am
Thanks! great piece
Condor
26 Oct 10 at 7:42 am
i will explain you with an example
eg:
ls dir1 > file1 2>&1
ls dir1 output is sent to file1
if there is any errors that also will be sent to file1
Majid Khan
20 Dec 10 at 5:28 am
in short both error message and output message is sent to same file
2>&1 means error message is sent to the standard out put file
Majid Khan
20 Dec 10 at 5:31 am
That was really helpful. Thanks to all the people to make this discussion more informative.
Deepak
3 Jan 11 at 3:27 pm
*/25 * * * * /notesbit/work/scripts/crons/denyhack3 > /dev/null 2>&1
Is this working redirect to mail spooling..?
Asif syed
11 Jan 11 at 9:26 am
thanku it really works…
satveer
13 Jan 11 at 1:41 am
Neat explanation and analogy. Thanks!
Nelson
11 Mar 11 at 12:02 am
This is EXACTLY the kind of explanation needed for MANY quirks in Unix. They are hard to come by. Thank you very much, this was uncommonly helpful.
Trev
15 Mar 11 at 10:04 pm
Thanks! Very handy. :)
Niklas
22 Mar 11 at 6:27 am
Tnx! ;)
Vincent
4 Apr 11 at 10:36 pm
Thxs Man!, you’v saved me alot of time.
James K
6 May 11 at 8:23 am
@Xaprb
Thanks for the helpful explanation!
@wjs32246
Thank you for the wonderful clarification!
Lakis Euthimiou
6 Jun 11 at 7:41 pm
Excellent info @Xaprb
Thanks for the nice article.
Can you also help us know source of your info.
I would like to read in detail about stuff like these.
Rohit V
7 Jul 11 at 10:23 am
Thanks nice inside,
but it didn’t solved my problem.
/usr/local/bin/php -q script.php –var=val >& \dev\null
i have tried almost everything, but could not figure this out. It returns
Ambiguous output redirect
Can you please help me solve this.
Thanks in advance
Gohar Sultan
2 Aug 11 at 7:44 am
@Gohar Sultan
Are you using csh?
If so,
man csh
>& name
>&! name
The file name is used as standard output. If the file does not exist then it is created; if the file exists, it is truncated, its previous contents being lost.
If the shell variable noclobber is set, then the file must not exist or be a character special file (e.g., a terminal or ‘/dev/null’) or an error results. This helps prevent accidental destruction of files. In this case the `!’ forms can used to suppress this check.
The forms involving `&’ route the diagnostic output into the specified file as well as the standard output. name is expanded in the same way as `<' input filenames are.
Hope this helps!
Rod Elias
2 Aug 11 at 8:16 am
@Xaprb Some posts are TIMELESS. 5 Years later I found this useful too!! Thanks.
Purvez
21 Aug 11 at 5:11 am
Thanks, short, concise and clear: it’s a pleasure to find such an answer to my question.
Laike,
still orbiting
Laika
22 Aug 11 at 11:18 am
Yay! Now I can surpress output in a nonconformist way that shows off my secret knowledge:
echo blah >&2> /dev/null
LinuxViaMac
25 Sep 11 at 8:01 pm
Just to add here, there also exists the following:
command &>filename
Above redirects STDOUT as well as STDERR of the command to file-name.
Gaurav Jain
19 Jan 12 at 11:20 pm
Thanks for this great explanation. it all makes sense now!
Kathryn
20 Jun 12 at 9:23 pm
If I’m going to use “2>/dev/null”, does this make sense? Is this the same as “> /dev/null 2>&1″?
Thanks in advance!
gwatsmiles
5 Sep 12 at 12:42 am
That makes sense, but it only redirects STDERR to /dev/null, not STDOUT. So it’s not the same.
Xaprb
7 Sep 12 at 10:04 am