Freeform it all discovery nitracle. To get a list with the size of each item in a folder, you’ll want to use the du command like this: du -sm. The -m argument will return the listing in megabytes (note that you can use -h for human readable, but it won’t sort correctly) Now we will want to run this through the sort command, sorting in reverse order -r and numeric -n. To sort a Unix / Linux directory listing by file size, you just need to add one or more options to the base ls. On Mac OS X (which runs a form of Unix) this command works for me: ls -alS That lists the files in order, from largest to smallest. To reverse the listing so it shows smallest to largest, just add the 'r' option to that command: ls -alSr.

One of the common problem while working in Linux is to find large files to free some space. Suppose, your file system is full and you are receiving an alert to remove spaces or if your host is run out of space and your server is not starting up, the first thing you do is find top 10 largest files and see if you can delete them. Usually, old files, large Java heap dumps are good candidates for removal and freeing up some space. If you are running Java application e.g. core Java-based programs or web application running on Tomcat then you can remove those heap dump files and free some space, but the big question is how do you find those? How do you know the size of the biggest file in your file system, especially if you don't know which directory it is? We'll try to find answers for some of those questions in this article.
When I was new to Linux, I don't have any other choice but to go to the log directory and look for old files which are larger among rest and delete them. They worked well until one day our server died due to a huge cache file.
I wasn't able to locate that because it wasn't in the log directory, then I come to know about the find command which let you search sub-directories for large files as shown below:
$ find . -size +1G
Du Sort By SizeThis command will print all the files which are greater than 1GB from current directory and any subdirectory.
The only problem with this one is that it doesn't print the exact size. The problem was solved by using the -printf option, which allows you to specify a format String much like Java's printf() method.

1. Finding Big files using find command in Linux

You can further tweak the command to find files up-to certain size e.g. below command will find all files. Here is the modified UNIX command to find large files with size :
$ find . -size +1G -printf '%s %pn'
here is %s is for size and %p are for the path.
Alternatively, You can also use -exec option to run ls on each file the find command return to print its size as shown below:
$ find . -size +100M -exec ls -sh {} ;
This is good enough, you can just see which files you can delete and free some space, but problem is that you will not find any file which is larger than 1GB, hence I always use this command with some hypothetical large number e.g. 10GB etc, but, those are just workaround, not the proper fix. Let's see what we can do next.
Btw, if you are new to find then I suggest you get familiar with its different option as it's a very important and powerful command. I suggest you check Linux Command Line Interface (CLI) Fundamentals to learn more about various options of find command in Linux.
Alternatively, you can also take a look at below diagram from Julia Evans which is good to remember useful find command options:

2. Finding large file using du command in Linux

Btw, you can also use the du (disk usage) command to find large directories and their size, as shown below :
$ du -a . sort -n -r head -n 10
16095096 .
13785288 ./logs
6095380 ./logs/app
2125252 ./temp
2125244 ./temp/data
2125240 ./temp/data/app
This is the right command, it will list both directories and file. I have also combined the output of the du command with sort command to print the top 10 largest file and directories.

Ubuntu Du Sort By Size

This is exactly what we are looking for. In fact, this is also one of the frequently asked Linux Interview questions, so if you know this trick you answer this question on interviews as well.
As I have said, a good knowledge of various Linux commands is very important for a programmer working in a Linux machine. I know you can always Google things but you have to know what to Google and that's why basic information about various Linux command is essential.
If you feel that you don't know enough Linux commands then you can join a comprehensive course like Linux Command Line Basics to get hold of the Linux commands which matter most.
That's all about how to find the large files and directories in Linux. As I said, earlier I used to search large files by using find command with -size option but that is more or less a guesswork because you never know the size of the largest file in a machine, but by using a reasonable high size, you can possibly find all big files in your filesystem.
One more command you can use to find the large files with size in Linux is the disk usage or du command, which will also list both files and directories.
Further Learning
Learn Linux in 5 Days and Level Up Your Career
10 example of networking command in UnixLinux sort files by size recursive
10 Example of tar commands in Unix

Linux Du Sort By Size Top 10

5 Example of kill commands in Unix and Linux
Linux Command Line Interface (CLI) Fundamentals
VI Editor examples and tips for beginners
The Linux Command Line: A Complete Introduction
Thanks a lot for reading this articleso far. If you like this UNIX command tips then please share with your friends and colleagues. If you have any question or comment then please drop a comment