2

I want to sum up the size of all files (recursively) which are under the ownership of a specific user. Though, I don't want to have a huge list of all directories, just the overall size.

Therefore the solution from this answer like:

find . -user BobTheCat -type d -exec du -hs {} \;

has to be modified, but how?

I know it is possible with a post-treatment with something like awk but I guess this can be done more straightforward.

Community
  • 1
  • 1
EverythingRightPlace
  • 1,197
  • 12
  • 33

2 Answers2

3

If your find ships with -printf, use that. E.g.:

$ find . -user BobTheCat -type f -printf '%s\n' | awk '{bytes+=$0}END{print bytes}'
Adrian Frühwirth
  • 42,970
  • 10
  • 60
  • 71
  • 1
    @konsolebox How so? This is potentially cleaner and faster than any other solution by skipping unnecessary forks. Yes, if one is being nitpicky about "no `awk`" then this is technically not a correct answer, but OP clearly states that they are after an elegant solution and I believe this is one? Unless you mean something else and I'm misinterpreting the question :) – Adrian Frühwirth Aug 19 '14 at 12:09
  • I actually would have answered `-printf '%s\n'` as well. It was the quickest idea I came up with even when I was just reading the title. But it's not enough to solve the problem. Consider reading comments on the other answer. – konsolebox Aug 19 '14 at 12:19
  • The "no awk" is not a strict rule if the solution is elegant. Still your answer seems to count files several times. For my test-directory the printed size is too big. – EverythingRightPlace Aug 19 '14 at 12:23
  • @konsolebox Would you care to elaborate? I don't see how the comments in the other answer are relevant without using some other construct involving `-type d`. The other answer is technically not correct (as stated by Barmar himself) since it won't descend into directories not owned by `BobTheCat` even if it contains files owned by `BobTheCat` and nowhere in OP's question do they state that this is expected behaviour. Please, enlighten me. – Adrian Frühwirth Aug 19 '14 at 12:51
  • @EverythingRightPlace How did you verify that the result is "too big"? – Adrian Frühwirth Aug 19 '14 at 12:53
  • I tested again for another dir and it seems ok: `>du -sb 5563001872`; `>du -sh 5.3G`; `>echo "5563001872/(1024*1024*1024)" | bc -l 5.18094922602176666259`; `>find . -user BobTheCat -type f -printf '%s\n' | awk '{bytes+=$0}END{print bytes}' 5562973200`. Don't know why the `du -h` is wrong though. – EverythingRightPlace Aug 19 '14 at 13:04
  • Forgot to mention, the test with solely `du` is done in the dir which belongs to Bob. The test for your command is done a dir upwards. In this dir there are also other directories, belonging to other users. By the way, the command of @Barmar gives the exact output of `5563001872`. Still I didn't test the behaviour if files of *Bob* are in a sub-dir of *Pete*. – EverythingRightPlace Aug 19 '14 at 13:11
1

Try this:

find . -user BobTheCat -type d -exec du -shc {} + -prune | egrep '^total\b'

The -prune prevents find from recursing into the directory, since du -s already does that, and it would result in counting each subdirectory multiple times.

The -c option to du tells it to print a grand total at the end, and tail -1 just prints that line.

If you want to include files that are owned by BobTheCat, but aren't in directories that he owns, remove -type d from find.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • That's the point, it's not about home directories. There are directories and sub-dirs of different users and I want to count only the ones from BobTheCat. – EverythingRightPlace Aug 19 '14 at 11:46
  • Revised the answer to use `find ... -prune`. – Barmar Aug 19 '14 at 11:50
  • 2
    What about a folder owned by user 'SteveTheDog' but containing files owned by 'BobTheCat'? The size of those files would be missed by this, would it not? – sanmiguel Aug 19 '14 at 12:01
  • If you just remove `-type d`, the sum may double for every directory containing other files. Oh nevermind. `-prune` does the trick. – konsolebox Aug 19 '14 at 12:03
  • It still has `-prune`, so it won't descend into directories that Bob owns. – Barmar Aug 19 '14 at 12:05
  • So the only thing left is that if there are files inside Bob's directories that are owned by other users, they'll get counted. That's unusual for anyone other that system accounts. – Barmar Aug 19 '14 at 12:06
  • 1
    Just to be safe, you probably want to do `| grep 'total$'` instead of `| tail -1`. Find has an args-list length limit which, if breached, will cause find to split the list of matching directories and execute multiple times. Might not be relevant if there are not many directories, but if it's a large filesystem, it could yield unexpected results. – sanmiguel Aug 19 '14 at 12:10
  • Only if the user has thousands of files in directories he doesn't own. – Barmar Aug 19 '14 at 12:12