Languages and Scripting
Showing results for 
Search instead for 
Do you mean 

Listing space of all files in all directories into a file

Occasional Visitor

Listing space of all files in all directories into a file

I have set of directories (32 directory) which contains couple of files. I need to check the space of each file and write down in some another file. I can do it manually by just typing the command ls -lrt but want it to do by script/automatically.

E.G in directory - /dnbusr1/gbid/gbid01 have the following files

-rw-r--r-- 1 gbx gbx 227 Feb 12 01:22 error
-rw-r--r-- 1 gbx gbx 0 Feb 12 01:22 sucess
-r--r--r-- 1 gbx gbx 4230 Jun 29 05:58 ph25dh00
-r--r--r-- 1 gbx gbx 2088140800 Jun 29 05:58 ph25dd00
-r--r--r-- 1 gbx gbx 7154 Jun 29 05:58 ph25df00
-r--r--r-- 1 gbx gbx 6004 Jun 29 05:58 ph25df00.eng

So my file will contains file name (error) and it's size (error)
4 REPLIES
Outstanding Contributor Outstanding Contributor

Re: Listing space of all files in all directories into a file

Take a look at the du command.

du -sk /dnbusr1/gbid/gbid01 will give the total for the directory

du -sk /dnbusr1/gbid/gbid01/* will give the size of each of the files (and subdirectories)

That may be what you are looking for.


Pete

Pete
Acclaimed Contributor Acclaimed Contributor

Re: Listing space of all files in all directories into a file

Hi:

You could do this:

# cat .report
#!/usr/bin/sh
typeset DIRLIST=$1
typeset DIRINFO=$2
while read DIR
do
ls -l ${DIR}/error | awk '{print $5, $9}'
done < ${DIRLIST} > ${DIRINFO}

...run as:

# ./report ./mydirs ./myoutput

...where the input file ./mydirs is a simple list of the directories you want to examine:

# cat ./mydirs
/home/panpaliya
/dnbusr1/gbid/gbid01
/dnbusr2/gbid/gbid02

...and ./myouput will be as requested.

Regards!

...JRF...
Honored Contributor Honored Contributor

Re: Listing space of all files in all directories into a file

Find might also help you.

find /dnbusr1/gbid/ -type f -exec ll {} \; |awk '{print $9 , $5}' > /var/tmp/dnbusr1.txt

Highlighted
Respected Contributor Respected Contributor

Re: Listing space of all files in all directories into a file

Hi,

A very simple script ::::

##How to get total file size of all files in each directory under /dnbusr1/gbid

for x in `ll /dnbusr1/gbid |awk '{$NF}'`
do
echo "Total File size of directory /dnbusr1/gbid/$x is following" >> /tmp/spacedetails.file
du -sk /dnbusr1/gbid/$x >> /tmp/spacedetails.file
echo "######################"
done

Then run

cat /tmp/spacedetails.file
"There is always some scope for improvement"