3

Suppose I create a directory test/, with two files (test/a, test/b) and an inner directory test/c with a file test/c/d, like this:

mkdir test
cd test
touch a
echo 1 > a
touch b
echo 1 > b
mkdir c
cd c
touch d
echo 1 > d
cd ../..
du test -ab

The output of the last line (running du) is:

2       test/a
4096    test/c
2       test/b
8196    test

The size of the directory is 8196 (instead of 6, which would be: size of file a + size of file b + size of file c/d). This is because, as i understand it, du includes the size of directories themselves (because a directory is just a special file, which records file entries in it).

I don't want that. I want to see the combined size of all the files in a directory (the way Windows Explorer reports directory size). So in this example, the result should be:

2       test/a
2       test/c
2       test/b
6       test

More importantly, what I really want is that last line: the sum of sizes of all the files in the directory (recursively).

But I have gone through all the options of du, and there doesn't seem to be a way to do this. Is there any way?

3
  • 1
    Just out of curiosity is there a reason for this? Usually you want to know how much space it takes to store those files in that structure, and that would include the size of a directory. Dec 20, 2011 at 15:53
  • The reason may be a bit stupid :) I just copied a huge directory from my linux machine to my windows machine, and now i just wanted a simple way to make sure that everything got copied OK. So i thought, ill just check the total sizes of both directories on both machines. But the totals were different, so i looked into it, and saw that du includes the size of the directory itself and windows explorer doesn't, so i thought maybe there is a way to emulate the explorer-like behaviour for du, because explorer doesn't have a whole lof of flexibility :)
    – Kipras
    Dec 20, 2011 at 16:00
  • While not an answer to the question asked, this question provides a means to verify the integrity of files copied between linux and windows, and is how I would recommend doing file verification checks. Dec 20, 2011 at 16:31

3 Answers 3

1
$ ls -goR | awk '{sum += $3} END{print sum}'
16992

Edit. To exclude directories, use grep

$ ls -goR | grep -v ^d | awk '{sum += $3} END{print sum}'
6
4
  • That is not exactly what i need (i updated the question to be more specific). If i understand it correctly, this produces the total of sizes of files in the current directory (recursively), which is good, and it doesn't include the size of the current directory itself (good again), but it does include the sizes of the inner directories (not good). I added an inner directory c and a file c/d in the question, to be more specific.
    – Kipras
    Dec 20, 2011 at 16:16
  • @Kipras: See updated answer. Dec 20, 2011 at 17:54
  • Works, but the directory is so large, that if i run this command at the root of it, awk returns inf. Do you know of any way to work around it? What i ended up doing is running this individually for every child directory.
    – Kipras
    Dec 20, 2011 at 18:49
  • 2
    @Kipras: You have to make some choices. You could change ls to report size in blocks. You could alter the awk script to divide $3 by 1024. You could replace awk by Perl. In extremis you could use Perl's Math::BigInt module. You could rethink what you are doing and calculate a checksum instead. You could use a port of ls on Windows, redirect output to a file and do a diff. etc etc Dec 20, 2011 at 19:08
2

If all you want is the size of the files, excluding the space the directories take up, you could do something like

$ find test/ -type f -print0 | xargs -0 du -scb | awk '/total/{k+=$1}END{print k" total"}'
6   total
2
  • But this will fail if there are so many files that xargs needs to invoke du more than once, won’t it? Jun 13, 2014 at 13:21
  • @Scott yes it will, good point. I fixed it by using awk instead, thanks.
    – terdon
    Jun 13, 2014 at 13:29
0

You specify that you want a summary of files within a directory, but your example output includes specific files.

For detail including specific files, try this:

$ find test/ -exec du -s {} \;

20      test/
4       test/b
4       test/a
8       test/c
4       test/c/d

For just a summary of each directory, try this:

$ find test/ -type d -exec du -s {} \;

20      test/
8       test/c

Hopefully that will be of help to you?

1
  • 2
    ...and the downvote is for what exactly?
    – Bryan
    Jun 18, 2014 at 8:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .