LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Why is there a need for /dev/null, /dev/zero and other special device (files) in /dev (https://www.linuxquestions.org/questions/linux-general-1/why-is-there-a-need-for-dev-null-dev-zero-and-other-special-device-files-in-dev-4175420472/)

suttiwit 08-05-2012 10:15 AM

Why is there a need for /dev/null, /dev/zero and other special device (files) in /dev
 
I wonder why there is a need that the UNIX and/or Linux developers create /dev/null, /dev/zero and other special device (files) in /dev.

Anyways, Why?

pixellany 08-05-2012 10:39 AM

/dev/null is a place to send things that you NEVER want to see again---e.g. error messages

/dev/zero is a handy place to get a zero---or LOTS of zeros

WHY?
As with many things in engineering, it could have been the first solution that met the design criteria, and it never had enough issues to motivate anyone to change it.

segmentation_fault 08-05-2012 11:02 AM

Because they are tools that can and are useful.

SharpyWarpy 08-05-2012 11:29 AM

Speaking for myself it's good to know /dev/null is there.

suttiwit 08-06-2012 08:46 AM

Thanks
 
Hi, Thanks for a fast reply. I have been coding advanced programs for linux and never need to use /dev/null, /dev/zero at all.

SharpyWarpy 08-06-2012 12:08 PM

I use it when I'm putting together a DVD of something, like when I need to extract a video track from a video file I do:
tcextract -x mpeg2 -i file.mpg > file.m2v 2>/dev/null
This keeps the process from spitting out bunches of stuff to standard output, meaning the screen, and causing unnecessary curiosity or suspicion on your part. Keeps the screen clean.

segmentation_fault 08-06-2012 12:25 PM

Also
Code:

somecommand > /dev/null 2>&1
keeps the screan totally clean :D

SharpyWarpy 08-06-2012 01:57 PM

Quote:

Originally Posted by segmentation_fault (Post 4747356)
Also
Code:

somecommand > /dev/null 2>&1
keeps the screan totally clean :D

Can you provide an explanation for the "2>&1" ? Thanks

pixellany 08-06-2012 02:54 PM

Quote:

Originally Posted by SharpyWarpy (Post 4747435)
Can you provide an explanation for the "2>&1" ? Thanks

Redirects standard error to standard output.

SharpyWarpy 08-06-2012 04:57 PM

Wouldn't redirect to standard output cause it to show up on the screen? Isn't standard output the screen? I mean what is displayed there? And the ampersand followed by the number 1, I need a clear explanation of what each one means, the ampersand and the number 1. What I DO know is the ampersand can be used at the end of a command line to make it run in the background. I didn't know it could be used for other stuff. Every time I go to the bash user manual I have a devil of a time finding these special symbols. I find them being USED but not an adequate decription of exactly what they are meant to do in each particular situation in which they are used. Another example of what I mean here is the curly bracket, "{" and "}". I know it's used in special cases such as changing certain elements of a bunch of filenames with the inclusion of other symbols such as # ^ but as soon as I learn it I seem to forget. Does that mean I'm too old to learn? I hope not because that would be catastrophic.

suicidaleggroll 08-06-2012 05:26 PM

Quote:

Originally Posted by SharpyWarpy (Post 4747544)
Wouldn't redirect to standard output cause it to show up on the screen? Isn't standard output the screen? I mean what is displayed there? And the ampersand followed by the number 1, I need a clear explanation of what each one means, the ampersand and the number 1. What I DO know is the ampersand can be used at the end of a command line to make it run in the background. I didn't know it could be used for other stuff. Every time I go to the bash user manual I have a devil of a time finding these special symbols. I find them being USED but not an adequate decription of exactly what they are meant to do in each particular situation in which they are used. Another example of what I mean here is the curly bracket, "{" and "}". I know it's used in special cases such as changing certain elements of a bunch of filenames with the inclusion of other symbols such as # ^ but as soon as I learn it I seem to forget. Does that mean I'm too old to learn? I hope not because that would be catastrophic.

file descriptor 2 is stderr
file descriptor 1 is stdout
file descriptor 0 is stdin

You need to put a & in front of the 1 to tell the redirection to go to file descriptor 1 rather than a file called "1". You're right in that stdout is the screen, however the "> /dev/null" has already redirected stdout to /dev/null.

Basically the "2>&1" combines both stdout and stderr on stdout, then the "> /dev/null" throws both of them into the abyss.

SharpyWarpy 08-06-2012 06:13 PM

Okay one more thing. Does it matter in which order these are used? I've always seen ">" used before "2>" but does it make a difference if they are used with "2>" before ">" ?

towheedm 08-06-2012 07:26 PM

Here is a start on redirection:
http://mywiki.wooledge.org/BashFAQ/055
http://www.gnu.org/software/bash/man...l#Redirections

chrism01 08-07-2012 12:26 AM

Because stdout is the default output, its allowed to skip the '1' ie
Code:

prog > t.t

# exactly same as

prog 1>t.t

Obviously when using stderr as well, its a good idea to specify both to avoid ambiguity.
You can in fact separate output & errors thus
Code:

prog 1>prog.out 2>prog.err


All times are GMT -5. The time now is 11:51 PM.