Beginners – Piping and Re-Direction

Valic —  January 6, 2013 — Leave a comment

Piping Commands Together

The pipe character “|”  is usually used to chain 2 or more commands. The pipe command will take the standard output (STDOUT) from the program that you are using and will sent as standard input (STDIN) for the command after the pipe.

Example: root@server:~# ls /var/log/ | grep mail

The “pipe” command will take the output of the ls command and “pipes” into the input of the grep command.

Redirecting Program Output to Files

There are situations when you want to save the output of a command to a file rather than displaying it on the screen or both save to a file and display. For instance if you want to create a file that contain a list of all files and directories from /var/www you can do something like this:

ls /var/www > www_list.txt

This will redirect the standard output to www_list.txt

Another similar command can be written so rather than creating a new file called www_list.txt to append the output to the end of the original file.

ls /var/www >> www_list.txt

  • > – if the file does not exist will be created,
  • >> – will append to the end of the file, and if the file does not exist will be created,
  • 2>> – will append only error to the file
  • 2>&1 – will redirect errors and the output in the same place.


Posts Twitter Facebook

Editor in Chief at Debian-Tutorials, Linux enthusiast.

No Comments

Be the first to start the conversation.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.