I have 2 folders that have subfolders (named after users), most subfolders exists in both folders, but not all. Now I want to create a list of space used by each user.

I can use the following command to get a list for each folder,

du -m --max-depth=1 | sort -nr

but I don't know an easy way to merge two of these lists. Not all users have a folder in both places, so simple sorting does not work. Any idea on how to sum up the folder sizes for each user?

link|improve this question

60% accept rate
I don't quite get your folders structure. Could you give a simple example to clarify? – Patkos Csaba Sep 28 '10 at 13:45
feedback

1 Answer

up vote 3 down vote accepted

Give this a try:

join -j 2 <(cd dir1; du -m --max-depth=1 | sort -k2,2) <(cd dir2; du -m --max-depth=1 | sort -k2,2) | awk '{print $2 + $3, $1}' | sort -nr

It should look like this:

11 ./bob
9 ./jan
8 ./cheryl
3 ./mike

You can change the AWK command to make the output a little more attractive:

awk '{printf "%6s\t%s\n", $2 + $3, $1}'

which will right-align the numbers and make the names line up in a column.

link|improve this answer
Thanks, this works perfectly. I didn't know about the join command, that's really handy. – Mad Scientist Sep 29 '10 at 7:27
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.