I have a mounted hard-drive with multi-users like this:
/HDD1/user1
/HDD1/user2
/HDD1/user3
I'd like to look in each user's folder, find all the files that match an expression (say, "*.txt"
), and then sum the space used by all those files, and report it on a per user base:
user1: x bytes
user2: y bytes
user3: z bytes
I found the directories of all the files with:
find /HDD1/ -name "*.txt" | rev | cut -d"/" -f2- | rev | uniq > txtfiles.dat
I thought I'd use a loop to go through each line in txtfiles.dat
calculating the disk usage in each folder, but this seems very cumbersome. Is there a neater way to do this? Something like a du
that looks in each user's folder but only counting the files that match an expression?
Explanation: the
find
command takes care of filtering the file set, and specifying-print0
makes the output NUL-terminated.The
find
output is piped todu
. Specifying--files0-from -
indicates that file paths are read in a NUL-terminated stream from STDIN. Finally,-ch
instructsdu
to add a grand total line, in human-readable form.If all you want is the total, you can pipe the result to
tail -1
: