Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 17 Dec 2001 22:43:44 +1100
From:      Edwin Groothuis <edwin@mavetju.org>
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>
In-Reply-To: <20011217122028.K21241@xs4all.nl>; from rene@xs4all.nl on Mon, Dec 17, 2001 at 12:20:28PM %2B0100
References:  <20011217122028.K21241@xs4all.nl>

next in thread | previous in thread | raw e-mail | index | archive | help
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




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