3.5. grep with Pipes

Instead of taking its input from a file, grep often gets its input from a pipe.

Example 3.35.
% ls -l
					drwxrwxrwx    2    ellie    2441   Jan   6   12:34   dir1
					-rw-r--r--    1    ellie    1538   Jan   2   15:50   file1
					-rw-r--r--    1    ellie    1539   Jan   3   13:36   file2
					drwxrwxrwx    2    ellie    2341   Jan   6   12:34   grades

% ls -l | grep '^d'
					drwxrwxrwx    2    ellie    2441   Jan   6   12:34   dir1d
					rwxrwxrwx     2    ellie    2341   Jan   6   12:34   grades
				

Explanation

The output of the ls command is piped to grep. All lines of output that begin with a d are printed; that is, all directories are printed.

Get Linux Shells by Example now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.