Tips Linux Explorers   All Things Linux Forum   Great Linux Links   LinuxClues.com   Hometown   Email 



REDIRECTIONS
( AND THE BLACK HOLE: >/dev/null )


Right, it´s about time to put something right: uptill now I did use the ¨<¨ and ¨>¨ characters to make clear to you what part of the text the real commands were. For ¨The Linux Starters¨ that seemed like a good idea.

Now we come to the point where I do need those signs to have a function in the commands I will tell you about.
But let´s be fair, if you made it up till here, you can´t be considered as a ¨Starter¨ anymore. And you will know a command when you see one !!

We use the ¨> <¨ signs for redirections, example:

CODE
$ ls /music/dylan/*.mp3 1>songs.txt


Will make a list ( ls ) of all the files in the dylan directory ( /music/dylan ) ending with .mp3 ( *.mp3 ) . This list usualy sent to standard out ( 1 ) but now redirected ( > ) to the file songs.txt ( songs.txt ) in your /home.

NOTE: the 1 for standard out is in this example not really needed, but we might as well get used to it !
0=Standard in ( usually the keyboard ) 1=Standard out ( the normal output on your screen ) 2=Standard error ( the error messages you get on the screen )


Still with me ? Next example:

CODE
$ wc -l 0<songs.txt


Will count the lines ( wc-l ) from the input ( 0< ) of the file songs.txt
Simply said: it will tel you how many Bob Dylan .mp3´s there are in my dylan directory !
Well, that is a nifty trick isn´t it ?


O.K. Because you´re starting to like this, one last example:

CODE
$ ls -R /var >/dev/null 2>errors.txt


This is a funny one, let me first explain what we end up with, before cutting it in pieces: we will get a list of all the files we have no permission for, in the /var directory ( the error messages we usually see on the screen as we try to access a file we have no permissions for ).
List all the files in all directories ( ls -R ) in the /var directory ( /var ) and send them ( > ) to the black hole ( /dev/null ) and send all the error messages ( 2> ) to a file called errors.txt ( errors.txt )


Bruno


-- May 13 2003 ( Revised Dec 10 2005 ) --


Tips Linux Explorers   All Things Linux Forum   Great Linux Links   LinuxClues.com   Hometown   Email