- 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
- Linux Command , how to find files by size larger than x?
- 4 Answers 4
- 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.
Linux Command , how to find files by size larger than x?
I’m trying to find files with size larger than «x» , ex: 32 bytes But what I found in ls —help was only ls -S , that just sort by size and not satisfied my demand I’m new to Linux , I’ve tried Google but I don’t know what keywords should I use to find answer , could somebody give me advice , thank you !
Your question is not about programing but about linux command that would be better in serverfault.com. If you want to ask something about shell script please read this first. stackoverflow.com/help/mcve
4 Answers 4
Try to use the power of find utility.
Find files greater than 32 bytes:
Of course you could also ask for files smaller than 32 bytes:
And files with exact 32 bytes:
For files with 32 bytes or more:
Or not smaller than 32 bytes (the same as above, but written another way):
And files between 32 and 64 bytes:
And you can also apply other common suffixes:
If you get find: illegal option — i error, specify the folder to search in after find . Add a dot to find files in the current folder: find . -size +32k -size -64M
You can change 32 for the size you want. In this case, I used your example.
Both answers you two provided me are excellent 😀 But I think I will give @rslemos the best answer tag since he’s «1 minute earier» , but still thanks to you !
Explanation: Use unix command find Using -size oprator
The find utility recursively descends the directory tree for each path listed, evaluating an expression (composed of the ‘primaries’ and ‘operands’) in terms of each file in the tree.
Solution: According to the documentation
-size n[ckMGTP] True if the file's size, rounded up, in 512-byte blocks is n. If n is fol- lowed by a c, than the primary is true if the file's size is n bytes (charac- ters). Similarly if n is followed by a scale indicator than the file's size is compared to n scaled as: k kilobytes (1024 bytes) M megabytes (1024 kilobytes) G gigabytes (1024 megabytes) T terabytes (1024 gigabytes) P petabytes (1024 terabytes)
Usage: perform find operation with -size flag and threshold measurement arguments: more than(+)/less than(-), number(n) and measurement type (k/M/G/T/P).
Formula: find -size [+/-]
1.Greater Than — Find all files in my current directory (.) that greater than 500 kilobyte
2.Less Than — Find all files in my current directory (.) that less than 100 megabyte.
3.Range — Find specific file (test) in my current directory (.) that greater than 500 kilobyte less than 100 megabyte (500k-1000k)
find . -name "test" -size +500k -size -100M
4.Complex Range with Regex Find all .mkv files in all filesystem (root /) that are greater than 1 gigabyte and created this month, and present info of them.
find / -name "*.mkv" -size +1G -type f -ctime -4w | xargs ls -la
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.