Jump to content
  • Announcements

    • spinynorman

      Mandriva Official Documentation

      Official documentation for extant versions of Mandriva can be found at doc.mandriva.com.   Documentation for the latest release may take some time to appear there. You can install all the manuals from the main repository if you have Mandriva installed - files are prefixed mandriva-doc.
    • paul

      Forum software upgrade   10/29/17

      So you may have noticed the forum software has upgraded !!!
      A few things that have changed. We no longer have community blogs (was never really used) We no longer have a portal page.
      We can discuss this, and decide whether it is needed (It costs money) See this thread: Here
Sign in to follow this  
fuzzylizard

How to get total size of directory in shell?

Recommended Posts

When I do an ls -l command I can get the size of files, but I can't get the total size of a directory -- i.e. the size of everything below that directory.

 

Is there anyway of getting this information with a simple command form within a shell? I realize that I can open Konquerer and right click on a directory, but there must be a way of doing this from within the shell.

 

Thanks.

Share this post


Link to post
Share on other sites

That's what I was looking for. Thanks.

 

Hey, anyway of integrating that with the ls command so I can see the size of all files and the total size of the directories at the same time?

Share this post


Link to post
Share on other sites

Just to increase my post count :wink: :

 

For partitions, you have to use 'df'

 

bash-2.05b$ df -h /dev/hda*

Filesystem            Size  Used Avail Use% Mounted on

/dev/hda                 0     0     0   - 

/dev/hda1                0     0     0   - 

/dev/hda2                0     0     0   - 

/dev/hda5              22G   17G  5.5G  75% /mnt/Datas

/dev/hda6             3.1G  2.1G  1.1G  67% /

/dev/hda7                0     0     0   -

 

MOttS

Share this post


Link to post
Share on other sites

Now that is very cool. I Got to remember that one.

 

Learn something new and usefull everyday on this board.

Share this post


Link to post
Share on other sites

I'm surprised you didn't get the summary with ls -l

 

What do you get with ls -sh?

Share this post


Link to post
Share on other sites

with ls -sh all you get is each directory listed as either 4k or 8k and nothing more.

 

I ran the du -sh dir command and one directory came in at amost 7megs.

Share this post


Link to post
Share on other sites

Well I thought you meant just to check one directory (in the original post where you used ls -l)

 

sh-2.05b$ ls -sh RealPlayer8/

total 1.6M

4.0k app.kdelnk    24k LICENSE         4.0k pluginstall.sh    8.0k rp7doc.xpm

12k audiosig.rm  4.0k Mailcap         8.0k raclass.zip       4.0k rp7mini.xpm

4.0k Codecs        12k mimeinstall.sh  4.0k README            4.0k rp7.xpm

4.0k Common       4.0k mime.kdelnk     1.2M realplay          136k rpnp.so

80k firstrun.rm  4.0k Mime.types      4.0k realplay.desktop

4.0k Help         4.0k Plugins         4.0k rp7doc.png

 

And if I wanted to check the directories recursively:

 

sh-2.05b$ ls -sh RealPlayer8/*

4.0k RealPlayer8/app.kdelnk      8.0k RealPlayer8/raclass.zip

12k RealPlayer8/audiosig.rm     4.0k RealPlayer8/README

80k RealPlayer8/firstrun.rm     1.2M RealPlayer8/realplay

24k RealPlayer8/LICENSE         4.0k RealPlayer8/realplay.desktop

4.0k RealPlayer8/Mailcap         4.0k RealPlayer8/rp7doc.png

12k RealPlayer8/mimeinstall.sh  8.0k RealPlayer8/rp7doc.xpm

4.0k RealPlayer8/mime.kdelnk     4.0k RealPlayer8/rp7mini.xpm

4.0k RealPlayer8/Mime.types      4.0k RealPlayer8/rp7.xpm

4.0k RealPlayer8/pluginstall.sh  136k RealPlayer8/rpnp.so



RealPlayer8/Codecs:

total 1.9M

100k 14_4.so.6.0   40k ddnt.so.6.0  216k drv3.so.6.0     132k rv20.so.6.0

56k 28_8.so.6.0   32k dnet.so.6.0   44k dspr.so.6.0     124k rv30.so.6.0

32k atrc.so.6.0  176k drv1.so.6.0  508k rncolor.so.6.0   28k sipr.so.6.0

64k cook.so.6.0  192k drv2.so.6.0   56k rv10.so.6.0      64k tokr.so.6.0



RealPlayer8/Common:

total 1.9M

24k pnrscmgr.so.6.0  148k rcaembed.so.6.0

600k rcacore.so.6.0   1.2M rmacore.so.6.0



RealPlayer8/Help:

total 12k

4.0k readme  4.0k realplay  4.0k realplay.htm



RealPlayer8/Plugins:

total 5.7M

100k audplin.so.6.0   140k pxcpng.so.6.0    152k rtrender.so.6.0

124k authmgr.so.6.0   268k pxff.so.6.0      160k rvrend.so.6.0

44k bascauth.so.6.0  112k pxgf.so.6.0       72k sdpplin.so.6.0

4.0k ExtResources     160k pxgr.so.6.0      192k skinfs.so.6.0

196k httpfsys.so.6.0   96k pxjf.so.6.0       72k smlffpln.so.6.0

72k memfsys.so.6.0   176k pxjr.so.6.0      200k smlrendr.so.6.0

72k meta.so.6.0      160k pxpf.so.6.0       36k smmrendr.so.6.0

40k mp3f.so.6.0      152k pxpr.so.6.0       72k smplfsys.so.6.0

76k mp3mfpln.so.6.0  184k pxrend2.so.6.0   120k swff.so.6.0

124k mp3r.so.6.0      144k rarender.so.6.0  580k swfrend.so.6.0

40k pnxres.so.6.0    448k rcadlg.so.6.0    132k vidplin.so.6.0

136k ppffplin.so.6.0  196k rmffplin.so.6.0  220k vsrcplin.so.6.0

64k pxcgif2.so.6.0    48k rn5auth.so.6.0    92k xlibsite.so.6.0

156k pxcjpeg2.so.6.0  152k rtffplin.so.6.0

 

Although in this case I'd use ls -sh RealPlayer8/* | most (most is a pager I use, like less, or more)

Share this post


Link to post
Share on other sites

So how does that give the size of a directory in one nicely formated, single line?

Share this post


Link to post
Share on other sites
When I do an ls -l command I can get the size of files, but I can't get the total size of a directory -- i.e. the size of everything below that directory.

 

Your original question did not state that that was all you wanted, and you've been introduced to df and du --- I was simply commenting on the fact that ls -l did not give you the summary, when it does on my system. As far as a single line:

 

sh-2.05b$ ls -sh temp

total 68k

12k 11-Match-_30sec_anim.gif-19720.gif  8.0k nutbolt.jpg

16k glove.jpg                            16k sit3-shine.7.gif

16k jigsaw.xcf

 

You'll notice the very first line after the command is the summary, on a single line. However, as I said, it was only brought up as a matter of curiosity about your ls -l, du is the way to go

Share this post


Link to post
Share on other sites
Guest Halodude2400

Not robust, by any means, but this will do the job...

 

It will provide the exact number of bytes, including the size of directories.

 

#!/bin/sh

# folders typically have a single size of 4kB or so...
DIR_SIZE=4096

size=0

if [ -d $1 ]
then
 files=`ls $1`

 for file in ${files}
 do
   file_size=`$0 "${1}/${file}"`
   size=`expr ${size} + ${file_size}`
 done

 # remove this line to exclude the size of folders
 size=`expr ${size} + ${DIR_SIZE}`
elif [ -f $1 ]
then
 size=`ls $1 -l | cut -d ' ' -f 5`
else
 printf "Error: $1 not found\n"
fi

echo ${size}

Edited by Halodude2400

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×