Sunday, 4 August 2019

linux - Is there a way to force du to report a directory size (recursively) including only sizes of files?


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?



Answer



$ 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

No comments:

Post a Comment

How can I VLOOKUP in multiple Excel documents?

I am trying to VLOOKUP reference data with around 400 seperate Excel files. Is it possible to do this in a quick way rather than doing it m...