From owner-freebsd-questions Wed Mar 6 22:19:44 2002 Delivered-To: freebsd-questions@freebsd.org Received: from post-20.mail.nl.demon.net (post-20.mail.nl.demon.net [194.159.73.1]) by hub.freebsd.org (Postfix) with ESMTP id A4FCF37B433 for ; Wed, 6 Mar 2002 22:19:29 -0800 (PST) Received: from [212.238.194.207] (helo=mailhost.raggedclown.net) by post-20.mail.nl.demon.net with esmtp (Exim 3.35 #2) id 16irFQ-000NNW-00 for freebsd-questions@freebsd.org; Thu, 07 Mar 2002 06:19:29 +0000 Received: from angel.raggedclown.net (angel.raggedclown.intra [192.168.1.7]) by mailhost.raggedclown.net (Ragged Clown Mail Gateway [buffy]) with ESMTP id E90A413040 for ; Thu, 7 Mar 2002 07:19:27 +0100 (CET) Received: by angel.raggedclown.net (Ragged Clown Host [angel], from userid 1005) id 947FA22597; Thu, 7 Mar 2002 07:19:26 +0100 (CET) Date: Thu, 7 Mar 2002 07:19:26 +0100 From: Cliff Sarginson To: freebsd-questions@freebsd.org Subject: Re: question about the 'find' command Message-ID: <20020307061926.GC2142@raggedclown.net> References: <20020307020236.7623.qmail@web20110.mail.yahoo.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20020307020236.7623.qmail@web20110.mail.yahoo.com> User-Agent: Mutt/1.3.27i Sender: owner-freebsd-questions@FreeBSD.ORG Precedence: bulk List-ID: List-Archive: (Web Archive) List-Help: (List Instructions) List-Subscribe: List-Unsubscribe: X-Loop: FreeBSD.ORG 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 -- To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-questions" in the body of the message