- Поиск больших файлов Linux
- Поиск больших файлов Linux
- 1. GDMap
- 2. Утилита ncdu
- 3. Утилита du
- 4. Утилита find
- Выводы
- 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
- 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?
- 13 Answers 13
Поиск больших файлов Linux
Если на вашем жёстком диске закончилось свободное пространство, самый простой способ его освободить — найти и удалить самые большие и при этом ненужные файлы. Такая задача может возникнуть как на сервере, так и на домашнем компьютере, поэтому существуют удобные решения для обоих ситуаций. Способов поиска больших файлов существует очень много.
Как вы уже поняли, в этой небольшой инструкции мы рассмотрим, как найти большие файлы Linux с помощью графического интерфейса или консольных утилит. Будем двигаться от самого простого к более сложному.
Поиск больших файлов Linux
1. GDMap
Несмотря на то, что графических утилит есть около десятка, все они мне не очень нравятся. Например в Gnome можно использовать GDMap, а в KDE — fileslight. Обе утилиты сканируют файловую систему и выводят все файлы в виде диаграммы. Размер блока зависит от размера файла. Чем больше файл или папка, тем больше блок. Для установки GDMap в Ubuntu выполните:
Затем запустите утилиту из главного меню. По умолчанию она отображает домашнюю папку. Здесь можно оценить, какие файлы самые увесистые.
2. Утилита ncdu
Это псевдографическая утилита, которая работает в терминале Linux. Она отображает список файлов и директорий по объёму и, что самое интересное, тут же позволяет удалять ненужные файлы. Для установки утилиты выполните:
Затем запустите утилиту, передав ей в качестве параметра папку, которую надо просканировать. Можно проверить ту же домашнюю папку:
У утилиты очень простое управление. Для перемещения по списку используйте кнопки со стрелками вверх и вниз, для открытия папки — клавишу Enter, а для удаления файла — кнопку d. Также можно использовать для перемещения кнопки в Vim стиле — h, j, k, l.
3. Утилита du
Если у вас нет возможности устанавливать новые утилиты, может помочь установленная по умолчанию во всех дистрибутивах утилита du. С помощью следующей команды вы можете вывести 20 самых больших файлов и папок в нужной папке, для примера снова возьмём домашнюю папку:
sudo du -a /home/ | sort -n -r | head -n 20
Мы не можем использовать опцию -h для вывода размера в читабельном формате, потому что тогда не будет работать сортировка.
4. Утилита find
С помощью команды find вы тоже можете искать большие файлы Linux. Для этого используйте опцию -size. Например, давайте найдём файлы, которые больше 500 мегабайтов в той же домашней папке:
sudo find /home -xdev -type f -size +500M
Можно пойти ещё дальше — вывести размер этих файлов и отсортировать их по размеру:
find / -xdev -type f -size +100M -exec du -sh <> ‘;’ | sort -rh
Самые большие файлы Linux будут сверху, а более мелкие — ниже.
Выводы
В этой небольшой статье мы разобрались, как выполняется поиск больших файлов Linux. После того, как вы их нашли, остаётся выбрать ненужные и удалить, если подобное происходит на сервере, то, обычно, это логи различных сервисов или кэш. Обратите внимание, что после удаления файлов место в файловой системе может и не освободится. Для полного освобождения места следует перезагрузить компьютер. Это довольно частая проблема на серверах и VPS.
Обнаружили ошибку в тексте? Сообщите мне об этом. Выделите текст с ошибкой и нажмите Ctrl+Enter.
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.
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.
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.