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. How to find smallest files or directories using Linux find command in linux system. how to list all the smallest files or directories on your linux system.
This guide will show you on how to find smallest files /directories by size under linux operating system.
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:
1 | ls -lSr |
If you just wnat to see the first 10 smallest file, you can use pipe line and follow to “head -n” command.
1 | ls -lSr | head -n 10 |
output:
1 2 3 4 5 6 7 8 9 10 11 | [root@itsprite 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:
1 | du -S . | sort -n | head -n 10 |
Output:
1 2 3 4 5 6 7 8 9 10 11 | [root@itsprite var]# 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 |
See Also:
1. Linux Find Command Examples
2. Linux find command
3. Linux find command manual page
Done….
联系客服