MandrivaUsers.org : How to get total size of directory in shell? - MandrivaUsers.org

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

How to get total size of directory in shell?

#1 User is offline   fuzzylizard 

  • MUB Addict
  • Group: Members
  • Posts: 803
  • Joined: 23-September 02

Posted 30 March 2003 - 04:25 PM

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.
The universe began with a word. But which came first: the word or the thought behind the word? You can't create language without thought, and you can't conceive a thought without language, so which created the other, and thus created the universe?
---
Registered Linux User #269723 | www.fuzzylizard.com | My Desktop
0

#2 User is offline   aru 

  • Mandriva Guru
  • Group: Members
  • Posts: 2,150
  • Joined: 04-December 02

Posted 30 March 2003 - 04:32 PM

du -s /dir

or


du -sh /dir


man du :D/>
The text you've read above is supposed to be in english;
oops! btw, the code in this post -if any- is released under the GNU GPL v2 or later, that means among other things that it hasn't any warranty.


- The Unix philosophy in one lesson: KEEP IT SIMPLE, STUPID! -- ES. Raymond. "TAOUP"

You might want to take a look to our Musb FAQs and to "THE" FAQs... and remember "/usr/bin/man" is your best friend, use it!

:wq
0

#3 User is offline   fuzzylizard 

  • MUB Addict
  • Group: Members
  • Posts: 803
  • Joined: 23-September 02

Posted 30 March 2003 - 04:51 PM

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?
The universe began with a word. But which came first: the word or the thought behind the word? You can't create language without thought, and you can't conceive a thought without language, so which created the other, and thus created the universe?
---
Registered Linux User #269723 | www.fuzzylizard.com | My Desktop
0

#4 User is offline   MottS 

  • Mandriva Guru
  • Group: Members
  • Posts: 1,041
  • Joined: 19-September 02

Posted 30 March 2003 - 04:52 PM

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
-Server: MDK 9.0 SMP (dual processor)
-Desktop: WinXP
0

#5 User is offline   fuzzylizard 

  • MUB Addict
  • Group: Members
  • Posts: 803
  • Joined: 23-September 02

Posted 30 March 2003 - 04:56 PM

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

Learn something new and usefull everyday on this board.
The universe began with a word. But which came first: the word or the thought behind the word? You can't create language without thought, and you can't conceive a thought without language, so which created the other, and thus created the universe?
---
Registered Linux User #269723 | www.fuzzylizard.com | My Desktop
0

#6 User is offline   qnr 

  • Awesome
  • Group: Members
  • Posts: 477
  • Joined: 15-February 03

Posted 30 March 2003 - 05:42 PM

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

What do you get with ls -sh?
0

#7 User is offline   fuzzylizard 

  • MUB Addict
  • Group: Members
  • Posts: 803
  • Joined: 23-September 02

Posted 30 March 2003 - 05:57 PM

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.
The universe began with a word. But which came first: the word or the thought behind the word? You can't create language without thought, and you can't conceive a thought without language, so which created the other, and thus created the universe?
---
Registered Linux User #269723 | www.fuzzylizard.com | My Desktop
0

#8 User is offline   qnr 

  • Awesome
  • Group: Members
  • Posts: 477
  • Joined: 15-February 03

Posted 30 March 2003 - 06:07 PM

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)
0

#9 User is offline   fuzzylizard 

  • MUB Addict
  • Group: Members
  • Posts: 803
  • Joined: 23-September 02

Posted 30 March 2003 - 06:12 PM

So how does that give the size of a directory in one nicely formated, single line?
The universe began with a word. But which came first: the word or the thought behind the word? You can't create language without thought, and you can't conceive a thought without language, so which created the other, and thus created the universe?
---
Registered Linux User #269723 | www.fuzzylizard.com | My Desktop
0

#10 User is offline   qnr 

  • Awesome
  • Group: Members
  • Posts: 477
  • Joined: 15-February 03

Posted 30 March 2003 - 06:16 PM

Quote

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
0

#11 Guest_Halodude2400_*

  • Group: Guests

Posted 21 May 2011 - 01:22 AM

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}


This post has been edited by Halodude2400: 21 May 2011 - 07:46 PM

0

#12 User is offline   paul 

  • Kimalicious
  • View blog
  • Group: Admin
  • Posts: 5,677
  • Joined: 07-September 02

Posted 21 May 2011 - 07:24 AM

du -sh `ls directory/`
?
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users