howtouselinux

3 Ways to find largest file in Linux

Table of Contents

 

In this blog post, we will discuss three different ways to find the largest file on a Linux system. This is a handy trick to know if you are running out of disk space and need to free up some space. We will be using the “du” command, the “find” command, and the “ls” command to find our largest files. Let’s get started!

The following commands can be used to find the largest file in Linux.

  • du -sh * | sort -rh | head -5
  • ls -lSh /bin | head -5
  • find ./ -type f -exec du -sh {} \; |sort -h|tail -5
  • du -ah /home | sort -h -r | head -n 5
  • find $directory -type f -exec ls -s {} \; | sort -n | tail -n 5

 

Using du Command to Find the Largest File in Linux

The best way to find the largest file in Linux is using du -sh * | sort -rh | head -5. Open the terminal and type this command. It will list the largest file and directories under current directory.

  • du -sh: display file and director size in human-readable format
  • sort -rh:  Reverse the result based on human readable numbers
  • head -5: display first 5 largest files

 

Du command will check size for files and directories. If you need to list file size only, check the find command below.

Here are more Linux commands to find the largest files in Linux.

Find Largest Files and Directories under a specific directory and subdirectory
# du -ah /home | sort -h -r | head -n 5
Find Largest Files and Directories under the current working directory and subdirectory
# du -ah | sort -h -r | head -n 5

Find Largest Directories or Files in the current working directory
# du -sh * | sort -rh | head -5
Find Largest Directories or Files in / directory
# du -ahx / | sort -rh | head -5

We use the following commands in our examples.

  • du command : It estimates file space usage
  • sort command : Sort lines of text files or given input data
  • head command : Output the first part of files i.e. to display first 10 largest file

We use the following options in our examples.

  • du command -h option : display file sizes in human-readable format, in Kilobytes, Megabytes and Gigabytes.
  • du command -s option : Show total for each argument.
  • du command -x option : Skip directories. (if on different file systems)
  • sort command -r option : Reverse the result of comparisons.
  • sort command -h option : Compare the numbers.
  • head command -10 OR -n 10 option : Displays the first 10 lines of the output.

 

Using ls Command to Find the Largest File in Linux

The ls command is one of the most basic commands in Linux, and it is used to list the contents of a directory. By default, the ls command sorts files alphabetically, but you can also use it to sort files by size, by date, or by other attributes.

  • If you want to sort the output by file size, you can use the -lS option. This will show you the files in a long list format and sort them by size.
  • If you want to see the human readable form of file sizes, you can use the -lhS option. This will show you the files in a long list format and sort them by human readable file size.
  • If you want to see the reverse order of file sizes, you can use the -lrhS option. This will show you the files in a long list format and sort them by reverse order of file size.

 

ls -lSh /bin | head -5

Using Find Command to Find the Largest File in Linux

The find command in Linux is a command line utility for walking a file hierarchy. It can be used to find files and directories and perform subsequent operations on them. It supports searching by file, folder, name, creation date, modification date, owner and permissions. By using the ‘-exec’ other Linux commands can be executed on files or folders found.

To find the largest file on a Linux system using the find command, you can use the following syntax:

find /path/to/directory -type f -exec du -hs {} \;

This will list all of the files in the specified directory and will print out the size of each file in human-readable format. You can then sort the output by size to find the largest files.

If you want to find the largest files on the entire system, you can use the following command: find / -type f -exec du -hs {} \; | sort -rh | head -n 15

  • Find the largest files under the current directory: find ./ -type f -exec du -sh {} \; |sort -h|tail -5
  • Find the largest directory under the current directory: find ./ -type d -exec du -sh {} \; |sort -h|tail -5
  • find / -size +100M -ls
  • find $directory -type f -exec ls -s {} \; | sort -n | tail -n 5

10 Must-know Tips to Delete Files in Linux

10 Find Exec examples – Advanced Part

Share on facebook
Facebook
Share on twitter
Twitter
Share on linkedin
LinkedIn
Liquid Web Managed Word Press