Piping

From SlackWiki
Jump to navigation Jump to search
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.

You can mix commands to do certain things.

 dmesg | grep hd

That will search dmesg and try to find the string "hd". Now piping is extremely useful. Another example of a pipe:

 dmesg | less

Now this command you can see the dmesg fully and is easier to read.

You can also make longer 'pipelines' by using a pipe of several commands. For instance, to browse the last fifty lines of dmesg:

 dmseg | tail -50 | less

Redirection:

This will save the dmesg information in your home directory in the file log.txt

 dmesg > ~/log.txt

You can also redirect only the stdout by using '1>' or only the stderr by using '2>'.

For instance, if you want to run a command that gives a lot of junk as output, and you just want to see any errors, you can redirect the output to the null device (/dev/null):

 mplayer /path/to/movie.avi 1> /dev/null

You can also use the > and >> to cat stdin into a new or existing file:

 bash-3.1$ cat > newFile
 This is a new file            
 you can type text here and it
 get's redirected to the file newFile
 ctrl-c exits
  • Warning
  • cat > fileThatAlreadExists WILL OVERWRITE THE FILE! be careful :-)

You can use cat to check the text you just added:

 bash-3.1$ cat newFile 
 This is a new file 
 you can type text here and it
 get's redirected to the file newFile
 ctrl-c exits
 

Now that a file is created you can add to it with the >> operator

 bash-3.1$cat >> newFile
 This is a new line to add to newFile

And if you use cat again to read the file

 bash-3.1$cat newFile
 This is a new file 
 you can type text here and it
 get's redirected to the file newFile
 ctrl-c exits
 This is a new line to add to newFile