Find and Grep 101

Find and Grep are perhaps the most used command line tools for the Linux user or administrator. Terse but powerful, these two commands will allow you to search through files and their contents by almost any imaginable attribute or filter criteria: file name, date modified, occurrence of the some specific word in a file, etc. Combined with a couple of other standard unix utilities, you can automate and process modifications over a number of files that match your search.

Here are two blog posts by Eric Wendelin which nicely illustrate the basics of these two commands:

Find is a Beautiful Tool
Grep is a Beautiful Tool

There are a number of other great unix utilities for file search, but knowing how to use find and grep is fundamental, as these two utilities can be found on the most basic build of every unix-like machine you come across.

Got a favorite command line hack that uses find or grep? Drop it on us in the comments.


Recent Entries

Comments

Oldest comments listed first.

Posted by: Phaedrus on July 14, 2008 at 4:36 PM

#!/bin/bash

# Seeing as I use a "podcatcher" which
# does not support variable speed playback,
# I have writ this script to play my "podcasts"
# in random order.
# Each followed by the option to delete.

# Those "'s above are indicative of my
# general distaste of all things "pod".
# So there.

# To prevent 'for' from booching on spaces
IFS=$'\n\t'

# Where to work
PODCASTDIR=/mnt/hdg2/share/podcasts/
PODCASTLIST=~/.podlist

# Player of choice
PLAYER=/usr/bin/mplayer

# Why ... not
function die()
{
echo "$@"
exit 1
}


# Build the list
find "$PODCASTDIR" -iname "*.mp3" -o -iname "*.ogg" | sort -R > "$PODCASTLIST"

# Parse and play
for i in $(cat "$PODCASTLIST"); do

# Play or puke
$PLAYER $i || die "Someone set up us the bomb!"

# Prompt and purge
echo -e "\nWould you like to delete the file you just heard:"
read -p "$i? "
echo $REPLY | grep -qi y && /bin/rm -i "$i" || echo -e "\nVery well, moving on."

# Prompt and procede
read -p "Would you like another? "
echo $REPLY | grep -qi y && echo -e "\nHit me!\n" || die "Right on. Until next time."
done

# Inform user of sound shortage
echo "So, uh..."
sleep 3
echo "It seems that you have finished."
sleep 1
echo "Well then."
sleep 2
die "Peace."

# I'm pretty sure that posting this here will bork the line breaks, so don't expect a copy/paste to work as is.
# I can email the original on request.
# Some minor language unniceties have been cleaned up for public consumption.
# Hope somebody can make use of this, or let me know where it might be improved.
# phaedrus
# @
# gmx.com


Posted by: Dave on July 14, 2008 at 7:29 PM

I hit the Unix Grymoire all the time when I need to get started with a find, sed, or awk recipe.

http://www.grymoire.com/Unix/


Posted by: Anonymous on July 27, 2008 at 7:31 PM

real simple:

alias hg='history|grep'

so all you would need to do to search through your history for all of the ssh logins would be

$ hg ssh

it's such a simple and useful alias that it bothers me that perhaps there is something similar out there already that I've overlooked. All you need to do to find a command that you have already issued is to remember one piece of the string.


Leave a comment


Subscribe to MAKE!Subscribe to MAKE Magazine!

Subscribe today, save 42% and get web access to MAKE free. MAKE Digital Edition is available only to subscribers.

$34.95 / 1 year
(4 Quarterly Issues)

Subscribe now


Void your warranty, violate a user agreement, fry a circuit, blow a fuse, poke an eye out. Make: The risk-takers, the doers, the makers of things... Welcome to Make: Online!


CRAFT Maker Shed Maker Faire MAKE television
Holiday Gift Guides from MAKE
Gifts for Dads
Science and Chemistry
Gifts Under $20
More guides: Santa Claus Machines, Geek Toys for Grown Up Girls & Boys


Check out all of the episodes of Make: television

Alex Rider Dream Gadget Contest
Make: Science Room

Connect with MAKE

Be a MAKE fan on Facebook MAKE on Facebook
Visit our Facebook page and become a fan of MAKE!
MAKE on Twitter MAKE on Twitter
Follow our MAKE tweets!
MAKE Flickr Pool MAKE on Flickr
Join our MAKE Flickr Pool!
    make_tips on Twitter




    Maker SHED

    Advertise here with FM.

    Why advertise on MAKE?
    Read what folks are saying about us!

    Click here to advertise on MAKE!



    Subscribe to MAKE Magazine!

    Make: Online authors!

    Gareth BranwynGareth Branwyn
    Senior Editor


    Phillip TorronePhillip Torrone
    Senior Editor
    | AIM | Twitter


    Becky SternBecky Stern
    Associate Editor
    | AIM | Twitter


    Marc de VinckMarc de Vinck
    Contributing Writer
    | AIM | Twitter


    John ParkJohn Park
    Contributing Writer
    | Twitter


    Sean RaganSean Ragan
    Contributing Writer
    | Twitter


    Matt MetsMatt Mets
    Contributing Writer
    | AIM | Twitter


    Dale DoughertyDale Dougherty
    Editor & Publisher
    | Twitter


    Shawn ConnallyShawn Connally
    Managing Editor
    | Twitter


    Goli MohammadiGoli Mohammadi
    Associate Managing Editor

    Kip KayKip Kay
    Weekend Projects
    | AIM | Twitter


    Collin CunninghamCollin Cunningham
    Contributing Writer
    | AIM | Twitter

    Adam FlahertyAdam Flaherty
    Contributing Writer
    | AIM | Twitter



    More contributors: Mark Frauenfelder (Editor-in-Chief, MAKE magazine), Kipp Bradford (Technical Consultant/Writer), Chris Connors (Education), Diana Eng (Guest Author), Peter Horvath (Intern), Brian Jepson (O'Reilly Media), Robert Bruce Thompson (Science Room)

    Suggest a Site!

    Current Podcast

    itunesdl.gif Weekend Project: Beetlebot Simple robot from your parts bin that avoids obstacles. Thanks go to Jerome Demers for the original article in MAKE, Volume 12. To download the Beetlebot video, click here or subscribe in iTunes. Check out the complete Beetlebot article... More...

    Get the Make: Online sent via email
    Enter your email to receive Make: Online each day:



    MAKE Fascination video series brought to you by Dow

    Make: Education
    MAKE: en EspaƱol MAKE: Japan
    Important please read


    Subscribe to MAKE Magazine!

    Recent Posts from the Craft: Blog