Linux:How To Find The Smallest Files/Directories In A Directory
Sometimes, Maybe you have some specific request as a linux admin, it is to find the smallest files or directory in a directory or in the current directory, or you need to know how to find the files or list all files or directories in a directory and sort by file size. this guide will show you on how to list all smallest files/directories by size.
Find The Smallest Files In A Directory
Often, if you want to list all files in a directory, you can use “ls” comand with a few options per your specific request. now the below example will show you how to use “ls” command with both “-S” and “-r” options to list the smallest files in a directory, where, “-r” options will reverse the sorting order of the listing. “-S” option will do sorting operation. Issue the following command to find the smallest files in the current directory:
ls -lSr
If you just wnat to see the first 10 smallest file, you can use pipe line and follow to “head -n” command.
ls -lSr | head -n 10
output:
[root@osetc var]# ls -lSr | head -n 10 total 64 lrwxrwxrwx. 1 root root 10 Dec 17 2012 mail -> spool/mail drwxr-xr-x. 2 root root 4096 Sep 23 2011 yp drwxrwxrwt. 2 root root 4096 Sep 24 15:38 tmp drwxr-xr-x. 8 root root 4096 Dec 17 2012 spool drwxr-xr-x. 10 root root 4096 Jun 27 18:04 run drwxr-xr-x. 2 root root 4096 Sep 23 2011 preserve drwxr-xr-x. 2 root root 4096 Sep 23 2011 opt drwxr-xr-x. 2 root root 4096 Sep 23 2011 nis drwxr-xr-x. 4 root root 4096 Oct 12 03:27 log
Find The Smallest Directories In A Directory
Using “du” command with “-S” options, which will put the size in kilobytes of each directory in the first column of output , you can give the target directory you want to search and pipe the output to sort command with ‘-n‘ option, type:
du -S . | sort -n | head -n 10
Output:
[root@osetcvar]# du -S . | sort -n | head -n 10 4 . 4 ./cache 4 ./cache/man/cat1 4 ./cache/man/cat2 4 ./cache/man/cat3 4 ./cache/man/cat4 4 ./cache/man/cat5 4 ./cache/man/cat6 4 ./cache/man/cat7 4 ./cache/man/cat8
Done….