You can use the grep command as a filter with other commands, enabling you to filter out unnecessary information from the command output. To use grep as a filter, you must pipe the output of the command through grep. The symbol for pipe is “|”.
The following example displays files that end in “.ps” and were created in the month of September.
$ ls -l *.ps | grep Sep |
The first part of this command line produces a list of files ending in .ps.
$ ls -l *.ps -rw-r--r-- 1 user2 users 833233 Jun 29 16:22 buttons.ps -rw-r--r-- 1 user2 users 39245 Sep 27 09:38 changes.ps -rw-r--r-- 1 user2 users 608368 Mar 2 2000 clock.ps -rw-r--r-- 1 user2 users 827114 Sep 13 16:49 commands.ps $ |
The second part of the command line pipes that list through grep, looking for the pattern Sep.
| grep Sep |
The search provides the following results.
$ ls -l *.ps | grep Sep -rw-r--r-- 1 user2 users 39245 Sep 27 09:38 changes.ps -rw-r--r-- 1 user2 users 827114 Sep 13 16:49 commands.ps $ |