LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 03-30-2007, 05:45 AM   #1
pxumsgdxpcvjm
Member
 
Registered: Aug 2005
Posts: 235

Rep: Reputation: 30
Quick Directory Size Question


Hi. I'm sorry if this is a stupid question, but how can I get the total size of all the files (not directories) in a directory and all its subdirectories.

By searching, I've found the du command but it is adding the . directory(?) size to the total. Here's what I mean...

Code:
bash-3.1$ ls -al

total 26
drwxr-xr-x  2 richard users  168 2007-02-20 15:21 .
drwxr-xr-x 27 richard users 1408 2007-03-30 22:36 ..
-rw-r--r--  1 richard users 5909 2007-02-20 13:54 bg.png
-rw-r--r--  1 richard users 4815 2007-02-18 22:04 counter.png
-rw-r--r--  1 richard users 1150 2007-02-20 15:21 favicon.ico
-rw-r--r--  1 richard users  789 2007-02-20 15:42 index.htm
So by adding all the files together, I get 12,663 bytes in total. When I use the du command, it tells me that the folder is 12,831 in size.

What can I do to resolve this? I've tried a bunch of arguments to du but it makes no difference.

Thanks to any replies.
 
Old 03-30-2007, 06:09 AM   #2
Simon Bridge
Guru
 
Registered: Oct 2003
Location: Waiheke NZ
Distribution: Ubuntu
Posts: 9,211

Rep: Reputation: 197Reputation: 197
Show us the du command and it's output please. I get:
Code:
simon@errata:~/projects/tutorial$ ls -al
total 1748
drwxr-xr-x 3 simon simon   4096 2007-03-19 20:20 .
drwxr-xr-x 8 simon simon   4096 2007-03-21 21:04 ..
-rw-r--r-- 1 simon simon  55079 2007-03-16 15:30 aulug-tut1-intro.pdf
-rw-r--r-- 1 simon simon  34744 2007-03-16 15:30 lesson0.odt
-rw-r--r-- 1 simon simon  47329 2007-03-16 15:42 lessons.odt
-rw-r--r-- 1 simon simon 460737 2007-03-16 15:42 lessons.pdf
drwxr-xr-x 2 simon simon   4096 2007-03-13 17:39 licenses
-rw-r--r-- 1 simon simon 832414 2007-03-19 11:05 tutorial.tar.gz
-rw-r--r-- 1 simon simon 315864 2007-03-19 01:20 ualug-web.tar.bz2
simon@errata:~/projects/tutorial$ du -ah
36K     ./lesson0.odt
4.0K    ./licenses/ccsa.txt
20K     ./licenses/gnugplv2.txt
668K    ./licenses/gplvseula.pdf
104K    ./licenses/vista-eula.pdf
800K    ./licenses
456K    ./lessons.pdf
48K     ./lessons.odt
60K     ./aulug-tut1-intro.pdf
316K    ./ualug-web.tar.bz2
820K    ./tutorial.tar.gz
2.5M    .
simon@errata:~/projects/tutorial$ du -h
800K    ./licenses
2.5M    .
simon@errata:~/projects/tutorial$
There are 2.5 MiB of files in the working directory, of which 800MiB are in the licenses subdirectory. The size of the subdirectory doesn't appear to have been been included in this.
 
Old 03-30-2007, 06:14 AM   #3
pwc101
Senior Member
 
Registered: Oct 2005
Location: UK
Distribution: Slackware
Posts: 1,846

Rep: Reputation: 128Reputation: 128
Quote:
Originally Posted by Simon Bridge
Show us the du command and it's output please. I get:
Code:
simon@errata:~/projects/tutorial$ ls -al
total 1748
drwxr-xr-x 3 simon simon   4096 2007-03-19 20:20 .
drwxr-xr-x 8 simon simon   4096 2007-03-21 21:04 ..
-rw-r--r-- 1 simon simon  55079 2007-03-16 15:30 aulug-tut1-intro.pdf
-rw-r--r-- 1 simon simon  34744 2007-03-16 15:30 lesson0.odt
-rw-r--r-- 1 simon simon  47329 2007-03-16 15:42 lessons.odt
-rw-r--r-- 1 simon simon 460737 2007-03-16 15:42 lessons.pdf
drwxr-xr-x 2 simon simon   4096 2007-03-13 17:39 licenses
-rw-r--r-- 1 simon simon 832414 2007-03-19 11:05 tutorial.tar.gz
-rw-r--r-- 1 simon simon 315864 2007-03-19 01:20 ualug-web.tar.bz2
simon@errata:~/projects/tutorial$ du -ah
36K     ./lesson0.odt
4.0K    ./licenses/ccsa.txt
20K     ./licenses/gnugplv2.txt
668K    ./licenses/gplvseula.pdf
104K    ./licenses/vista-eula.pdf
800K    ./licenses
456K    ./lessons.pdf
48K     ./lessons.odt
60K     ./aulug-tut1-intro.pdf
316K    ./ualug-web.tar.bz2
820K    ./tutorial.tar.gz
2.5M    .
simon@errata:~/projects/tutorial$ du -h
800K    ./licenses
2.5M    .
simon@errata:~/projects/tutorial$
There are 2.5 MiB of files in the working directory, of which 800MiB are in the licenses subdirectory. The size of the subdirectory doesn't appear to have been been included in this.
Is the licenses directory not 800KiB?
 
Old 03-30-2007, 06:16 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
Hi,

ls reports the size of the file, du on the other hand reports the amount of blocks (-k will give 1024 instead of the default 512 bytes) it occupies on disk. This is (almost) always larger then what ls reports.

Small files will occupy at least on block on your hd (blocksize differ, but are generally 4096 on modern system hd's). So a file that is 1 byte (shown by ls as being 1 byte) will show up in du as being 4096 bytes. Du also reports in 512 byte blocks (or 1024 if -k is used).

Hope this helps.
 
Old 03-30-2007, 06:43 AM   #5
pxumsgdxpcvjm
Member
 
Registered: Aug 2005
Posts: 235

Original Poster
Rep: Reputation: 30
Yea, thanks for your help. So there is no standard way to figure this out?
 
Old 03-30-2007, 09:02 PM   #6
Simon Bridge
Guru
 
Registered: Oct 2003
Location: Waiheke NZ
Distribution: Ubuntu
Posts: 9,211

Rep: Reputation: 197Reputation: 197
Quote:
Is the licenses directory not 800KiB?
Of course not - check out the contents.

4.0K ./licenses/ccsa.txt
20K ./licenses/gnugplv2.txt
668K ./licenses/gplvseula.pdf
104K ./licenses/vista-eula.pdf

4+20+668+104=796K
... do you suspect that the missing 4K is the space used by the directory itself?
(rt-click on licences and select "properties" and I see it listed as 779.2K ... remeber, there is rounding to consider with du.)

But you were comparing du with ls
Code:
simon@errata:~/projects/tutorial$ ls -l licenses
total 796
[snip]
See?

Quote:
Yea, thanks for your help. So there is no standard way to figure this out?
I think the thing to ask here is "what are you trying to achieve?"

Presumably you could write a script to get the size of regular files only out of an ls output and then add them, but why would you want to?

du will tell you how much disk space a given directory occupies if you have restricted space in your account, and ls will tell you how big the files are if you want to move them to another media and you want to know if they will fit.
 
Old 03-31-2007, 04:55 AM   #7
pwc101
Senior Member
 
Registered: Oct 2005
Location: UK
Distribution: Slackware
Posts: 1,846

Rep: Reputation: 128Reputation: 128
Quote:
Originally Posted by Simon Bridge
Quote:
Is the licenses directory not 800KiB?
Of course not - check out the contents.

4.0K ./licenses/ccsa.txt
20K ./licenses/gnugplv2.txt
668K ./licenses/gplvseula.pdf
104K ./licenses/vista-eula.pdf

4+20+668+104=796K
... do you suspect that the missing 4K is the space used by the directory itself?
Quote:
Originally Posted by Simon Bridge
<snip> ... of which 800MiB are in the licenses subdirectory.
I was just checking if you'd meant to type 800MiB in your first post, not questioning your logic Nice clarification though!
 
Old 03-31-2007, 10:55 PM   #8
Simon Bridge
Guru
 
Registered: Oct 2003
Location: Waiheke NZ
Distribution: Ubuntu
Posts: 9,211

Rep: Reputation: 197Reputation: 197
Quote:
I was just checking if you'd meant to type 800MiB in your first post, not questioning your logic Nice clarification though!
Ah... easy mistake to make.

I didn't type that stuff at all, I used cut and paste from actual commands run in terminal on my system. So all that was the exact output.

A tip: this is what you should do when you are representing the results of commands/programs you run in terminal. As much as possible, copy over the entire command run as well as the output. This way, everyone can be sure there are no typos in this vital information.

Just drag the mouse over the text to select it, then rt-click your selection for the menu.

Last edited by Simon Bridge; 03-31-2007 at 10:57 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Directory Size carlosinfl Linux - General 3 01-09-2007 08:06 PM
How to know the size of a directory? kornerr Linux - General 4 06-30-2005 09:59 AM
how could I know the directory size gsbarry Linux - General 4 03-07-2003 08:50 AM
directory size adenardo Linux - General 1 10-23-2001 01:34 PM
Directory size Steave Linux - General 4 09-03-2001 04:13 AM


All times are GMT -5. The time now is 03:44 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration