How to traverse sub. dirs? (307 Views)
Reply
Honored Contributor
Michael Steele_2
Posts: 4,934
Registered: ‎08-14-2002
Message 1 of 26 (307 Views)
Accepted Solution

How to traverse sub. dirs?

Hi Everyone: From a starting point of /u5/data/ I would like traverse down each subdirectory without crossing into another. For example, /u5/data/tomx/*, but not /u5/data/megax/*. This would be similar to find /u5/data/* -xdev. ( But not. )
Support Fatherhood - Stop Family Law
Honored Contributor
Patrick Wallek
Posts: 13,771
Registered: ‎06-21-2000
Message 2 of 26 (307 Views)

Re: How to traverse sub. dirs?

I assume you are using find?

Are there certain sub-dirs you want to go into and others you don't?

Can you not just specify 'find /u5/data/tomx -name filename'

Honored Contributor
Michael Steele_2
Posts: 4,934
Registered: ‎08-14-2002
Message 3 of 26 (307 Views)

Re: How to traverse sub. dirs?

Yes I am using find.
The problem is not limited to one file but 2.5 million files spread out across 49 sub dir.s under /u5/data. Each file requires that a command be run against it, i.e, find /u5/data/megax/* -xdev -exec chmod 777 {} \;
Support Fatherhood - Stop Family Law
Honored Contributor
Sajid_1
Posts: 604
Registered: ‎07-29-2002
Message 4 of 26 (307 Views)

Re: How to traverse sub. dirs?

hello,

may be this will:
# find /u5/data/megax -xdev -print
learn unix ..
Honored Contributor
Patrick Wallek
Posts: 13,771
Registered: ‎06-21-2000
Message 5 of 26 (307 Views)

Re: How to traverse sub. dirs?

If all files in a sub-dir need a chmod command run against them, why are you using find?

Why not something like:

# cd /u5/data
# chmod -R 777 megax

If you need the megax directory itself to be some other permissions then you could do:

# chmod 555 megax

Honored Contributor
Michael Steele_2
Posts: 4,934
Registered: ‎08-14-2002
Message 6 of 26 (307 Views)

Re: How to traverse sub. dirs?

Sorry. I should have not used chmod 777 as an example. In reality the command is the following: find /u5/data -xdev -exec net perms {} \;

And again, chmod -R is a recursive command that will cross over into other sub directories.

The solution to this problem is not to cross over into another sub dir from a starting point of: /u5/data/megax, for example.

I.e, find /u5/data -(something here) -exech net perms {} \;
Support Fatherhood - Stop Family Law
Honored Contributor
Sajid_1
Posts: 604
Registered: ‎07-29-2002
Message 7 of 26 (307 Views)

Re: How to traverse sub. dirs?

hello,

try this:
# find /u5/data/tomx -xdev -exec chmod 777 {} \;

You can also try chmod -R
learn unix ..
Esteemed Contributor
Gregory Fruth
Posts: 356
Registered: ‎05-14-1996
Message 8 of 26 (307 Views)

Re: How to traverse sub. dirs?

It's still not clear to me why the previous poster's
suggestion wouldn't work.

If you are trying to exclude certain directories from
the find command, you can use the ! operator:

find /u5/data \! -path '/u5/data/megax*' -exec ...

You can use the () and -a operators if you have
multiple directories you want to exclude.

HTH

Honored Contributor
Michael Steele_2
Posts: 4,934
Registered: ‎08-14-2002
Message 9 of 26 (307 Views)

Re: How to traverse sub. dirs?

find xdev only works on mount points. Substitute mount point with sub dir for the correct answer.

find /u5/data/!, etc. omits one of the 49 sub dir.s only.
Support Fatherhood - Stop Family Law
Honored Contributor
Sajid_1
Posts: 604
Registered: ‎07-29-2002
Message 10 of 26 (307 Views)

Re: How to traverse sub. dirs?

hello,

start your find from /u5/data/tomx and not /u5/data. this will search only that directory.
learn unix ..
Honored Contributor
Michael Steele_2
Posts: 4,934
Registered: ‎08-14-2002
Message 11 of 26 (307 Views)

Re: How to traverse sub. dirs?

Yes, that is closer to the solution. Now, how to traverse each of the other sub dirs in /u5/data?

The following doesn't work with net perms.

cat file | while read a
do
net perms /u5/data/$a/*
done

Where file = ls -d /u5/data/* > file
Support Fatherhood - Stop Family Law
Honored Contributor
Patrick Wallek
Posts: 13,771
Registered: ‎06-21-2000
Message 12 of 26 (307 Views)

Re: How to traverse sub. dirs?

OK, now I'm totally confused!

You have files in subdirs that you need to run the 'net perm' command against.

Which files in which subdirs need this?

Are all of the sub-dirs under /u5/data? So you have megax, tomx, etc.? Or are the subdirs under /u5/data/megax?

I'm really confused about exactly what you are trying to accomplish.
Honored Contributor
Patrick Wallek
Posts: 13,771
Registered: ‎06-21-2000
Message 13 of 26 (307 Views)

Re: How to traverse sub. dirs?

How about this:

You have a list of subdirs under /u5/data that you want to process and this list is in the file /u5/data/subdirs.

# cat /u5/data/subdirs
tomx
megax

You then write a script that reads from that file and does what you want:

#!/usr/bin/sh

for i in `cat /u5/data/subdirs`
do
find /u5/data/$i -exec net perm {} \;
done



That will run the find command for all dirs in /u5/data/subdirs execute whatever it is you need in each of the subdirs.
Honored Contributor
Darrell Allen
Posts: 1,550
Registered: ‎10-23-2000
Message 14 of 26 (307 Views)

Re: How to traverse sub. dirs?

Maybe you need something like:

cd /u5/data
# assuming there are only directories in /u5/data
for dir in *
do
find $dir -xdev -exec chmod 777 {} \;
done

That way you'll traverse all sub-dirs of /u5/data one at a time.

If you have files in /u5/data you could change the "for" statement to:
for dir in `ll | awk '/^d/ {print $NF}'`

Is this closer to what you need?

Darrell
"What, Me Worry?" - Alfred E. Neuman (Mad Magazine)
Honored Contributor
Michael Steele_2
Posts: 4,934
Registered: ‎08-14-2002
Message 15 of 26 (307 Views)

Re: How to traverse sub. dirs?

All the 2.5 million files are under /u5/data.

There are 49 sub dirs. under /u5/data.

How to traverse one of 49 sub dirs under /u5/data without crossing over into another sub dir?
Support Fatherhood - Stop Family Law
Esteemed Contributor
Jon Mattatall
Posts: 328
Registered: ‎08-22-2001
Message 16 of 26 (307 Views)

Re: How to traverse sub. dirs?

I'm not understanding this.

if you have /u5/data/1, /u5/data/2, ... /u5/data/400,
and you cd to /u5/data, then a simple find 2 will give you only the files/dirs under /u5/data/2/*.

If you just want files listed, not directories, use the type f option.

ie: find 2 -type f (pwd=/u5/data)

Jon
A little knowledge is dangerous - none is absolutely terrifying!!!
Honored Contributor
Patrick Wallek
Posts: 13,771
Registered: ‎06-21-2000
Message 17 of 26 (307 Views)

Re: How to traverse sub. dirs?

Are you saying that the subdirs have subdirs?

Like /u5/data/megax/someotherdir

and you DON'T want to go into the someotherdir directory under megax?
Honored Contributor
Sajid_1
Posts: 604
Registered: ‎07-29-2002
Message 18 of 26 (307 Views)

Re: How to traverse sub. dirs?

hello,

you can write a script for doing this. first get a list of all sub-dirs, then:

for s in `cat sub_dir_list`
do
find /u5/data/$i -depth -exec net perms {} \;
done

This will do each dir. seperately and if you want you can add more lines for more functions.
learn unix ..
Honored Contributor
Sajid_1
Posts: 604
Registered: ‎07-29-2002
Message 19 of 26 (307 Views)

Re: How to traverse sub. dirs?

oops.. it should be:

find /u5/data/$s ..

actually, are you trying to give different settings for different sub-dirs?
learn unix ..
Honored Contributor
Michael Steele_2
Posts: 4,934
Registered: ‎08-14-2002
Message 20 of 26 (307 Views)

Re: How to traverse sub. dirs?

The closest was Darrell and Sajid which are restatements of each other. Darrel had the following for instance.

cd /u5/data
# assuming there are only directories in /u5/data

for dir in *
do
find $dir -exec net perms {} \;
done

But I'm going to have to substitute an inner loop for the find command above in order to break apart the data and write output to 49 separate files for all 49 dirs.

I think that will do it.

Thanks
Support Fatherhood - Stop Family Law
Honored Contributor
Wodisch_1
Posts: 468
Registered: ‎09-13-2000
Message 21 of 26 (307 Views)

Re: How to traverse sub. dirs?

Hi,

well, how about using the feature of "find" to tell multiple starting directories?
Since that would be less than 49 if I read your posting correctly, you would go by with a single "find" command:

cd /u5/data
find tom* -exec net perms {} ";"

You could use any list or wildcard pattern you want!
If you don't like the subdirs starting with "m", that would be

find [^m]* -exec net perms {} ";"

HTH,
Wodisch
Honored Contributor
Darrell Allen
Posts: 1,550
Registered: ‎10-23-2000
Message 22 of 26 (307 Views)

Re: How to traverse sub. dirs?

For output to 49 different files, perhaps:

for dir in *
do
find $dir -exec net perms {} \; >/some_dir_path/output.$dir
done

Darrell
"What, Me Worry?" - Alfred E. Neuman (Mad Magazine)
Honored Contributor
Michael Steele_2
Posts: 4,934
Registered: ‎08-14-2002
Message 23 of 26 (307 Views)

Re: How to traverse sub. dirs?

Yes Darrell that is what I've been looking for but net perms has thrown in an additional twist. Here is the net perms syntax:
"net perms c:/u5/data" so your answer fails when c: is added like so "find /u5/data -exec net perms c: {} \;" So I'm back to embedded loops unless you also know of a way to catenate the string "c:" in front of a find result?
Support Fatherhood - Stop Family Law
Honored Contributor
Bill Hassell
Posts: 14,205
Registered: ‎05-29-2000
Message 24 of 26 (307 Views)

Re: How to traverse sub. dirs?

If you give find a starting directory, then it will traverse through that directory. However, you did not give find a single directory. Instead, you combined the shell's built-in filename globbing with find (ie, the *). The * is not seen by the find command at all. Instead, find saw a long list of names in the directory. To see what find is seeing, surround the entire line in "" and put echo in front:

echo "find u5/data/tomx/*"

The find command is getting help from the shell which is not what you intended. Using * in find almost always causes uinexpected problems and find does not need * because the default is to identify everything in the current directory and compare that to whatever options you've added (like -type f or similar).
As a rule, never use * in a find command unless you know what the effect is (hint: use the echo above)

Honored Contributor
Darrell Allen
Posts: 1,550
Registered: ‎10-23-2000
Message 25 of 26 (307 Views)

Re: How to traverse sub. dirs?

I'm afraid I'm stumped by that too. I believe I'd also capture the output to a file and go from there. Like you say, use an embedded loop.

Any way, welcome to the ITRC forums. They've been very helpful to me. Hope you find them useful also. They become addictive after awhile.

Darrell
"What, Me Worry?" - Alfred E. Neuman (Mad Magazine)
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation.