Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 7 Mar 2002 07:19:26 +0100
From:      Cliff Sarginson <csfbsd@raggedclown.net>
To:        freebsd-questions@freebsd.org
Subject:   Re: question about the 'find' command
Message-ID:  <20020307061926.GC2142@raggedclown.net>
In-Reply-To: <20020307020236.7623.qmail@web20110.mail.yahoo.com>
References:  <20020307020236.7623.qmail@web20110.mail.yahoo.com>

next in thread | previous in thread | raw e-mail | index | archive | help
On Wed, Mar 06, 2002 at 06:02:36PM -0800, Bsd Neophyte wrote:
> 
> my unix text talks about the 'find' command... it further goes to talk
> about an "action" used with the find command.
> 
> I am completely confused as to what the {} do with the find comand.
> 
> the explanation is this: "A set of braces, {}, delimits where the file
> name is passed to the command from the prceding expressions."
> 
> Now what does this mean?  It makes no sense to me.
> 
> an example they give is the following:
> 
> $ find ~ -name core -exec rm {} \;
> 
Find is one of the most useful programs you will ever use, unfortunately
it is more or less unique in it's usage. It has also undergone changes
over the years, and has some marginal differences on different Unix
systems (but don't worry about that :).

The {} is find-ese, for the filename that is the result of the find
command, so you can use {} as an argument to something you want to
"exec", i.e. do, on that filename. This exec "argument" must be
terminated by a ";", which means something to the shell (it seperates 
shell commands) so it has to be escaped with the "\", this tells the
shell to pass the ";" as-is, and not treat it normally.

So to break the command example down:

find ~
        start "finding" from ~, i.e. the home directory of the current
user, this will cause by default recursion through sub-directories as
well.

-name core
        look for files called "core"

-exec rm {}
        execute the "rm" command giving it the pathname of the file
found as an argument, so say if your home directory is /home/mspiggy,
and in sub-directory "muppet" there is a file called "core", this will
expand to
      rm /home/mspiggy/muppet/core

\;
        as I explained above this terminates the exec command list.

So this command will find all files called core in and below your home
directory and delete them.

There are a staggering number of things you can do with find, so it is
really worth learning, the manual page will probably scare the pants of
you, but give it a go. I suggest while experimenting you avoid "rm" as
the exec command (it is a bit of a dangerous command to use in an
example).

Here is a real-world example from one of my scripts:

/usr/bin/find /backup/angel -name \*.bz2 -mtime +7

This finds all files with any name ending in ".bz2" that were
modified more than 7 days ago. It starts at directory "/backup/angel"
and works down through all sub-directories. There is no exec clause here
so the default behaviour is to print the filename of the files it finds
(it's default behaviour used to be to do nothing..except set an exit
status .. which was the killer when learning find in the bad old days).
Note again that the "*" is significant to the shell and so should be
escaped (or the whole expression quoted). 

Hope that helps.

-- 
Regards
   Cliff Sarginson -- <csfbsd@raggedclown.net>

To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-questions" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20020307061926.GC2142>