[Courses][Linux comands] Finding things with locate, find, and grep
Carla Schroder
carla at bratgrrl.com
Tue Feb 17 23:30:35 EST 2004
Finding things on Linux can get incredibly complex, especially if you ask a
regular expression guru for help. By the time they're done helping you, you
feel inferior and too lowly to be worthy of Linux. While being able to find a
specific comma on a 160-gigabyte hard drive is fun, or a particular instance
of the word "the," you can actually do quite a lot with the three basic Linux
finding-things utilities: locate, find, and grep. Locate and find search for
files, and grep searches for text in files.
Locate is the easiest. It maintains its own database of files on your system,
so it's blazing fast. Some distros install with a cron job that rebuilds the
locate database regularly. However you do it, it must be updated
periodically:
# updatedb
Finding files is like so easy:
$ locate filename
Do a case-insensitive search:
$ locate -i filename
It is typical that locate will return a skillion hits. Locate and grep
together are the Dynamic Duo:
$ locate -i tuxracer | grep -i readme
/usr/share/doc/tuxracer-data/README
/usr/share/doc/tuxracer/README
/usr/share/doc/tuxracer/README.Debian
/usr/share/games/tuxracer/music/readme
Now suppose you need to find a file with a particular bit of text in it. grep
is wicked cool for searching dmesg:
$ dmesg | grep -i usb
usb.c: registered new driver usbdevfs
usb.c: registered new driver hub
host/usb-uhci.c: $Revision: 1.275 $ time 20:17:46 Aug 3 2003
host/usb-uhci.c: High bandwidth mode enabled
host/usb-uhci.c: USB UHCI at I/O 0xd800, IRQ 11
host/usb-uhci.c: Detected 2 ports
Blah blah, and there are many more lines like this. Grep prints the whole line
each time it finds your search string. dmesg is a special case, as it is a
command, and not just a text file. For example, to search an ordinary text
file, such as the XFree log, for references to 'glcore', this is the normal
grep syntax:
# grep -iw glcore XFree86.0.log
(II) LoadModule: "GLcore"
(II) Module GLcore: vendor="The XFree86 Project"
(II) Loading sub module "GLcore"
(II) LoadModule: "GLcore"
grep - options - search string - file to search
The -w flag means 'whole word search.' Which is really handy, because
otherwise grep returns every single line that contains your search string.
Here is the kewlest grep trick of all: searching bales of files for a word or
string or regexp. CD to the directory you want to search, then:
$ grep -lir "word" *
Lesson5.html
-l means print only the filenames containing your search term, -r is
recursive, and * means all files. If you omit the -l flag, it prints the
filenames and the lines containing your search term:
$ grep -ir swap *
Lesson5.html:chuck the data, and none of the other options matter. A swap
device is
Lesson5.html:initialised with <code>mkswap /dev/something</code>, and can be
Lesson5.html:manually activated with <code>swapon /dev/something</code>,
although you'll
Find is wicked cool. Do this to search for a file named arrow.jpg:
$ find / -name arrow.jpg
That tells find to start its search from /. Maybe you only know part of the
filename, so give it some wildcards. This searches your home directory for
filenames that start with arrow, and can end with anything:
$ find ~ -name 'arrow*'
/home/carla/downloads/neverball-0.25.11/data/mtrl/arrow-green.jpg
/home/carla/downloads/tuxpaint-0.9.12/src/mouse/arrow.xbm
/home/carla/downloads/tuxpaint-0.9.12/src/mouse/arrow-mask.xbm
Maybe you only want 'arrow' images in .xbm format:
$ find ~ -name 'arrow*.xbm'
/home/carla/downloads/tuxpaint-0.9.12/src/mouse/arrow.xbm
/home/carla/downloads/tuxpaint-0.9.12/src/mouse/arrow-mask.xbm
Or all the .xbm files in the home directory:
$ find ~ -name '*.xbm'
A cool thing with find is tracking down orphaned files, after you've booted a
user off the system. You need to locate all the files that belonged to that
user. Oops, their account is long gone, so you can't search by login name. No
problem, search on their UID:
# find / -uid 1005
If you're searching system directories as a lowly unprivileged user, you'll
generate bales of error messages. These are like, so depressing, so they must
be suppressed, or redirected into the bitbucket, hahaha:
# find / -uid 1005 2>/dev/null
OK that's enough for this week!
--
~~~~~~~~~~~~~~~~~~~~~~~~~
Carla Schroder
www.tuxcomputing.com
this message brought to you
by Libranet 2.8 and Kmail
~~~~~~~~~~~~~~~~~~~~~~~~~
More information about the Courses
mailing list