- What’s a command line way to find large files/directories to remove and free up space?
- 13 Answers 13
- How to Find Out Top Directories and Files (Disk Space) in Linux
- How to Find Biggest Files and Directories in Linux
- Find Largest Directories in Linux
- Find Out Top File Sizes Only
- Related Posts
- 28 thoughts on “How to Find Out Top Directories and Files (Disk Space) in Linux”
- Finding the Biggest Files and Folders in Linux Command Line
- How to find the biggest folders in Linux?
- Adding Options
- Human-Readable Output
- Corrected Human-Readble Output
- Where are the largest files?
What’s a command line way to find large files/directories to remove and free up space?
What’s odd is I have two servers that are running the same thing. One is at 50% disk usage and the other is 99%. I can’t find what’s causing this.
13 Answers 13
If you just need to find large files, you can use find with the -size option. The next command will list all files larger than 10MiB (not to be confused with 10MB):
If you want to find files between a certain size, you can combine it with a «size lower than» search. The next command find files between 10MiB and 12MiB:
find / -size +10M -size -12M -ls
apt-cache search ‘disk usage’ lists some programs available for disk usage analysis. One application that looks very promising is gt5 .
From the package description:
Years have passed and disks have become larger and larger, but even on this incredibly huge harddisk era, the space seems to disappear over time. This small and effective programs provides more convenient listing than the default du(1). It displays what has happened since last run and displays dir size and the total percentage. It is possible to navigate and ascend to directories by using cursor-keys with text based browser (links, elinks, lynx etc.)
On the «related packages» section of gt5, I found ncdu . From its package description:
Ncdu is a ncurses-based du viewer. It provides a fast and easy-to-use interface through famous du utility. It allows to browse through the directories and show percentages of disk usage with ncurses library.
How to Find Out Top Directories and Files (Disk Space) 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 unnecessary junk and free up from your hard disk.
This brief tutorial describes how to find the largest files and folders in the Linux file system using du (disk usage) and find command. If you want to learn more about these two commands, then head over to the following articles.
How to Find Biggest Files and Directories in Linux
Run the following command to find out top biggest directories under /home partition.
# du -a /home | sort -n -r | head -n 5
The above command displays the biggest 5 directories of my /home partition.
Find Largest Directories in Linux
If you want to display the biggest directories in the current working directory, run:
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 the 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.
The above command will show the top directories, which are eating up more disk space. If you feel that some directories are not important, you can simply delete a few sub-directories or delete the entire folder to free up some space.
To display the largest folders/files including the sub-directories, run:
Find out the meaning of each option using in above command:
- du command: Estimate file space usage.
- -h : Print sizes in human-readable format (e.g., 10MB).
- -S : Do not include the size of subdirectories.
- -s : Display only a total for each argument.
- sort command : sort lines of text files.
- -r : Reverse the result of comparisons.
- -h : Compare human readable numbers (e.g., 2K, 1G).
- head : Output the first part of files.
Find Out Top File Sizes Only
If you want to display the biggest file sizes only, then run the following command:
# find -type f -exec du -Sh <> + | sort -rh | head -n 5
To find the largest files in a particular location, just include the path beside the find command:
# find /home/tecmint/Downloads/ -type f -exec du -Sh <> + | sort -rh | head -n 5 OR # find /home/tecmint/Downloads/ -type f -printf "%s %p\n" | sort -rn | head -n 5
The above command will display the largest file from /home/tecmint/Downloads directory.
That’s all for now. Finding the biggest files and folders is no big deal. Even a novice administrator can easily find them. If you find this tutorial useful, please share it on your social networks and support TecMint.
I am an experienced GNU/Linux expert and a full-stack software developer with over a decade in the field of Linux and Open Source technologies
Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.
Related Posts
28 thoughts on “How to Find Out Top Directories and Files (Disk Space) in Linux”
I need a shell script to display the largest folder in the server directory and user information. Reply
$ du -s * | sort -rn | awk '' drwxr-xr-x 33 tecmint tecmint 4096 Nov 16 10:46 .
Sorry for the two mistakes. First, if running as root, wildcard («*») expansion includes “dot directories“, and current dir («.») would always be the biggest one since it includes all others. And there was also a mistake in the awk statement. So this should do the trick: $ find . -mindepth 1 -maxdepth 1 -type d | xargs du -s * | sort -rn | awk ‘
$ find . -mindepth 1 -maxdepth 1 -type d | xargs du -s * | sort -rn | awk '' du: cannot access './VirtualBox': No such file or directory du: cannot access 'VMs': No such file or directory drwxr-xr-x 17 tecmint tecmint 184320 Nov 16 14:52 Downloads
@Ravi Sorry for replying here; the “reply link” is missing from your latest comment. > “but the command giving an error, see below” sorry again, remove the «*» from «xargs du -s *» .
The fixed command is :
$ find $HOME -mindepth 1 -maxdepth 1 -type d | xargs du -s | sort -rn | awk ‘
Thank you sharing this. I was trying to find these commands from ages and now I got the write answer Reply
It appears that if you use -h (human readable) with du, the sort gets confused as it doesn’t interpret the decimal unit prefix (i.e. the G, M, or K) properly, and you get the largest displayed numeric value. For example: 9G, 10K, 8K, 4M, 7G would sort to 10K, 9G, 8K, 7G, 4M. I noticed this when I got the following results:
# du -h /data.source/ | sort -n -r | head -n 5 1020K /data.source/mod_tile/default/9/0/0/1/176 1020K /data.source/mod_tile/default/10/0/0/1/178 1016K /data.source/mod_tile/default/11/0/0/35/65 996K /data.source/mod_tile/default/9/0/0/16/170 992K /data.source/mod_tile/default/9/0/0/1/160
# du /data.source/ | sort -n -r | head -n 5 680595148 /data.source/ 597114072 /data.source/pgsql 597114068 /data.source/pgsql/9.6 597114060 /data.source/pgsql/9.6/data 595032120 /data.source/pgsql/9.6/data/base
Hello Ravi, My questioned is to find the big file for particular date says we have thousand files of in folder with different date and need to find the biggest file of december 2016. Reply
Finding the Biggest Files and Folders in Linux Command Line
Quick tutorial to show you how to find the biggest files on your Linux machine using a few commands that you may already be familiar with du, sort, and head.
This is a quick tutorial to show you how to find the biggest files on your Linux machine using a few commands that you may already be familiar with du, sort, and head.
To find the 10 biggest folders in current directory:
To find the 10 biggest files and folders in current directory:
du -ah | sort -hr | head -n 10
Read the rest of the article to get a detailed explanation of these commands.
How to find the biggest folders in Linux?
The du command is used for getting the disk usage. Sort command sorts the data as per your requirement. The head command displays the top lines of a text input source.
This is just one combination for getting the biggest files and directories in Linux command line. There can be several other ways to achieve the same result.
What happens if you run these three commands together without options? Your output probably won’t be very useful.
When you run these commands, unless specified with du, everything will run automatically using the current working directory as the source file.
Sort without options arranges items in numerical order, but this behavior is a little strange. 100 is considered less than 12 because 2 > 0. That’s definitely not what we want.
Head here defaults to displaying the first 10 items. Depending on the directory you want to analyze, you can tailor this to find large files quickly.
[email protected]:~$ du | sort | head 100 ./.local/share/evolution/addressbook 108 ./.mozilla/firefox/jwqwiz97.default-release/datareporting 112 ./.local/share/gvfs-metadata 12 ./.cache/fontconfig 12 ./.cache/gnome-software/screenshots/112x63 12 ./.cache/thumbnails/fail 12 ./.config/dconf 12 ./.config/evolution 12 ./.config/gnome-control-center/backgrounds 12 ./.config/ibus
Adding Options
So let’s look at what might be more typical options.
Adding -n to sort command means that items will be sorted by numeric value. Adding -r means that the results will be reversed. This is what we want when searching for the largest number.
I’m also going to add -5 to limit our results further than the default for head. This value is something that you should decide based on what you know about the system.
You may want to expand the value to a number greater than 10, or omit it entirely if there are many large files you are trying to filter. Otherwise, you may run it, delete several files, but still have space issues.
Okay, let’s put it all together and see what happens.
[email protected]:~$ du | sort -nr | head -5 1865396 . 1769532 ./Documents 76552 ./.cache 64852 ./.cache/mozilla 64848 ./.cache/mozilla/firefox
That’s better, you can quickly see where the largest files are. You can do better, though. Let’s clean it up with some more options.
Human-Readable Output
The human options for certain commands help present numbers in a way that is familiar to us. Let’s try adding that to the du command.
[email protected]:~$ du -h | sort -nr | head -5 980K ./.local/share/app-info 976K ./.local/share/app-info/xmls 824K ./.cache/thumbnails 808K ./.cache/thumbnails/large 804K ./.local/share/tracker
Corrected Human-Readble Output
Wait a second… Those numbers don’t make any sense. No, they don’t because You have only changed the content to human-readable for the du command. Sort has its own built-in function for human-readable numeric sort with -h. Both must be used to get the desired output. You can run into these kinds of issues often in Linux.
It’s important to experiment and make sure that your results “make sense” before using a command a specific way.
[email protected]:~$ du -h | sort -hr | head -5 1.8G . 1.7G ./Documents 75M ./.cache 64M ./.cache/mozilla/firefox/jwqwiz97.default-release 64M ./.cache/mozilla/firefox
Where are the largest files?
You can tell from the output that the Documents folder contains some larger files, but if you switch to that folder and run our command again, you don’t get the largest file. You get this:
[email protected]:~/Documents$ du -h | sort -hr | head -5 1.7G .
This is just telling us what you already know. The current directory, referred to as . , has 1.7G worth of files. That isn’t helpful if you’re trying to find single, unusually large files.
You need to add another flag to du for this task. Using option -a, you can get the output that we’re looking for. Let’s try it.
[email protected]:~/Documents$ du -ah | sort -hr | head -5 1.7G . 1.1G ./1gig-file.file 699M ./doc.tar 2.9M ./photo-of-woman-wearing-turtleneck-top-2777898.jpg 1.4M ./semi-opened-laptop-computer-turned-on-on-table-2047905.jpg
Did you enjoy this guide to finding large files in Linux? I hope all of these tips taught you something new.
If you like this guide, please share it on social media. If you have any comments or questions, leave them below.