Announcement

Collapse
No announcement yet.

Finding the largest files and directories in Linux

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Finding the largest files and directories in Linux

    As a Linux administrator, you must periodically check which files and folders are consuming more disk space. It is very necessary to find the unnecessary junks and free up them from your hard disk.

    There are multiple way to achieve the same. I will be mentioning few of them:


    Run the following command to find out top biggest directories under /home partition.
    Code:
    # du -a /home | sort -n -r | head -n 5

    The above command displays the biggest 5 directories of my /home partition.

    If you want to display the biggest directories in the current working directory, run:
    Code:
    # du -a | sort -n -r | head -n 5

    Let us break down the command and see what says each parameter.

    du command: Estimate file space usage.
    a : Displays all files and folders.
    sort command : Sort lines of text files.
    -n : Compare according to string numerical value.
    -r : Reverse the result of comparisons.
    head : Output the first part of files.
    -n : Print the first ‘n’ lines. (In our case, We displayed first 5 lines).
    Some of you would like to display the above result in human readable format. i.e you might want to display the largest files in KB, MB, or GB.

    Code:
    # du -hs * | sort -rh | head -5


    To display the largest folders/files including the sub-directories, run:
    Code:
    # du -Sh | sort -rh | head -5


    If you want to display the biggest file sizes only, then run the following command:
    Code:
    # find -type f -exec du -Sh {} + | sort -rh | head -n 5


    To find the largest files in a particular location, just include the path besides the find command:
    Code:
    # find /home/geekworld/Downloads/ -type f -exec du -Sh {} + | sort -rh | head -n 5
    OR
    # find /home/geekworld/Downloads/ -type f -printf "%s %p\n" | sort -rn | head -n 5


    This nifty command allows you to built up a list of the largest files and directories:

    Code:
    [B]FS='/';clear;date;df -h $FS; echo "Largest Directories:"; du -hcx –max-depth=2 $FS 2>/dev/null | grep [0-9]G | sort -grk 1 | head -15 ;echo "Largest Files:"; nice -n 19 find $FS -mount -type f -print0 2>/dev/null| xargs -0 du -k | sort -rnk1| head -n20 |awk '{printf "%8d MB\t%s\n",($1/1024),$NF}'[/B]
    You’ll need to adjust it, depending on which directory you wish to look within. For example, if you’re looking for a list of the largest files and folders in the /home directory, you’d use:

    Code:
    [B]FS='/home';clear;date;df -h $FS; echo "Largest Directories:"; du -hcx –max-depth=2 $FS 2>/dev/null | grep [0-9]G | sort -grk 1 | head -15 ;echo "Largest Files:"; nice -n 19 find $FS -mount -type f -print0 2>/dev/null| xargs -0 du -k | sort -rnk1| head -n20 |awk '{printf "%8d MB\t%s\n",($1/1024),$NF}'[/B]
Working...
X