LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
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-12-2013, 11:23 AM   #1
basher400
Member
 
Registered: Mar 2005
Posts: 54

Rep: Reputation: 15
How to get total size of directory ?


hi

I need the sum of the size of files in a certain directory and the files in it's sub directories.

this should have done it:
Code:
du -sh mydirectory
but the thing is it counts empty directories as 4k. I don't want that, I want empty directories to give 0.
I only want the sum of size of the files.

also, I need for it to only give something like "72M", I don't want the name of the directory/directories/files as output.

Last edited by basher400; 05-12-2013 at 11:26 AM.
 
Old 05-12-2013, 11:30 AM   #2
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
Any new empty directory by default gets size of 4k. To check size of files and sub-directories, try:
Code:
~$ cd /path/to/directory
~$ du -sk ./*
 
Old 05-12-2013, 11:35 AM   #3
basher400
Member
 
Registered: Mar 2005
Posts: 54

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by shivaa View Post
Any new empty directory by default gets size of 4k. To check size of files and sub-directories, try:
Code:
~$ cd /path/to/directory
~$ du -sk ./*
isn't there a way to sum only the size of the files ?
also , the command you posted gives me the list of the names as well instead of just the number as the sum, which I stated I can't use.

Last edited by basher400; 05-12-2013 at 11:36 AM.
 
Old 05-12-2013, 11:41 AM   #4
jv2112
Member
 
Registered: Jan 2009
Location: New England
Distribution: Arch Linux
Posts: 719

Rep: Reputation: 103Reputation: 103
Try:

Code:
find . -type f | du -sh
 
1 members found this post helpful.
Old 05-12-2013, 11:54 AM   #5
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 5,029

Rep: Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845
seems like you would have to do it manually:
Code:
find . -type f -printf "%s\n" | awk '{sum = sum + $1} END {print " sum = " sum}'
 
1 members found this post helpful.
Old 05-12-2013, 12:08 PM   #6
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
Quote:
Originally Posted by basher400 View Post
...the command you posted gives me the list of the names as well instead of just the number as the sum, which I stated I can't use.
A simple:
Code:
~$ du -sk /path/to/directory
Will give you size of whole directory.

If you want to check sizes of all files without filenames, try awk, as:
Code:
~$ du -sk ./* | awk '{print $1}'
On the other hand, try find cmd and it's various options, as mentioned in above posts, to check size of each and every file.
 
Old 05-12-2013, 02:37 PM   #7
era31415
LQ Newbie
 
Registered: May 2013
Posts: 3

Rep: Reputation: Disabled
wc counts characxters (bytes)

Try...
$ find OTR_podcasts -type f | xargs cat | wc -c
297912862

The xargs collects filenames into shell size groups and then invokes 'cat' for each group.

So that makes a stream of bytes on stdout of 'cat' piped to 'wc -c' which gives the total.

That command reads through each of the files and I just realized that the total you want can be obtained faster with...
$ find OTR_podcasts -type f | xargs wc -c | tail -1
297912862 total

'wc -c' uses file size read from the inode so it goes quickly but each file is listed along with its size and then the total is the sum
of all the input files.
--
Ed Allen
 
Old 05-13-2013, 02:48 AM   #8
mddnix
Member
 
Registered: Mar 2013
Location: Bangalore, India
Distribution: Redhat, Arch, Ubuntu
Posts: 512

Rep: Reputation: 139Reputation: 139
Code:
$ du -ch $(find mydirectory -type f -not -empty) | tail -n1

Last edited by mddnix; 05-13-2013 at 02:58 AM.
 
  


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
BASH : Total directory size xgreen Programming 7 03-04-2013 06:33 AM
Total particular files size in a directory kirukan Linux - Newbie 3 01-03-2010 03:48 PM
how could i get real time total size of directory? DoME69 Programming 1 07-30-2008 11:47 AM
finding size of total files in a directory blackzone Linux - Newbie 3 01-07-2005 04:01 AM
How to list total file size of a directory phil1076 Linux - General 3 12-18-2003 04:47 PM


All times are GMT -5. The time now is 01:39 AM.

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