Difference between revisions of "CLI Piping"

From FreekiWiki
Jump to navigation Jump to search
Line 25: Line 25:
  
 
=== Example 2 ===
 
=== Example 2 ===
 +
ls -l /home | grep -vE "diskless|build"
  
 
== Quiz ==
 
== Quiz ==

Revision as of 12:59, 29 March 2008

Commands

grep  = print lines matching a pattern
less  = view the contents of a text file
wc    = print the number of newlines, words, and bytes in files
cat   = concatenate files and print on the standard output
find  = search for files in a directory hierarchy
sort  = sort lines of text files
uniq  = condense duplicate lines
awk   = pattern scanning and text processing language
lsof  = list open files
xargs = build and execute command lines from standard input

The Pipe Character

The pipe is the vertical bar, '|', it allows you to send the output from one command, as input to a second command. It allows you to combine two or more commands to process data using combinations of commands. It is very useful, but requires you to have a strong understanding of the commands that you're going to combine. As you use the command line more, you'll become more familiar with the commands and you'll start to understand which commands make sense to pipe together and which ones don't. Each side of the pipe is a an individual statement, with a command, possible options and possibly some other information that the command needs.

Examples

Count the number of files in the ~/examples directory

ls ~/examples | wc -w

'ls ~/examples' returns

adv_cli  bash_script  basic_commands  c_example  media_example

'wc -w' counts the number of words (words are sets of characters surrounded by whitespace. So, when you combine the two you are counting the number of files that were listed.

Example 2

ls -l /home | grep -vE "diskless|build"

Quiz

  1. Run 'dmesg', pipe it to 'sort' and then to 'uniq'