Linux:How To Find The Largest Files/Directories In A Directory

You must often encounter the problem of insufficient disk space in linux system as a linux admin, At this time, what you do for this problem? I think you need to check of  which the biggest files or directories occupied  on your disk, so you can decide if some files can be removed or moved to others disk or partication to get space. so now the problem is how to find the biggest files or directories in linux or a directory.

Find The Largest Files In Current Directory

To find the largest files in a given directory or in the current directory, you can use “ls” command with “-S” option to list all the largest files , the option “-S” will sort files in descending order by their size. issue the following command:

ls -lS .

or

ls -ls /path

output:

[root@osetc~]# ls -lS

total 3808

-rw-------  1 root root 3885425 May 18 03:21 nohup.out

drwxr-xr-x  3 root root    4096 Aug 18  2013 cn

-rw-rw-r--+ 1 root root      14 May  6 17:30 test1

Find The Larget Directories In Current Directory

To find the largest directories in current directory or given directory, you need to use “du” command with “-S” option and sort command with “-nr” option, issue the following commad:

du -S . | sort -nr

you can give a target directory to list all largest directries in it, type:

du -S /path | sort -nr

output:

[root@osetc~]# du -S /home | sort -nr

20      /home/user1

16      /home/user2

16      /home/mysql

4       /home

[root@osetc~]# du -S . | sort -nr

3872    .

8       ./.ssh

8       ./cn/test/scr

4       ./cn/test

4       ./cn

Find The Largest Files In A Directory recursively

To find the largest file in a directory and its subdirectories , you should use find command and combine with sort command , issue the following command to find all files and show up the first 10 largest files in current directory:

find .  -type f  | sort -nr | head -n 10

output:

[root@osetc var]# find . -type f | sort -nr | head -n 10

./spool/plymouth/boot.log

./spool/mail/www

./spool/mail/user2

./spool/mail/user1

./spool/mail/mysql

./spool/anacron/cron.weekly

./spool/anacron/cron.monthly

./spool/anacron/cron.daily

./run/utmp

./run/syslogd.pid

done…..

You might also like:

Sidebar



back to top