Search code examples
linuxbashfindlsdu

How to display directory size of the n most recent additions in bash


I'd like to display the 10 most recently added directories, for instance with:

ls -tlh | head -20

However, I'd also like to include directory size with with du. But I can't seem to figure out how to get the size of only the 10 most recent additions - querying all dirs would take far too long.

du --max-depth=1 | head -20

..doesn't seem to work. So I'm looking for a way to display both date modified and directory size of the 10 most recently modified dirs. Is this possible?


Solution

  • This will print the directory size of the 10 most recently modified dirs:

    for dir in $(ls -t -c1 | head -20); do 
        echo $(du -sh $dir 2>/dev/null)
    done
    

    As a one liner:

    for dir in $(ls -t -c1 | head -20); do echo $(du -sh $dir 2>/dev/null) ; done