[1+1=2]

OneAndOneIs2

« The firstNew arrival »

Fri, Sep 24, 2010

[Icon][Icon]Backticks

• Post categories: Omni, FOSS, Technology, My Life, Programming, Helpful

Another of those little command-line things I've found incredibly useful lately: The backtick. It looks like a slanty apostrophe, and on a GB keyboard it's in the top-left corner: `

What this does it make it really simple to turn the output of one command into the input of another.

"So what?" you might say, "That's what pipes do."

But they don't!

Suppose that you want to get a list of files containing a certain string. Obviously, you'd use grep:

grep 'string' directory/ -r

This will look in every file in the directory of your choice and find you every single one that has 'string' in it.

Now suppose you want to figure out which was the most recently-edited of those files? Try doing that with a pipe :P

With backticks, it's easy: First, you need to get just the list of files, not their contents. This is easy with the -l switch:

grep 'string' directory/ -rl

That gives you the files you want. Now you just pass those files to ls

ls `grep 'string' directory/ -rl`

This is telling ls to list all the files returned by grep. However, it's still not doing what we want, because we need to pass ls a couple more options:

ls `grep 'string' directory/ -rl` -lt

Done. A list of files containing 'string' in chronological order of edits.

All courtesy of the backtick turning the output of one file into another.

Another example would be, what if you wanted all files which contained both 'string1' and 'string2' - a single grep can't help here. But two greps with backticks can:

grep 'string2' `grep 'string1' directory -lr` -l

This finds all files with string1 in, then passes those filenames to a second grep that then checks to see if those files also have string2.

Pretty cool, huh? :o)

4 comments

Dion Moult
Comment from: Dion Moult [Visitor] · http://thinkmoult.com/
It's 2:40AM and I'm reading (and commenting) about backticks.

Something is wrong here.
24/09/10 @ 19:43
Åke
Comment from: Åke [Visitor] · http://akeiexil.wordpress.com
Is this feature included in all major shells or is it mainly bash-like shells? Or is it even a POSIX-feature?
28/09/10 @ 11:42
oneandoneis2
Comment from: oneandoneis2 [Member] · http://geekblog.oneandoneis2.org/
Quite possibly, yes

;o)
28/09/10 @ 12:50
Daniel #
Comment from: Daniel # [Visitor]
You really made this backticking idea that I'm trying to understand for a few days easy...
Thank you for the post.
05/10/10 @ 13:40

Leave a comment


Your email address will not be revealed on this site.

Your URL will be displayed.
(Line breaks become <br />)
(Name, email & website)
(Allow users to contact you through a message form (your email will not be revealed.)
This is a captcha-picture. It is used to prevent mass-access by robots.
Please enter the characters from the image above. (case insensitive)
 

[Links][icon] My links

[Icon][Icon] I'm in the Perl newsletter again. I should try and write about some other language...
21/05/12

[Icon][Icon] Facebook Syndication Error
22/05/12

[Icon][Icon] I last listened to:
Johann Pachelbel - Canon in D major

[Icon][Icon] Most recent photo:
js.js

[Icon][Icon]About Me

[Icon][Icon]About this blog

[Icon][Icon]My LQ profile

[Icon][Icon]My /. profile

[Icon][Icon]My Wishlist

[Icon]MyCommerce

[FSF Associate Member]


May 2012
Mon Tue Wed Thu Fri Sat Sun
 << <   > >>
  1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31      

Search

User tools

XML Feeds

eXTReMe Tracker

Valid XHTML 1.0 Transitional

Valid CSS!

[Valid RSS feed]

free blog software