Linux CLI for Beginners, or, Fear Not the Linux Command Line!: Page 3

(Page 3 of 4)

"grep" is kind of a nasty name -- there's some disagreement on what it stands for, including "Globally search for Regular Expressions and Print" and "General Regular Expression Parser". But it's an incredibly useful program that prints just the lines matching a particular pattern.

For instance, to combine the squirrel search and the tiger search, you just pipe one search through grep, passing grep the second pattern:

$ locate squirrel | grep tiger

locate squirrel found all those squirrel images, then grep searched through all of them to find any that also had "tiger" in the name. Now I know exactly where the file is.

Counting lines

Pipelines aren't limited to just two commands, though. For instance, remember that long list of files with cat in the name? I know there's a program called "cat"; suppose I want to know how many of those other files with "cat" in the name are programs.

Programs are usually located in /usr/bin, but sometimes they're in /bin or some other place. But they usually have "bin" in the name somewhere. So if I take all the files that have both "cat" and "bin" in the name:

$ locate cat | grep bin
I can then find out how many lines there were, by using the useful wc (word count) program:
$ locate cat | grep bin | wc -l

wc -l says to count the number of lines (that's ell for "lines", not a one) -- if you omit the -l part, it prints lines, words and characters.

On my system I have 34 programs with cat in the name. How many do you have?

Akkana Peck is a freelance programmer whose credits include a tour as a Mozilla developer. She's also the author of Beginning GIMP: From Novice to Professional.

This article was first published on LinuxPlanet.com.

Page 3 of 4

Previous Page
1 2 3 4
Next Page

0 Comments (click to add your comment)
Comment and Contribute


(Maximum characters: 1200). You have characters left.



IT Management Daily
Don't miss an article. Subscribe to our newsletter below.

By submitting your information, you agree that datamation.com may send you Datamation offers via email, phone and text message, as well as email offers about other products and services that Datamation believes may be of interest to you. Datamation will process your information in accordance with the Quinstreet Privacy Policy.