Tuesday, March 27, 2012

Sort Files According To Their Size

With few basic commands in Linux box, we can generate a report of files according to ascending or descending order of their size.

Let's start by displaying all the files from a folder:
find ~/testFolder -type -f 
To get their size lets pass each file to du command:
find ~/testFolder -type -f -exec du -s {} \;
Now lets sort them according to their size in numeric order, taking first column as file size and in reverse order:
find ~/testFolder -type -f -exec du -s {} \; | sort -k1nr
Let's save the output to a file size.txt:
find ~/testFolder -type -f -exec du -s {} \; | sort -k1nr > ~/size.txt 

0 comments:

Post a Comment