From owner-freebsd-questions Mon Dec 17 3:44: 1 2001 Delivered-To: freebsd-questions@freebsd.org Received: from topaz.mdcc.cx (topaz.mdcc.cx [212.204.230.141]) by hub.freebsd.org (Postfix) with ESMTP id 610DE37B41D for ; Mon, 17 Dec 2001 03:43:54 -0800 (PST) Received: from k7.mavetju.org (topaz.mdcc.cx [212.204.230.141]) by topaz.mdcc.cx (Postfix) with ESMTP id 3D2E62B78A; Mon, 17 Dec 2001 12:43:50 +0100 (CET) Received: by k7.mavetju.org (Postfix, from userid 1001) id 99F923D7; Mon, 17 Dec 2001 22:43:44 +1100 (EST) Date: Mon, 17 Dec 2001 22:43:44 +1100 From: Edwin Groothuis To: rene@xs4all.nl Cc: questions@freebsd.org Subject: Re: /bin/sh script to walk through a filetree? [shell, example, file, directory, tree] Message-ID: <20011217224344.M724@k7.mavetju.org> References: <20011217122028.K21241@xs4all.nl> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.2.5i In-Reply-To: <20011217122028.K21241@xs4all.nl>; from rene@xs4all.nl on Mon, Dec 17, 2001 at 12:20:28PM +0100 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 Mon, Dec 17, 2001 at 12:20:28PM +0100, rene@xs4all.nl wrote: > Hi. I need to do 'something' with all or some files in a directory tree. Can > someone perhaps point me to a skeleton example that shows me how to walk > recursively through a filetree, listing all files/directories in it? The easiest way is: "find .". To distinguish between files and directories: "find . -type f" and "find . -type d". To do something with it, if the argument list isn't becoming too big, is to pipe it through xargs: "find . -type f | xargs rm" to remove all the files. Now a little bit more difficult with recursion: #!/bin/sh ============================= go ( ) { local DIR local entry DIR=$1 echo "now in $DIR" cd "$DIR" for entry in *; do if [ -d "$entry" ]; then go "$DIR/$entry" fi if [ -f "$entry" ]; then echo "File $entry" fi done cd .. echo "back to .." } go . ============================= good luck Edwin -- Edwin Groothuis | Personal website: http://www.MavEtJu.org edwin@mavetju.org | Interested in MUDs? Visit Fatal Dimensions: ------------------+ http://www.FatalDimensions.org/ To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-questions" in the body of the message