LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 08-05-2012, 11:15 AM   #1
suttiwit
Member
 
Registered: Aug 2012
Location: Chiang Mai, Thailand
Distribution: Kubuntu 12.10 x86_64
Posts: 192
Blog Entries: 2

Rep: Reputation: 22
Question 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?
 
Old 08-05-2012, 11:39 AM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 740Reputation: 740Reputation: 740Reputation: 740Reputation: 740Reputation: 740Reputation: 740
/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.
 
Old 08-05-2012, 12:02 PM   #3
segmentation_fault
Member
 
Registered: Sep 2008
Location: Ioannina, Greece
Distribution: Gentoo
Posts: 332

Rep: Reputation: 55
Because they are tools that can and are useful.
 
Old 08-05-2012, 12:29 PM   #4
SharpyWarpy
Member
 
Registered: Feb 2003
Location: Florida
Distribution: Fedora 18
Posts: 862

Rep: Reputation: 91
Speaking for myself it's good to know /dev/null is there.
 
Old 08-06-2012, 09:46 AM   #5
suttiwit
Member
 
Registered: Aug 2012
Location: Chiang Mai, Thailand
Distribution: Kubuntu 12.10 x86_64
Posts: 192

Original Poster
Blog Entries: 2

Rep: Reputation: 22
Cool 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.
 
Old 08-06-2012, 01:08 PM   #6
SharpyWarpy
Member
 
Registered: Feb 2003
Location: Florida
Distribution: Fedora 18
Posts: 862

Rep: Reputation: 91
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.
 
Old 08-06-2012, 01:25 PM   #7
segmentation_fault
Member
 
Registered: Sep 2008
Location: Ioannina, Greece
Distribution: Gentoo
Posts: 332

Rep: Reputation: 55
Also
Code:
somecommand > /dev/null 2>&1
keeps the screan totally clean
 
Old 08-06-2012, 02:57 PM   #8
SharpyWarpy
Member
 
Registered: Feb 2003
Location: Florida
Distribution: Fedora 18
Posts: 862

Rep: Reputation: 91
Quote:
Originally Posted by segmentation_fault View Post
Also
Code:
somecommand > /dev/null 2>&1
keeps the screan totally clean
Can you provide an explanation for the "2>&1" ? Thanks
 
Old 08-06-2012, 03:54 PM   #9
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 740Reputation: 740Reputation: 740Reputation: 740Reputation: 740Reputation: 740Reputation: 740
Quote:
Originally Posted by SharpyWarpy View Post
Can you provide an explanation for the "2>&1" ? Thanks
Redirects standard error to standard output.
 
Old 08-06-2012, 05:57 PM   #10
SharpyWarpy
Member
 
Registered: Feb 2003
Location: Florida
Distribution: Fedora 18
Posts: 862

Rep: Reputation: 91
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.
 
Old 08-06-2012, 06:26 PM   #11
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2134Reputation: 2134Reputation: 2134Reputation: 2134Reputation: 2134Reputation: 2134Reputation: 2134Reputation: 2134Reputation: 2134Reputation: 2134Reputation: 2134
Quote:
Originally Posted by SharpyWarpy View Post
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.
 
1 members found this post helpful.
Old 08-06-2012, 07:13 PM   #12
SharpyWarpy
Member
 
Registered: Feb 2003
Location: Florida
Distribution: Fedora 18
Posts: 862

Rep: Reputation: 91
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 ">" ?
 
Old 08-06-2012, 08:26 PM   #13
towheedm
Member
 
Registered: Sep 2011
Location: Trinidad & Tobago
Distribution: Debian Stretch
Posts: 612

Rep: Reputation: 125Reputation: 125
Here is a start on redirection:
http://mywiki.wooledge.org/BashFAQ/055
http://www.gnu.org/software/bash/man...l#Redirections
 
Old 08-07-2012, 01:26 AM   #14
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.10, Centos 7.5
Posts: 17,677

Rep: Reputation: 2488Reputation: 2488Reputation: 2488Reputation: 2488Reputation: 2488Reputation: 2488Reputation: 2488Reputation: 2488Reputation: 2488Reputation: 2488Reputation: 2488
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
pseudo terminals: /dev/ptmx <-> /dev/pts/ vs. /dev/ptyp <-> /dev/ttyp rtspitz Linux - Software 2 12-02-2011 03:07 PM
/dev/sdf and /dev/sdb special device cannot mount bloodyscript Linux - Newbie 2 02-28-2010 08:05 PM
using flash drive changes device /dev/sr0 to /dev/sr1 for mapping to /dev/pktcdvd/0? lugoteehalt Linux - Software 3 10-24-2007 11:27 AM
Startx Permission problems on /dev/null and /dev/mem on freshly compiled 2.6.22.1 Eric_Cartman Linux - Kernel 2 09-09-2007 02:42 AM
/dev/tty0, /dev/tty1, /dev/tty10...and so on...which should be used for a device ??? b0nd Slackware 2 04-02-2006 09:14 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 10:58 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration