<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD> <META http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <META NAME="GENERATOR" CONTENT="lfparser_2.32"> <META NAME="LFCATEGORY" CONTENT="UNIXBasics"> <link rel="icon" href="../../common/images/lf-16.png" type="image/png"> <TITLE>lf64, UNIXBasics: Finding files</TITLE> <style type="text/css"> <!-- td.top {font-family: Arial,Geneva,Verdana,Helvetica,sans-serif; font-size:12 } pre { font-family:monospace,Courier } p.cl { color:#EE9500 } a.nodec { text-decoration:none } p.trans { font-size:8pt; text-align:right } p.clbox { width:50%; alignment:center; background-color:#FFD700; border-style:none; border-width:medium; border-color:#FFD700; padding:0.5cm; text-align:center } p.code { width:80%; alignment:center; background-color:#aedbe8; border-style:none; border-width:medium; border-color:#aedbe8; padding:0.1cm; text-align:left } p.foot { background-color:#AAAAAA; color:#FFFFFF; border-style:none; border-width:medium; border-color:#AAAAAA; padding:0.5cm ; margin-top:0.1cm; margin-right:1cm; margin-left:1cm; text-align:center } .mark { background-color:#e6e6ff } --> </style> </HEAD> <BODY bgcolor="#ffffff" text="#000000"> <!-- this is generated html code. NEVER use this file for your translation work. Instead get the file with the same article number and .meta.shtml in its name. Translate this meta file and then use lfparser program to generate the final article --> <!-- lfparser can be obtained from http://www.linuxfocus.org/~guido/dev/lfparser.html --> <!-- this is used by a number of tools: =LF=AUTHOR: Guido Socher =LF=CAT___: UNIXBasics =LF=TITLE_: Finding files =LF=NUMBER: 64 =LF=ANAME_: article64.shtml --> <!-- 2pdaIgnoreStart --> <!-- start navegation bar, old style --> <MAP name="top"> <AREA shape="rect" coords="367,9,418,30" href="../index.shtml"> <AREA shape="rect" coords="423,9,457,30" href="../map.html"> <AREA shape="rect" coords="463,9,508,30" href="../indice.html"> <AREA shape="rect" coords="514,9,558,30" href="../Search/index.html"> </MAP> <MAP name="bottom"> <AREA shape="rect" coords="78,0,163,15" href="../News/index.html"> <AREA shape="rect" coords="189,0,284,15" href="../Archives/index.html"> <AREA shape="rect" coords="319,0,395,15" href="../Links/index.html"> <AREA shape="rect" coords="436,0,523,15" href="../aboutus.html"> </MAP> <!-- IMAGE HEADER --> <CENTER> <IMG src="../../common/images/Topbar-en.gif" width="600" height="40" border="0" alt="[Top bar]" ismap usemap="#top" ><BR> <IMG src="../../common/images/Bottombar-en.gif" width="600" height="21" border="0" alt="[Bottom bar]" ismap usemap="#bottom"> </CENTER> <!-- stop navegation bar, old style --> <!-- SSI_INFO --> <!-- tr_staticssi include virtual --> <!-- tr_staticssi exec cmd --> <!-- addedByLfdynahead ver 1.1 --><TABLE ALIGN="right" border=0><TR><TD ALIGN="right"><FONT SIZE="-1" FACE="Arial,Helvetica">This article is available in: <A href="../../English/September1998/article64.shtml">English</a> <A href="../../Castellano/September1998/article64.html">Castellano</a> <A href="../../Deutsch/September1998/article64.html">Deutsch</a> <A href="../../Francais/September1998/article64.html">Francais</a> <A href="../../Nederlands/September1998/article64.html">Nederlands</a> <A href="../../Portugues/September1998/article64.html">Portugues</a> <A href="../../Russian/September1998/article64.html">Russian</a> <A href="../../Turkce/September1998/article64.html">Turkce</a> <A href="../../Korean/September1998/article64.html">Korean</a> </FONT></TD></TR></TABLE><br> <!-- SSI_INFO STOP --> <!-- 2pdaIgnoreStop --> <!-- SHORT BIO ABOUT THE AUTHOR --> <TABLE ALIGN=LEFT BORDER=0 WIDTH="190" summary="about the author"> <TR> <TD> <img src="../../common/images/Guido-S.gif" alt= "[Photo of the Author]" height="164" width="173"> <BR>by Guido Socher <br> <small><guido(at)bearix.oche.de></small> <BR><BR> <I>About the author:</I><BR> Guido loves linux because it is a free system and it is also a lot of fun to work with people from the Linux community all over the world. He spends his spare time with his girl-friend, listens to BBC World Service radio, rides his bike through the countryside and enjoys playing with Linux. <!-- TRANSLATED TO en --> <!-- TRANSLATED TO STOP --> <BR><i>Content</i>: <UL> <LI><A HREF="#64lfindex0">Finding files by name</A></LI> <LI><A HREF="#64lfindex1">Getting an overview over the file system</A></LI> <LI><A HREF="#64lfindex2">Finding files by content (searching for text strings in files)</A></LI> </UL> </TD></TR></TABLE> <!-- HEAD OF THE ARTICLE --> <br> <H2>Finding files</H2> <img src="../../common/images/illustration3.gif" alt= "[Illustration]" hspace="10" height="100" width="100"> <!-- ABSTRACT OF THE ARTICLE --> <P><i>Abstract</i>: <P> This aricle gives an introduction on how to find files by name and by content. </P> <HR size="2" noshade align="right"><BR> <!-- BODY OF THE ARTICLE --> <A NAME="64lfindex0"> </A> <H2>Finding files by name</H2> <p>Probably you can remember this problem: You had some kind of file and you can't remember where you put it.</p> <p>This is where the <tt>find</tt> command comes handy. How to use it? <tt>find</tt> comes of course with a large man page but let's just look at some "normal cases". To traverse the directory tree starting with the current directory and search for a file named lostfile.txt:</p> <br clear="all"> <p class="code"> find . -name lostfile.txt -print </p> <p><tt>find</tt> takes also wild cards. Remember to quote the wild-cards otherwise the shell tries to expand them before they get handed over to find. Here is an example:</p> <p class="code"> find . -name "lost*" -print </p> <p>This command can be quite slow when you need to search in a large directory tree. Here the <tt>locate</tt> command can help. It does not really search directly for a file in the file system. It searches a database. This is a lot faster but the database might be out of date. In some distributions the <tt>located</tt> database is updated every night but you can of course run the <tt>updatedb</tt> command manually from time to time in order to update the database. <tt>locate</tt> does substring searches.</p> <p class="code"> locate lostfile</p> <p>This will locate the files lostfile.txt or mylostfile.txt etc...</p> <p>So far we were searching for files where we knew quite exactly what the file name was. Maybe the file name was not <i>lostfile.txt</i> but <i>lastfile.txt</i> or <i>leastfile.txt</i> or <i>lostfiles.txt</i> or <i>Lotsfile.txt</i> or <i>lostfile.text</i> and you can't remember the name exactly. How can you still find the file? In this case you could use a fault tolerant file find utility called ftff. ftff searches for the file and allows for a number of "spelling errors". This command would find all these slightly "misspelled" file names:</p> <p class="code"> ftff lostfiles.txt</p> <p>The number of allowed spelling errors depends on the file length of the file but can also be set with the -t option. To allow a maximum of 2 errors and use also a wild-card just type:</p> <p class="code">ftff -t2 "lostfiles*"</p> <p>Ftff is a program of my own design and is part of a package called whichman-1.5 that can be downloaded from</p> <a href= "http://linuxfocus.org/~guido/">linuxfocus.org/~guido/</a> <p>Some times you would like to find all files in the directory tree that do not contain a certain string. For example all files except .o and .c. Here are a couple of possibilities to do it:</p> <p class="code">find . ! -name "*.o" ! -name "*.c" -print<br> find . -print | fgrep -v '.o' | fgrep -v '.c'<br><br> if you have the the gnu version of find: <br> find | fgrep -v '.o' | fgrep -v '.c'<br> find | egrep -v '\.[oc]' </p> <A NAME="64lfindex1"> </A> <H2>Getting an overview over the file system</H2> <p>Sometimes you would like to get an overview about the file system in front of you. E.g. you get a new CD and would like to see what's on it. You could just use <i>ls -R</i>. Personally I prefer for readability one of the following. tree ( <a href= "ftp://sunsite.unc.edu/pub/Linux/utils/file/">sunsite.unc.edu/pub/Linux/utils/file/tree-1.2.tgz</a> ) draws a tree diagram.</p> <p class="code">tree<br><br> or with long file names: <br> tree -fF</p> <p>Then there is of course the good old find. The gnu version of find, which comes usually with Linux, has also the possibility to change the print format to print e.g the file size with the name:</p> <p class="code">find . -ls<br> find . -print<br><br> or with gnu find: <br> find<br> find . -printf "%7s %p\n"</p> <p>There is also a small perl wrapper around the ls command that does similar things. It can be downloaded <a href= "../../common/src/lsperl.gz">here: lsperl.gz</a>. You can probably find much more file viewing tools but this is for most cases more than sufficient.</p> <A NAME="64lfindex2"> </A> <H2>Finding files by content (searching for text strings in files)</H2> <p>The standard utilities for searching text strings in files are grep/egrep for regular expression searches and fgrep to search for literal strings. To search for an expression in all files in the current directory just type:</p> <p class="code">egrep -i "search expression" *</p> <p>To search for strings in all files down the directory tree you can combine the find or other file name search commands with e.g egrep. This can be done in several ways:</p> <p class="code">egrep -i "expression" `find . -type f -print`<br> find . -type f -exec egrep -i "expression" /dev/null {} \;<br> find . -type f -print | xargs egrep -i "expression"</p> <p>If you find this hard to remember then you can just use a small shell script downloadable form <a href= "../../common/src/grepfind.gz">here: grepfind.gz</a>. The script makes also sure that non-printable characters are removed in case you egrep accidentally through a binary file.</p> <p>A very interesting search program is agrep. agrep works basically like egrep but does fault tolerant searches. This way you can also find slightly mis-spelled words. To search for an expression and allow for a maximum of 2 spelling errors you use:</p> <p class="code">agrep -i -2 "search exprission" *</p> <p>The agrep program can be downloaded form sunsite <a href= "ftp://sunsite.unc.edu/pub/Linux/utils/text/agrep-2.04.tar.Z">sunsite.unc.edu/pub/Linux/utils/text/agrep-2.04.tar.Z</a> or the original site <a href= "ftp://ftp.cs.arizona.edu/agrep/">ftp://ftp.cs.arizona.edu/agrep/</a></p> <p>Then there is also <tt>glimpse</tt>. glimpse is a very powerful search utility. It uses a concept similar to locate. First a database needs to be build, but then searches are very fast. To build a search index for the contents of all files starting from the current directory:</p> <p class="code">glimpseindex .</p> <p>After that you can search for a string in all files that were previously indexed</p> <p class="code">glimpse -i -2 "search exprission"</p> <p>glimpse is also fault tolerant (as agrep) and the -2 allows for two errors. glimpse is available from <a href= "http://glimpse.cs.arizona.edu/">http://glimpse.cs.arizona.edu/</a></p> <hr noshade> <p>There are many search utilities available for Unix and Linux especially. This article can be by no means complete. If you are interested in more tricks and utilities like this then have a look at the .lsm files under <a href= "ftp://sunsite.unc.edu/pub/Linux/utils">http://sunsite.unc.edu/pub/Linux/utils</a></p> <p>Have fun, happy searching.</p> <!-- vim: set sw=2 ts=2 et tw=74: --> <!-- 2pdaIgnoreStart --> <HR size="2" noshade> <!-- ARTICLE FOOT --> <CENTER><TABLE WIDTH="98%" summary="footer"> <TR><TD ALIGN=CENTER BGCOLOR="#9999AA" WIDTH="50%"> <A HREF="../../common/lfteam.html">Webpages maintained by the LinuxFocus Editor team</A> <BR><FONT COLOR="#FFFFFF">© Guido Socher, <a href="../../common/copy.html">FDL</a> <BR><a href="http://www.linuxfocus.org">LinuxFocus.org</a></FONT> </TD> <TD BGCOLOR="#9999AA"> <!-- TRANSLATION INFO --> <font size=2>Translation information:</font> <TABLE summary="translators"> <tr><td><font size="2">en --> -- : Guido Socher <small><guido(at)bearix.oche.de></small></font></td></tr> </TABLE> </TD> </TR></TABLE></CENTER> <p><font size=1>2002-10-20, generated by lfparser version 2.32</font></p> <!-- 2pdaIgnoreStop --> </BODY> </HTML>