Find Largest Files in Linux – Bash Script

September 6, 2012     |     How To Guides,Linux

Pinterest
Print Friendly

On Linux, this is not uncommon to face the problem of disk space crunch. Most of the time this happens as some files occupy large amount of disk space. In such situations, one of the primary objective is to find largest files available on your Linux system.

This article provides a shell script to make this job easy. This script finds the top x largest files available in a specific folder on your Linux system and provides the following information for these files.

  • File size in bytes
  • Percentage of total disk space occupied by the file
  • File Owner
  • Last modified time of the file
  • File name along with full path

The script accepts two mandatory arguments.

  1. The folder you want to search for the largest files.
  2. A number x for extracting top x largest file.

The script has been tested on RHEL5.3.

Bash Script To Find Largest Files in Linux

# cat get_largest_files.sh 
#!/bin/bash
#
# Script Name: get_largest_files.sh
# http://www.thegeekscope.com/linux-script-to-find-largest-files/
#        
# This script is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
#
# Copyright www.thegeekscope.com
# Author: Kam Aggarwal, (kam@thegeekscope.com)
# Read the terms of use at http://www.thegeekscope.com/terms-of-use/
#

if [ "$1" = "" -o "$2" = "" ]
then
        echo
        echo "Usage: $0 FOLDER_NAME FILE_COUNT"
        echo
        echo       "FOLDER_NAME: Folder to be searched for largest files (with full path)"
        echo       "FILE_COUNT: Count of the largest files to fetch"
        echo
        echo "Example:  $0 /etc 5"
        echo

        exit 1
fi

SEARCH_DIR=$1
FILES_COUNT=$2

DISK_SIZE_BYTES=`df -PT -B 1 | awk '{if ($7 == "/")  print $3}'`

TEMP_FILE=/tmp/largest_files.tmp

printf "\n%-18s %-10s %-15s %-25s %s \n\n" "[SIZE (BYTES)]" "[% OF DISK]" "[OWNER]" "[LAST MODIFIED ON]" "[FILE]"

SUM=0

for FILE in `find $SEARCH_DIR -type f -exec du {} \; | sort -rn | head -$FILES_COUNT | awk '{print $2}'`
do
                FILE_SIZE_BYTES=`stat --print %s $FILE`
        OWNER=`stat --printf %U $FILE`
                LAST_MODIFIED_ON=`stat --printf %y $FILE | cut -c 1-19`

                FILE_SIZE_BYTES1=`expr $FILE_SIZE_BYTES \* 100`
                PERCENTAGE=`expr $FILE_SIZE_BYTES1 / $DISK_SIZE_BYTES`%

                printf "\n%-18s %-12s %-14s %-25s %s"  "$FILE_SIZE_BYTES" "$PERCENTAGE" "$OWNER" "$LAST_MODIFIED_ON" "$FILE"

                SUM=$(($SUM+$FILE_SIZE_BYTES))
done

echo
echo

SUM1=`expr $SUM \* 100`
TOTAL_PERCENTAGE=`expr $SUM1 / $DISK_SIZE_BYTES`%

echo "Total disk size: $DISK_SIZE_BYTES Bytes"
echo "Total size occupied by these files: $SUM Bytes  [ $TOTAL_PERCENTAGE of Total Disc Space  ]"

echo

echo "*** Note: 0% represents less than 1% ***"

echo

Script Usage

Download the script get_largest_files.sh and transfer it to your Linux system. On my Linux system, this script resides in /root folder.

Provide executable permission to the script.

# chmod 755 /root/get_largest_files.sh

Bingo!!! You are ready to use the script. Following examples demonstrate the script usage.

1. Find Top 5 Largest Files Available on Linux System

# sh get_largest_files.sh / 5

[SIZE (BYTES)]     [% OF DISK] [OWNER]         [LAST MODIFIED ON]        [FILE] 

56421808           0%           root           2012-08-02 14:58:51       /usr/lib/locale/locale-archive
32464076           0%           root           2008-09-18 18:06:28       /usr/lib/libgcj.so.7rh.0.0
29147136           0%           root           2012-08-02 15:17:40       /var/lib/rpm/Packages
20278904           0%           root           2008-12-09 13:57:01       /usr/lib/xulrunner-1.9/libxul.so
16001944           0%           root           2012-08-02 15:02:36       /etc/selinux/targeted/modules/active/base.linked

Total disk size: 23792652288 Bytes
Total size occupied by these files: 154313868 Bytes  [ 0% of Total Disc Space  ]

*** Note: 0% represents less than 1% ***

2. Find Top 10 Largest Files Available in /root folder

# sh get_largest_files.sh /root 10

[SIZE (BYTES)]     [% OF DISK] [OWNER]         [LAST MODIFIED ON]        [FILE] 

913608             0%           root           2012-08-25 17:33:33       /root/test.h.gch
73164              0%           root           2012-09-18 11:06:02       /root/mem.log
38772              0%           root           2012-08-02 15:17:39       /root/install.log
22455              0%           root           2012-09-17 21:09:15       /root/.bash_history
4890               0%           root           2012-08-25 17:33:33       /root/a.out
170                0%           root           2012-09-03 20:10:44       /root/userinfo.sh
385                0%           root           2012-09-07 01:02:02       /root/test.sh
23                 0%           root           2012-08-25 16:58:19       /root/test.h
175                0%           root           2012-08-25 17:04:46       /root/test.c.good
134                0%           root           2012-08-25 17:32:57       /root/test.c

Total disk size: 23792652288 Bytes
Total size occupied by these files: 1053776 Bytes  [ 0% of Total Disc Space  ]

*** Note: 0% represents less than 1% ***
Pinterest
Don't Miss a Post ! Subscribe to Our Free Email Newsletter.

{ 17 comments… read them below or add one }

Deepak Tripathi September 6, 2012 at 8:50 am

Amazing website …

Reply

Nihal September 11, 2012 at 4:27 pm

Good post… thanks

Reply

Boby September 16, 2012 at 12:43 am

nice, thank you so much ;)

Reply

reynaldodgomez blog October 8, 2012 at 5:26 pm

thanks, nice post.

Reply

Aryan October 12, 2012 at 3:33 am

Very useful article.

thank you

Reply

Imtiaz October 16, 2012 at 11:10 am

This is the best article I have read, thank you

Reply

Anonymous October 19, 2012 at 8:38 am

This is a great post. Thanks so much for sharing, like always.

Reply

Anonymous October 22, 2012 at 10:06 pm

Thanks for such a great post and the review, I am totally impressed!

Reply

Pranav October 25, 2012 at 6:33 pm

THX for sharing.good website.

Reply

Anonymous October 27, 2012 at 11:27 am

I really appreciate your help, it is very useful for me.

Reply

Andrik October 28, 2012 at 3:27 am

Thank you very much. This really helped me with my work. I appreciate your help.

Reply

rkg78 October 29, 2012 at 3:00 am

Good website !

Reply

Sara October 30, 2012 at 3:26 am

nice and thanks.

Reply

WebTechLabs December 4, 2012 at 12:19 pm

Great script. It is a lot better than some of the other find commands that I’ve came across. I am getting some errors though:

find: /home/virtfs/{account}/proc/12731: No such file or directory
find: /home/virtfs/{account}/proc/12755: No such file or directory
find: /home/virtfs/{account}/proc/27442: No such file or directory

Reply

rianala cleverson October 15, 2013 at 8:03 am

great thanks

Reply

Thirupathi October 18, 2013 at 9:59 am

Very Helpful
Thank you

Reply

erm3nda October 25, 2013 at 2:00 pm

Good.

That is better than deal manually with the scripts.
Thanks.

Reply

Leave a Comment

{ 1 trackback }

Previous post:

Next post: