Operating System - HP-UX
1827802 Members
2307 Online
109969 Solutions
New Discussion

List of Files and FS Sizes

 
SOLVED
Go to solution
panchpan
Regular Advisor

List of Files and FS Sizes

I would like to list entries of
ls -ld /a????/amfp*
But I require only these names and their filesystem's space alloted, used, available. Also, I would like to have this info from several ssh servers.

Please advice.
16 REPLIES 16
Andrew Young_2
Honored Contributor

Re: List of Files and FS Sizes

Hi

Are amfp* directories or files?

Regards

Andrew Y
Si hoc legere scis, nimis eruditionis habes
panchpan
Regular Advisor

Re: List of Files and FS Sizes

They are directories
panchpan
Regular Advisor

Re: List of Files and FS Sizes

I have somehow got all filesystem names and their directories in format like /a1234/amfp123a/ for each server. and stored them in servername.txt now. Does below code look okay to find out the size stat. Or any better way?

echo "Enter Linux Server Name (e.g. l57)"
read serv
case "$serv" in
l57)
ssh $serv "for i in `cat 57.txt`
do
df -k $i
done" >> 57.out
;;
l58)
;;
l59)
;;
*)
echo "Bad command, your choices are: Linux Server Names"
;;
esac
exit 0
Andrew Young_2
Honored Contributor
Solution

Re: List of Files and FS Sizes

Hi

Assuming they are all mountpoints use the following:

#!/usr/local/bin/bash
for id in `ls -d /a????/amfp*`
do
if [ -d $id ]
then
bdf $id
fi
done

Si hoc legere scis, nimis eruditionis habes
Andrew Young_2
Honored Contributor

Re: List of Files and FS Sizes

Hi

If they are Linux servers you will need to change the bdf to df -k

For ssh then try the following variation:

ssh $serv "for id in `ls -d /a\?\?\?\?/amfp\*` ; do if [ -d \$id ] ; then df -k \$id; fi ; done"

Please note I have escaped the wild cards and dollar symbols to prevent them from being evaluated by the local shell.

HTH

Andrew Y
Si hoc legere scis, nimis eruditionis habes
Andrew Young_2
Honored Contributor

Re: List of Files and FS Sizes

You may need to escape (\) the back ticks (`) too.

Regards

Andrew Y
Si hoc legere scis, nimis eruditionis habes
panchpan
Regular Advisor

Re: List of Files and FS Sizes

Is it correct?

for serv_cnt in `cat /home/mfgeb/pp/serv.txt`
do
echo "===[ Directory Size for Server: $serv_cnt ]==="
ssh $serv "for id in \`ls -d /a\?\?\?\?/amfp\*\` ;
do
if [ -d \$id ] ;
then df -k \$id;
fi ;
done" >> {serv}.out
done >> dbsize.lst
Andrew Young_2
Honored Contributor

Re: List of Files and FS Sizes

I will confirm this tomorrow.

I am having some issues with my testing which I think are shell specific with regards to what I need escape.

Regards

Andrew Y
Si hoc legere scis, nimis eruditionis habes
Peter Nikitka
Honored Contributor

Re: List of Files and FS Sizes

Hi,

- change to single quotes will make life easier.
- the remote shell will expand the filename pattern by itself: no need for 'ls -d'
- in this situation, I would send the output to a file AND terminal.
- you mixed up variable names ($serv, $serv_cnt)
- watch carefully the end of my quoting: the filename will be expanded by the local shell but used at the remote site.
Calling from a POSIX shell, I suggest:

for serv in $(do
print "===[ Directory Size for Server: $serv ]==="
ssh -n $serv 'for id in /a????/amfp*; do
[ -d $id ] && df -k $id; done | tee '${serv}.out
done | tee dbsize.lst

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
Andrew Young_2
Honored Contributor

Re: List of Files and FS Sizes

Hi Peter

Thanks for your help. I know this isn't my post but if I may ask two questions about your script.

Firstly I don't understand the location of your closing single quote. I would have thought the quote should be after the done before the first pipe symbol.

Secondly I had got most of the way as your script with my later versions, the only difficulty now is how would I do it if I want the remote shell to expand the wildcards and not the local shell?

If I do escape them then the escapes are carried over to the remote shell and if I don't then the local shell expands them.

The ugly work around was to do something like this:
ssh -n $serv 'files=`eval eval echo "/a\?\?\?\?/amfp\*"` ;for id in `echo $files` ; do [ -d $id ] && df -k $id; done | tee '${serv}.out done | tee dbsize.lst

However since this is Linux (I'm assuming RedHat here) a much safer way would be to use the /etc/mtab file which already lists the mounted filesystems.

Then you can replace the above code with:
ssh -n $serv 'for id in `cat /etc/mtab | cut -f2 -d" " | grep "/a..../ampf.*"` ; do df -k $id ; done ' | tee ${serv}.out done | tee dbsize.lst

Regards

Andrew Y
Si hoc legere scis, nimis eruditionis habes
Peter Nikitka
Honored Contributor

Re: List of Files and FS Sizes

Hi,

this is for Andrew, though others might be interested as well :-)

Let's look at this construction:
ssh -n $serv 'for id in /a????/amfp*; do
[ -d $id ] && df -k $id; done | tee '${serv}.out

1) The local shell expands the value of the variable 'serv'

2) ssh gets ONE execution string, consisting of the part between the single quotes - which will be left unmodified by the local shell - concatenated with the values of the expanded shell variable and the suffix: To make this work, no space is allowed between the single quote and the '$serv', neither may the variable contain a space character!

3) The remote shell spawned by the ssh command will now parse the command string, and do whát has to be done:
- look for quoting (none found!)
- do filename expansion of /a????/amfp* !!
...

I assumed when reading the remarks of the thread creator, that the file $serv.out should be created at the remote site.
To get that file locally is easier, of course.

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
panchpan
Regular Advisor

Re: List of Files and FS Sizes

Thank you, Yes the output file was being created on local server - which is what was expected.

I have one more doubt: In below script, IF i wish to grep some value, Its not coming at all.

for serv_cnt in `cat /home/mfgeb/pp/serv.txt`
do
ssh $serv_cnt "for id in \`cat arra.lst\` ;
do
grep \$id /home/ppp?.st ;
done" >> inner.out
done

The issue is that grep isnt able to understand $id within ssh's for loop. Am i missing something?

Thanks!
Peter Nikitka
Honored Contributor

Re: List of Files and FS Sizes

Hi,

read my replies carefully - I see double quotes in your command, so $id will be expanded locally.
Use single quotes for the ssh command string!

mfG Peter

PS: Better reopen this thread.
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
panchpan
Regular Advisor

Re: List of Files and FS Sizes

Still same issues :-( grep isnt able to understand $id

a300sl57:/home/mfgeb/pp/extents> ./ext_grp1.sh
+++ cat /home/mfgeb/pp/extents/serv.txt
++ ssh a300sl57 'for id in \`cat /home/mfgeb/pp/extents/extlist.lst\` ;
do
grep \$id /a???1/amfp???p/a??????p.st;
done'
++ ssh a300sl58 'for id in \`cat /home/mfgeb/pp/extents/extlist.lst\` ;
do
grep \$id /a???1/amfp???p/a??????p.st;
done'
a300sl57:/home/mfgeb/pp/extents> more ext_grp1.sh
set -ax
for srv in `cat /home/mfgeb/pp/extents/serv.txt`
do
ssh $srv 'for id in \`cat /home/mfgeb/pp/extents/extlist.lst\` ;
do
grep \$id /a???1/amfp???p/a??????p.st;
done' >> out.out2
done
a300sl57:/home/mfgeb/pp/extents>
Peter Nikitka
Honored Contributor

Re: List of Files and FS Sizes

Hi,

the modified version of your previous reply:

for serv_cnt in `cat /home/mfgeb/pp/serv.txt`
do
ssh $serv_cnt 'for id in `cat arra.lst` ;
do
grep $id /home/ppp?.st ;
done' >> inner.out
done

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
panchpan
Regular Advisor

Re: List of Files and FS Sizes

THANKS A LOT!!