Getting directories at a certain depth

Getting directories at a certain depth

Post by D. Alvara » Sat, 24 Jul 2004 06:13:56



Hi again, Using ksh on Solaris 8, I would like to retrieve all the
directories that are a certain depth away from my home directory.
Let's say my home directory is

/home/dave

I am interested in retrieving only the directories that are 3 away
from my home directory.  So let's say what the directories that
existed were

/home/dave/2004/ss/CTF/temp
/home/dave/2004/tt/
/home/dave/2004/yy/PPC/child
/home/dave/2004/yy/PPC/child/grandchild

I would only want to retrieve the "/home/dave/2004/ss/CTF/temp" and
"/home/dave/2004/yy/PPC/child" since they are 3 away from
"/home/dave".

As usual, your help is greatly appreciated, - Dave

 
 
 

Getting directories at a certain depth

Post by foo » Sat, 24 Jul 2004 06:36:05



> Hi again, Using ksh on Solaris 8, I would like to retrieve all the
> directories that are a certain depth away from my home directory.
> Let's say my home directory is

> /home/dave

> I am interested in retrieving only the directories that are 3 away
> from my home directory.  So let's say what the directories that
> existed were

> /home/dave/2004/ss/CTF/temp
> /home/dave/2004/tt/
> /home/dave/2004/yy/PPC/child
> /home/dave/2004/yy/PPC/child/grandchild

> I would only want to retrieve the "/home/dave/2004/ss/CTF/temp" and
> "/home/dave/2004/yy/PPC/child" since they are 3 away from
> "/home/dave".

> As usual, your help is greatly appreciated, - Dave

With GNU find:
        find /home/dave --maxdepth 3 -print

 
 
 

Getting directories at a certain depth

Post by Chris F.A. Johnso » Sat, 24 Jul 2004 07:00:37



Quote:> Hi again, Using ksh on Solaris 8, I would like to retrieve all the
> directories that are a certain depth away from my home directory.
> Let's say my home directory is

> /home/dave

> I am interested in retrieving only the directories that are 3 away
> from my home directory.  So let's say what the directories that
> existed were

> /home/dave/2004/ss/CTF/temp
> /home/dave/2004/tt/
> /home/dave/2004/yy/PPC/child
> /home/dave/2004/yy/PPC/child/grandchild

> I would only want to retrieve the "/home/dave/2004/ss/CTF/temp" and
> "/home/dave/2004/yy/PPC/child" since they are 3 away from
> "/home/dave".

printf "%s\n" $HOME/*/*/*/*/

--
    Chris F.A. Johnson                  http://cfaj.freeshell.org/shell
    ===================================================================
    My code (if any) in this post is copyright 2004, Chris F.A. Johnson
    and may be copied under the terms of the GNU General Public License

 
 
 

Getting directories at a certain depth

Post by Michael Pao » Tue, 27 Jul 2004 04:00:46


Quick and dirty approach which would work on most but not all UNIX
flavors/variants:
ls -d /home/dave/*/*/*/
Note that it will ignore "hidden" (starts with .) pathname components
and it will follow symbolic links, and it will output the trailing /
character.

Where find supports --maxdepth and --mindepth that offers a rather
simple solution.

For a more general backwards compatible solution, one may be able to do
something like:
(
    exec 2>>/dev/null
    cd /home/dave || break
    for a in * .[!.] ..?*
    do [ ! -h "$a" -a -d "$a" ] && (
        cd "$a" || continue
        for b in * .[!.] ..?*
        do [ ! -h "$b" -a -d "$b" ] && (
            cd "$b" || continue
            for c in * .[!.] ..?*
            do [ ! -h "$c" -a -d "$c" ] && (
                cd "$c" || continue
                ls -d /home/dave/"$a/$b/$c"
            )
            done
        )
        done
    )
    done
)


> Hi again, Using ksh on Solaris 8, I would like to retrieve all the
> directories that are a certain depth away from my home directory.
> Let's say my home directory is

> /home/dave

> I am interested in retrieving only the directories that are 3 away
> from my home directory.  So let's say what the directories that
> existed were

> /home/dave/2004/ss/CTF/temp
> /home/dave/2004/tt/
> /home/dave/2004/yy/PPC/child
> /home/dave/2004/yy/PPC/child/grandchild

> I would only want to retrieve the "/home/dave/2004/ss/CTF/temp" and
> "/home/dave/2004/yy/PPC/child" since they are 3 away from
> "/home/dave".

 
 
 

Getting directories at a certain depth

Post by Stephane CHAZELA » Tue, 27 Jul 2004 16:07:24


2004-07-25, 12:00(-07), Michael Paoli:
[...]

Quote:>     for a in * .[!.] ..?*

Use:
     for a in ./* .[!.] ..?*

Or you'll have troubles with "cd" bellow if some dirnames start
with "-"

Quote:>     do [ ! -h "$a" -a -d "$a" ] && (

Note that some "[" implementations don't have "-h" (only "-L"
and some only have "-h").
Most have problems when they get more than 3 arguments.

$ a='=' sh -c '[ ! -h "$a" -a -d "$a" ]' && echo = is a directory
= is a directory
$ ls -d =
=: No such file or directory

Use:
[ ! -h "$a" ] && [ -d "$a" ]

or

case $(ls -ld -- "$a") in
  d*) ...;;
esac

Quote:>         cd "$a" || continue

[...]

Some cd implementations (at least all the POSIX ones, not the
Bourne shell) support cd -- "$a", though.

--
Stephane

 
 
 

1. need all directories from a certain directory up

// __
I am trying silly scripts like;

 ls -laA -R | grep '^d'

but I don't get the full path

// __
this script (which I found online) prints out the files to you also:

 x=`pwd` && ls -al "`find $x -type f -print`"

I only need the directories

// __
How could you just get all directories in a file system using a script
in a memory  efficient, portable and fast way?

otf

2. ali 15xx support

3. How to have more resolutions at a certain depth?

4. Video Capture Card Drivers

5. Delete all containing certain text incl directories apart from one.

6. More than 1024 cylinders

7. Disallowing rm -rf in certain directories (again)

8. How to use vi

9. Check number of bytes for certain directory tree

10. disallowing rm -rf from working in certain directories

11. Restrict ftp user to certain directory tree?

12. Two Video Card Setup with X, Getting X to use certain card

13. someone who figures this out: recursively delete empty directories after a certain time