Linux find biggest 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.)

Screenshot of gt5

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.

Источник

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.

Finding Large Files Linux

What happens if you run these three commands together without options? Your output probably won’t be very useful.

Читайте также:  Openshift install oc linux

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.

Читайте также:  Как запустить nano в linux

If you like this guide, please share it on social media. If you have any comments or questions, leave them below.

Источник

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 

Источник

Читайте также:  Cracking password using kali linux

Поиск больших файлов 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.

Источник

Оцените статью
Adblock
detector