In this post we will learn how to find large files in Linux command line. You can use find/du command to find largest files Linux (recursive) or find largest directories in Linux terminal.
The du command is used to get an estimate of file space usage, it displays the amount of disk space used by the set of specified files and for each subdirectory.
The du command is also used to display the sizes of individual files or directories on a Linux system and then sort theme according to their size.
When executed the du command shows the sizes of all directories in the current working directory.
The basic usage of the du command is:
$ du -sh *
The command options -s is used to display only the total size of each file or directory, and the command option h is used to display the output in human-readable format (in KB, MB, or GB).
NOTE: The above command will display the list of the directories based on their alphabetical order.
If you want to display the result sorted by their size, you the du command together with the sort command.
$ du -sh * | sort -rh
In the command above the sort command is used to sort the display output in human-readable format (-h) and in the reverse numerical order (-r). The sort command will display the result in descending order.
Find largest files in Linux (recursive)
Open Terminal and type the following command to find out top 10 largest file or directories in Ubuntu systems:
$ du -a /var | sort -n -r | head -n 10
The simplest usage of the sort command is to display the files and directories by size, from largest to smallest.
To do so, run the following command:
$ du | sort –n –r
Find large files in Linux using find command
To find files and directories larger than a specific size, say to find files size greater than 100mb in Linux you can use the find command with the -size option.
For example, to find all files larger than 100 MB in the current directory including its sub-directories, run the following command:
$ find . -type f -size +100M -exec ls -lh {} \; | awk '{ print $5 ": " $NF }' | sort -n -r | head
NOTE:
- The -type f command option specifies that we only want to find files and do not want to include directories or its sub-directories.
- The -size +100M option is used to find files larger than 100 MB. We can always change the size as per our need.
Another thing to note here is that if you want to find large directories instead of the files, replace -type f with -type d to search for directories. So the command becomes:
$ find . -type d -size +100M -exec ls -lh {} \; | awk '{ print $5 ": " $NF }' | sort -n -r | head
I hope this you find this tutorial useful to sort files by size using du command, sort command and find command.