LinuxDevCenter.com

oreilly.comSafari Books Online.Conferences.
Sign In/My Account | View Cart   

We've expanded our Linux news coverage and improved our search! Search for all things Linux across O'Reilly!

Search
Search Tips

advertisement


Listen Print Subscribe to Linux Subscribe to Newsletters

Unix Power Tools
What Can You Do with an Empty File?

by Jerry Peek
01/27/2000

It isn't a file, actually, though you can use it like one. /dev/null is a UNIX device. (Well, okay. It's a device file.) It's not a physical device. /dev/null is a special device that "eats" any text written to it and returns "end-of-file" (a file of length 0) when you read from it. So what the heck can you use it for?

  • Empty another file. Just copy /dev/null "on top of" the other file .

  • Make another program "quiet" by redirecting its output there. For instance, if you're putting a program into the background and you don't want it to bother you, type:

    % progname > /dev/null &

    That redirects standard output but leaves standard error hooked to your terminal, in case there is an error.

  • Answer a program that asks a lot of questions-you know you'll just press RETURN at each prompt. In a lot of cases, you can redirect the program's standard input from /dev/null:

    % progname < /dev/null
    Want the default setup? If yes, press RETURN:
    Enter filename or press RETURN for default:
       ...

    You should test that with each program, though, before you assume this trick will work. (If it doesn't work, try yes

  • Where a program needs an extra filename but you don't want it to read or write an actual file. For instance, the grep programs won't give the name of the file where they find a match unless there are at least two filenames on the command line. When you use a wildcard in a directory where maybe only one file will match, use /dev/null to be sure that grep will always see more than one :

    % grep "whatever" * /dev/null

    You're guaranteed that grep won't match its regular expression in /dev/null. :-)

Another interesting device (mostly for programmers) is /dev/zero. When you read it, you'll get ASCII zeros (NUL characters) forever. There are no newlines either. For both of those reasons, many UNIX commands have trouble reading it. If you want to play, the command below will give you a start (and head will give you a stop!):

% fold -20 /dev/zero | od -c | head

On some UNIX versions, the head program may not terminate after it's printed the first ten lines. In that case, use sed 10q instead of head.


Back More Unix Power Tools

 




Tagged Articles

Be the first to post this article to del.icio.us

Sponsored Resources

  • Inside Lightroom
Advertisement
O'Reilly Media
© 2008, O'Reilly Media, Inc.
(707) 827-7000 / (800) 998-9938
All trademarks and registered trademarks appearing on oreilly.com are the property of their respective owners.
About O'Reilly
Privacy Policy
Contacts
Authors
Press Room
Jobs
User Groups
Academic Solutions
Newsletters
Writing for O'Reilly
RSS Feeds
Other O'Reilly Sites
O'Reilly Radar
Ignite
Tools of Change for Publishing
Digital Media
Inside iPhone
O'Reilly FYI
makezine.com
craftzine.com
hackszine.com
perl.com
xml.com
Sponsored Sites
Inside Aperture
Inside Lightroom
Inside Port 25
InsideRIA
java.net