7 Things To Do With The Pipe In a Linux Shell

July 1st, 2008 | by blank89 |

The pipe is the vertical line above your enter key. It looks like this without the quotes: “|”. Using the pipe character in the Linux shell allows you to string commands together, performing another command on the output from the first. This is a list of 10 things you can do with the pipe that will help you master the command line.

Remember, these commands don’t have to be used as given in the list below, look up the man pages for additional options and arguments. Also, don’t forget that you can string piped commands to use more than one. Stringing piped commands are very powerful when combined.

1. Grep

Grep is one of the most common commands used with the pipe. It allows you to quickly search through the output of another command. For an example, look at the following code. Grep can also take regular expressions as arguments.

ps ux | grep /usr

This command will print every line of the list of processes with “/usr” in it.

2. Head

Head simply displays the first 10 lines of whatever you piped into it. Here’s your example:

ls / -l | head

This example shows the first 10 lines of the directory listing.

3. Tail

Tail is the same as head, except that it displays the last 10 lines.

ls / -l | tail

This example shows the last 10 lines of the directory listing.

4. Sort

Sort will perform an alphanumeric and symbolic sort on whatever you pipe in to it.

last | sort

This example will display the last users to be logged in, ordered alphabetically by user.

5. Uniq

Uniq cuts out lines that are repeated. This can prevent you from sorting through hundreds of lines in a log file that are exactly the same. Here is an example that’s piped twice: first to uniq and then to grep.

cat some_log_file | uniq | grep [search]

This example will print unique lines of the log file that contain whatever you are searching for.

6. Less

Less allows the user to scroll through the input of a large amount of text.

[command with really long output] | less

This example will allow you to scroll through the output of the command. You can scroll up and down, unlike the “more” command.

7. Fold

Fold will take input and limit the width of the output to a certain number of characters (80 by default).

arp | fold

This command will limit the output of the arp command to 80 characters.

Your Ad Here
Share this article:
  • Digg
  • Reddit
  • Slashdot
  • del.icio.us
  • Technorati
  • StumbleUpon
  • Facebook
  • Google
  • BlinkList
  • Blogosphere News

Similar Posts

You must be logged in to post a comment.