howtouselinux

3 Ways to find largest files in Linux

Table of Contents

In this blog post, we will discuss three different ways to find the largest files 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 the largest files. Let’s get started!

The following commands can be used to find the largest files.

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

 

Find the Largest Files with find command in Linux

You can easily find the largest files in Linux using this command.

find /path/to/directory -type f -exec du -hs {} \;| sort -rh | head -n 1

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

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, modification date, owner and permissions. By using the ‘-exec’ other Linux commands can be executed on files or folders found.

Let’s break down the command:

find /path/to/directory -type f -exec du -hs {} \;| sort -rh | head -n 1

  • find /path/to/directory -type f : list all the files under directory and sub directories
  • du -sh: display file size in human-readable format
  • sort -rh:  Reverse the result based on human-readable numbers
  • head -1: display the first largest file

 

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 1

This command will take a long time if you have many files on your server.

Here are more examples.

Find the largest files under the current directory including sub directory

find ./ -type f -exec du -sh {} \; |sort -rh|head -n 1

Find the largest directory under the current directory including sub directory

find ./  -maxdepth 1 -mindepth 1  -type d  -exec du -hs {} \;| sort -rh | head -n 1

Find the largest files under directory including sub directory

find $directory -type f -exec ls -s {} \; | sort -rn | head -n 1

 

Find the Largest Files with du command in Linux

The du command is used to estimate the space used by a file or directory. We can pipe the output of the du command to the sort command to sort the files by size. Then use the head command to print the first few lines of the output.

 du -sh * | sort -rh | head -1

Open the terminal and type this command. It will list the largest files and directories under the current directory.

We use the following commands in our examples.

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

 

This command is pretty useful when you need to find out which file or directory uses the most disk space under one specific directory.

For example, if we need to get the largest files under /etc directory, we can run the following commands.

cd /etc/
du -sh * | sort -rh | head -1

or

du -h -d 1 /etc/ | sort -rh | head -1

 

Find the Largest Files with ls command 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 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.

For example, we can use the following command to get the 5 largest files under /bin directory.

ls -lSh /bin | head -5

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.

 

 

2 ways to Delete Files in Linux

10 Advanced Find Exec examples

Welcome to howtouselinux.com!

Our website is dedicated to providing comprehensive information on using Linux.

We hope you find our site helpful and informative, and we welcome your feedback and suggestions for future content.

Learn More

Facebook
Twitter
LinkedIn