LinuxQuestions.org
Review your favorite Linux distribution.
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 05-06-2008, 03:57 AM   #1
concoran
Member
 
Registered: Jun 2001
Location: 28N,82W
Distribution: XP,Ubuntu 9
Posts: 472

Rep: Reputation: 30
How to find a folder size?


How to find a folder's size?
 
Old 05-06-2008, 04:02 AM   #2
b0uncer
LQ Guru
 
Registered: Aug 2003
Distribution: CentOS, OS X
Posts: 5,131

Rep: Reputation: Disabled
A directory ("folder", if you like) is a file like all the others, just a somewhat "special" one. It's own size is rather small, but if you'd like to know the size of all of it's contents (recursively), you can use 'du':
Code:
du /path/to/directory
See
Code:
man du
for more information about the command line switches available.

I'll add an example:

First let's create a directory and a few files:
Code:
mkdir testdir
dd if=/dev/urandom of=testdir/testfile1 bs=2M count=1
dd if=/dev/urandom of=testdir/testfile2 bs=3M count=1
dd if=/dev/urandom of=testdir/testfile3 bs=5M count=1
Then what does the directory information look like?
Code:
ls -ld testdir
It prints this:
Quote:
drwxr-xr-x 2 username username 4096 2008-05-06 12:03 testdir
It's size seems to be 4K. Ok, that doesn't count the file sizes:
Code:
ls -l testdir
That prints
Quote:
-rw-r--r-- 1 username username 2097152 2008-05-06 12:03 testfile1
-rw-r--r-- 1 username username 3145728 2008-05-06 12:03 testfile2
-rw-r--r-- 1 username username 5242880 2008-05-06 12:03 testfile3
What if we use 'du' to calculate the disk usage, then?
Code:
du testdir
That prints
Quote:
10264 testdir/
or in human-readable form:
Code:
du -h testdir
that prints
Quote:
11M testdir
Got it?

As a note: the size of the content of a file isn't necessarily exactly the same size it takes up in disk space. For example if you create a file smaller than 4K, it's size still might show up as 4K (rounded up); this is because the filesystems typically don't (can't) reserve space exactly as much as is needed, but instead they divide the disk space into "blocks" of certain size, and the real data then uses as many blocks as it needs - the final block then either fills up, or more often is left partially empty. To get more information, read about filesystems; these things might sound stupid, minor or both, but actually they do have quite a lot to do with efficiency of a filesystem. Also note that in "human readable form" the file sizes are again rounded a little..

Last edited by b0uncer; 05-06-2008 at 04:19 AM. Reason: typos
 
1 members found this post helpful.
Old 05-06-2008, 07:41 AM   #3
concoran
Member
 
Registered: Jun 2001
Location: 28N,82W
Distribution: XP,Ubuntu 9
Posts: 472

Original Poster
Rep: Reputation: 30
Thanks a lot for the info, b0uncer. Very helpful response.

when I do a du -c /theDir, I get 3806348 Total. Does it mean this folder is 3.8 terabytes? (or closer). (This is a remote server, so it is a possibility, though I doubt).
 
Old 05-06-2008, 08:20 AM   #4
jovie
Member
 
Registered: May 2006
Posts: 54

Rep: Reputation: 15
I think it's GB but try du -ch /theDir and it will round the value to K M G or T

Quote:
[root@localhost ann]# du -ch Videos/
662M Videos/2007-11-09_Alice
563M Videos/2007-11-24_Thunder
951M Videos/Vai
8.8G Videos/Maranchez
184M Videos/zep3
211M Videos/Iommi
1.8G Videos/Foos
 
Old 11-20-2011, 11:23 AM   #5
masikh
LQ Newbie
 
Registered: Nov 2011
Posts: 2

Rep: Reputation: Disabled
Print size of directory ( recursive )

For a sorted list (biggest last!) with size range in MegaBytes (MB) and/or GigaBytes (GB)

du -ch ~/ | grep -e ^[0-9].[MG] | sort -n

use 'du -ch ~/ | grep -e ^[0-9].[MGK] | sort -n' to include KiloBytes (KB) (ommit the ''s)
 
Old 11-20-2011, 12:42 PM   #6
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Quote:
Originally Posted by masikh View Post
For a sorted list (biggest last!) with size range in MegaBytes (MB) and/or GigaBytes (GB)

du -ch ~/ | grep -e ^[0-9].[MG] | sort -n

use 'du -ch ~/ | grep -e ^[0-9].[MGK] | sort -n' to include KiloBytes (KB) (ommit the ''s)
Hi,

Welcome to LQ!

And while this is a great first post, sorting numeric data with
"human readable" sizes makes little sense. Plus the grep doesn't
really gain you anything in this case.

du -sk | sort -n
will do all he needs.


Cheers,
Tink
 
Old 11-21-2011, 11:23 AM   #7
masikh
LQ Newbie
 
Registered: Nov 2011
Posts: 2

Rep: Reputation: Disabled
Stating the obvious 'du -sk | sort -n' does indeed the trick. The grep just removes all the 'kilobyte' files, leaving you (assuming!) with a more interesting result. Obviously!

Kind regards!
 
Old 11-30-2011, 07:20 PM   #8
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Quote:
Originally Posted by masikh View Post
Stating the obvious 'du -sk | sort -n' does indeed the trick. The grep just removes all the 'kilobyte' files, leaving you (assuming!) with a more interesting result. Obviously!

Kind regards!
Which, just as obviously, gives a kind of funky sorting ...
Code:
echo -e "1G\n2M\n30G\n50M" | sort -n
1G
2M
30G
50M


Cheers,
Tink
 
  


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
how to find size increase most file or folder? jimmyjiang Linux - Server 1 07-16-2007 10:47 PM
What is the folder size? cupoftea Linux - Newbie 1 05-21-2006 10:19 AM
Folder Max Size and Limiting SSH access to home folder. Mefistofeles Linux - General 4 11-26-2005 02:09 PM
To Get Folder Size ArulPrabhuT Programming 6 08-12-2004 11:44 AM
how can i default the max folder file size when it create inside a folder antony_csf Linux - Software 1 06-17-2004 02:26 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:01 PM.

Main Menu
Advertisement
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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration