Unix Shell - need all directories from a certain directory up

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > November 2005 > need all directories from a certain directory up





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author need all directories from a certain directory up
onetitfemme

2005-11-12, 5:51 pm

// __
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

martin.witte@gmail.com

2005-11-12, 5:51 pm

'find . -type d' does it.

Michael Tosch

2005-11-12, 5:51 pm

onetitfemme wrote:
> // __
> 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
>


find . -type d -print
is portable.

find . -type d -exec ls -ld {} \;
is portable, too.
Only the "ls -ld" might or might not show the group.

--
Michael Tosch @ hp : com
onetitfemme

2005-11-12, 8:48 pm

thanks for you input.
Well, I crafted a silly script to automate what I want. The most
important thing to me is speed (of course ;-)) and portability; it
shoudl work on Solaris, *BSD and GNU/Linux versions (specifically
Debian (and its many forks), Susex and Fedora at the very least)

There is still one little thing that I am still somehow not able to
figure out right now; namely, how to exclude a certain pattern string.
That is, instead of saying parse and keep all lines that start with
"./proc" or "./KNOPPIX/dev" I would like to actually exclude these
lines from the result of: find . -type d > ${_FL00};

And also I have the feeling this script is working way to slow. (Based
on my experience coding similar things in C) I am using knoppix and
included my baseline so you tell me what do you think

Thanks again
otf

// __ script dirs04.sh
#!/bin/bash

# __ temporary buffer
_FL00="/home/knoppix/dirs04.txt"

# __ file with final results
_FL02="/home/knoppix/dirs06.txt"

# __ patterns to be excluded from ${_FL00} by 'grep'
_XKLD="^./proc|^./KNOPPIX/dev"

cd /
date;
_SKS00=`date +%s| awk '{print $1}'`;
echo "// __ _SKS00: $_SKS00";
find . -type d > ${_FL00};
wc ${_FL00};
_LNS00=`wc ${_FL00}| awk '{print $1}'`;
echo "// __ $_LNS00 lines in ${_FL00}";
cat ${_FL00} | grep -E ${_XKLD} > ${_FL02};
_SKS02=`date +%s| awk '{print $1}'`;
echo "// __ _SKS02: $_SKS02";
rm ${_FL00};
wc ${_FL02};
_LNS02=`wc ${_FL02}| awk '{print $1}'`;
echo "// __ $_LNS02 lines in ${_FL02}";
((_PLNS=$_LNS00 - $_LNS02));
((_SKS=$_SKS02 - $_SKS00));
((_LNSPS=$_PLNS / $_SKS));
echo "// __ $_PLNS lines parsed out using the pattern \"$_XKLD\" in
$_SKS seconds";
echo "// __ approximately $_LNSPS lines parsed per second";

// __ memory
root@1[floppy]# free
total used free shared buffers
cached
Mem: 2075228 542840 1532388 0 35460
193080
-/+ buffers/cache: 314300 1760928
Swap: 0 0 0
root@1[floppy]# dmesg | grep -i 'linux version'
Linux version 2.6.12 (root@Knoppix) (gcc-Version 3.3.6 (Debian
1:3.3.6-7)) #2 SMP Tue Aug 9 23:20:52 CEST 2005

// __ libraries
root@1[floppy]# ls /lib/libc*
/lib/libc-2.3.5.so /lib/libcfont.so.0.0.0 /lib/libcrypt-2.3.5.so
/lib/libc.so.6 /lib/libcom_err.so.2 /lib/libcrypt.so.1
/lib/libcap.so.1 /lib/libcom_err.so.2.1 /lib/libctutils.so.0
/lib/libcap.so.1.10 /lib/libconsole.so.0 /lib/libctutils.so.0.0.0
/lib/libcfont.so.0 /lib/libconsole.so.0.0.0

// __ IO to the CD-ROM drive
root@1[floppy]# hdparm -tT /dev/hdd

/dev/hdd:
Timing cached reads: 808 MB in 2.00 seconds = 403.46 MB/sec
Timing buffered disk reads: 8 MB in 3.85 seconds = 2.08 MB/sec

// __ running script
root@1[floppy]# sh ./dirs04.sh
Sat Nov 12 19:48:37 EST 2005
// __ _SKS00: 1131842917
21092 21159 915309 /home/knoppix/dirs04.txt
// __ 21092 lines in /home/knoppix/dirs04.txt
// __ _SKS02: 1131842921
549 549 10480 /home/knoppix/dirs06.txt
// __ 549 lines in /home/knoppix/dirs06.txt
// __ 20543 lines parsed out using the pattern "^./proc|^./KNOPPIX/dev"
in 4 seconds
// __ approximately 5135 lines parsed per second

Michael Tosch

2005-11-13, 7:49 am

onetitfemme wrote:
> thanks for you input.
> Well, I crafted a silly script to automate what I want. The most
> important thing to me is speed (of course ;-)) and portability; it
> shoudl work on Solaris, *BSD and GNU/Linux versions (specifically
> Debian (and its many forks), Susex and Fedora at the very least)
>
> There is still one little thing that I am still somehow not able to
> figure out right now; namely, how to exclude a certain pattern string.
> That is, instead of saying parse and keep all lines that start with
> "./proc" or "./KNOPPIX/dev" I would like to actually exclude these
> lines from the result of: find . -type d > ${_FL00};
>
> And also I have the feeling this script is working way to slow. (Based
> on my experience coding similar things in C) I am using knoppix and
> included my baseline so you tell me what do you think
>
> Thanks again
> otf
>
> // __ script dirs04.sh
> #!/bin/bash
>
> # __ temporary buffer
> _FL00="/home/knoppix/dirs04.txt"
>
> # __ file with final results
> _FL02="/home/knoppix/dirs06.txt"
>
> # __ patterns to be excluded from ${_FL00} by 'grep'
> _XKLD="^./proc|^./KNOPPIX/dev"
>
> cd /
> date;
> _SKS00=`date +%s| awk '{print $1}'`;
> echo "// __ _SKS00: $_SKS00";
> find . -type d > ${_FL00};
> wc ${_FL00};
> _LNS00=`wc ${_FL00}| awk '{print $1}'`;
> echo "// __ $_LNS00 lines in ${_FL00}";
> cat ${_FL00} | grep -E ${_XKLD} > ${_FL02};
> _SKS02=`date +%s| awk '{print $1}'`;
> echo "// __ _SKS02: $_SKS02";
> rm ${_FL00};
> wc ${_FL02};
> _LNS02=`wc ${_FL02}| awk '{print $1}'`;
> echo "// __ $_LNS02 lines in ${_FL02}";
> ((_PLNS=$_LNS00 - $_LNS02));
> ((_SKS=$_SKS02 - $_SKS00));
> ((_LNSPS=$_PLNS / $_SKS));
> echo "// __ $_PLNS lines parsed out using the pattern \"$_XKLD\" in
> $_SKS seconds";
> echo "// __ approximately $_LNSPS lines parsed per second";
>
> // __ memory
> root@1[floppy]# free
> total used free shared buffers
> cached
> Mem: 2075228 542840 1532388 0 35460
> 193080
> -/+ buffers/cache: 314300 1760928
> Swap: 0 0 0
> root@1[floppy]# dmesg | grep -i 'linux version'
> Linux version 2.6.12 (root@Knoppix) (gcc-Version 3.3.6 (Debian
> 1:3.3.6-7)) #2 SMP Tue Aug 9 23:20:52 CEST 2005
>
> // __ libraries
> root@1[floppy]# ls /lib/libc*
> /lib/libc-2.3.5.so /lib/libcfont.so.0.0.0 /lib/libcrypt-2.3.5.so
> /lib/libc.so.6 /lib/libcom_err.so.2 /lib/libcrypt.so.1
> /lib/libcap.so.1 /lib/libcom_err.so.2.1 /lib/libctutils.so.0
> /lib/libcap.so.1.10 /lib/libconsole.so.0 /lib/libctutils.so.0.0.0
> /lib/libcfont.so.0 /lib/libconsole.so.0.0.0
>
> // __ IO to the CD-ROM drive
> root@1[floppy]# hdparm -tT /dev/hdd
>
> /dev/hdd:
> Timing cached reads: 808 MB in 2.00 seconds = 403.46 MB/sec
> Timing buffered disk reads: 8 MB in 3.85 seconds = 2.08 MB/sec
>
> // __ running script
> root@1[floppy]# sh ./dirs04.sh
> Sat Nov 12 19:48:37 EST 2005
> // __ _SKS00: 1131842917
> 21092 21159 915309 /home/knoppix/dirs04.txt
> // __ 21092 lines in /home/knoppix/dirs04.txt
> // __ _SKS02: 1131842921
> 549 549 10480 /home/knoppix/dirs06.txt
> // __ 549 lines in /home/knoppix/dirs06.txt
> // __ 20543 lines parsed out using the pattern "^./proc|^./KNOPPIX/dev"
> in 4 seconds
> // __ approximately 5135 lines parsed per second
>


On Solaris and Linux (BSD?)
you can exlude /proc already for the find run:

find . -fstype proc -prune -o -type d -print

so you have less to grep.
grep -E is less portable, you should use egrep.

wc file | awk '{print $1}'
should be
awk 'END{print NR}' file

cat file | egrep ...
should be
egrep ... < file

find ... > file
egrep ... < file
rm file
maybe can be replaced by
find ... |
egrep ...

The last lines have syntax error, should be
_PLNS=$((_LNS00 - _LNS02))
_SKS=$((_SKS02 - _SKS00))
_LNSPS=$((_PLNS / _SKS))

In shell no ; needs to be at line end.

--
Michael Tosch @ hp : com
onetitfemme

2005-11-13, 5:53 pm

> you can exlude /proc already for the find run:
>
> find . -fstype proc -prune -o -type d -print
>
> so you have less to grep.


Thanks Michael for your improvements and tips on portability.

You meant to exclude "/dev/*", right?, because I can see "/proc/*"
when I go:

sh-3.00# find . -fstype proc -prune -o -type d > fs00.txt

otf

Michael Tosch

2005-11-13, 5:53 pm

onetitfemme wrote:
>
>
> Thanks Michael for your improvements and tips on portability.
>
> You meant to exclude "/dev/*", right?, because I can see "/proc/*"
> when I go:
>
> sh-3.00# find . -fstype proc -prune -o -type d > fs00.txt
>
> otf
>


No, I really meant

find . -fstype proc -prune -o -type d -print

The -print should suppress the /proc.
I.e. the explicite print (on the 2nd part) suppresses all
implicit prints (in the 1st part).

For /dev/* you need to filter:

find . -fstype proc -prune -o -type d -print | egrep -v "^/dev/"

Or, more efficient but less correct,
suppress ALL dev directories not just /dev:

find . -fstype proc -prune -o -type d \( -name "dev" -prune \
-o -print \)

--
Michael Tosch @ hp : com
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com