Dan Dan - 3 months ago 6x
Linux Question

Measure disk space of certain file types in aggregate

I have some files across several folders:


How can I measure the grand total amount of disk space taken up by all the .txt files in /home/d/?

I know du will give me the total space of a given folder, and ls -l will give me the total space of individual files, but what if I want to add up all the txt files and just look at the space taken by all .txt files in one giant total for all .txt in /home/d/ including both folder1 and folder2 and their subfolders like folder3?


this will do it:

for file in $(ls *.txt)
space=$(ls -l $file | awk '{print $5}')
let total+=space
echo $total