- Find and delete the zero size files and empty directories
- Zero size files
- Empty directories
- Поиск больших файлов Linux
- Поиск больших файлов Linux
- 1. GDMap
- 2. Утилита ncdu
- 3. Утилита du
- 4. Утилита find
- Выводы
- Remove files of 0 bytes in size via command line?
- 4 Answers 4
- Explanation
- Linux delete file with size 0 [duplicate]
- 8 Answers 8
- Efficiently delete large directory containing thousands of files
- 19 Answers 19
- Use rm -rf directory instead of rm -rf * .
Find and delete the zero size files and empty directories
How to, in the terminal, using the find utility, find and optionally delete all zero bytes/size/length files and empty directories in the specified directory including subdirectories.
Zero size files
To find all zero size files, simply use:
This commands will find all zero size files in the current directory with subdirectories and then print the full pathname for each file to the screen.
- The ./ means start searching from the current directory. If you want to find files from another directory then replace the ./ with the path to needed directory. For example, to search everything under the system log directory you need to replace ./ with /var/log .
- The -type f flag is specifies to find only files.
- The -size 0 and -empty flags is specifies to find zero length files.
To find and then delete all zero size files, there are variants you can use:
The xargs will cause all the filenames to be sent as arguments to the rm -f commands. This will save processes that are forked everytime -exec rm -f is run. But is fails with spaces etc in file names.
The -delete is the best when it is supported by the find you are using (because it avoids the overhead of executing the rm command by doing the unlink() call inside find() .
Empty directories
To find all empty directories, simply use:
This command will find all empty directories in the current directory with subdirectories and then print the full pathname for each empty directory to the screen.
- The ./ means start searching from the current directory. If you want to find files from another directory then replace the ./ with the path to needed directory. For example, to search everything under the system log directory you need to replace ./ with /var/log .
- The -type d flag is specifies to find only directories.
- The -empty flag is specifies to find empty directories.
To find and then delete all empty directories, use:
The -delete is the best when it is supported by the find you are using.
If this article has helped you then please leave a comment
Thanks for reading!
If this post helped you out and you’d like to show your support, please consider fueling future posts by buying me a coffee cup!
Источник
Поиск больших файлов Linux
Если на вашем жёстком диске закончилось свободное пространство, самый простой способ его освободить — найти и удалить самые большие и при этом ненужные файлы. Такая задача может возникнуть как на сервере, так и на домашнем компьютере, поэтому существуют удобные решения для обоих ситуаций. Способов поиска больших файлов существует очень много.
Как вы уже поняли, в этой небольшой инструкции мы рассмотрим, как найти большие файлы Linux с помощью графического интерфейса или консольных утилит. Будем двигаться от самого простого к более сложному.
Поиск больших файлов Linux
1. GDMap
Несмотря на то, что графических утилит есть около десятка, все они мне не очень нравятся. Например в Gnome можно использовать GDMap, а в KDE — fileslight. Обе утилиты сканируют файловую систему и выводят все файлы в виде диаграммы. Размер блока зависит от размера файла. Чем больше файл или папка, тем больше блок. Для установки GDMap в Ubuntu выполните:
sudo apt install gdmap
Затем запустите утилиту из главного меню. По умолчанию она отображает домашнюю папку. Здесь можно оценить, какие файлы самые увесистые.
2. Утилита ncdu
Это псевдографическая утилита, которая работает в терминале Linux. Она отображает список файлов и директорий по объёму и, что самое интересное, тут же позволяет удалять ненужные файлы. Для установки утилиты выполните:
sudo apt install ncdu
Затем запустите утилиту, передав ей в качестве параметра папку, которую надо просканировать. Можно проверить ту же домашнюю папку:
У утилиты очень простое управление. Для перемещения по списку используйте кнопки со стрелками вверх и вниз, для открытия папки — клавишу 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.
Источник
Remove files of 0 bytes in size via command line?
So, I got a directory filled with other directories, and I was wondering if it was possible to remove files that have no size. Typically these files are 0 bytes and since I want to merge all these subdirs I could replace a perfectly legit file with a weightless 0 byte file, and there goes my legit file. Any way to remove the zero byte files?
4 Answers 4
Use the Find command to find files by size and print file names to standard output.
substitute -print with -delete to delete the files rather than print them on screen.
Find and remove all files with a size of 0 recursively:
You can also do it directly in the shell. This could be useful if you don’t want to delete empty hidden files (those whose name begins with a . ). While you could do that with find as well, an alternative would be to use the shell itself:
Explanation
- shopt -s globstar : turns on the globstar option for bash which makes ** match one or more subdirectories. **/* will match all files and directories in the current directory and all its subdirectories.
- for file in **/*; do . ; done : iterate over all files and directories found;
- [ ! -s «$file» ] : [ -s «$file» ] is true if the file exists and is not empty. Therefore, [ ! -s «$file» ] (the ! inverses the test) is true if the file doesn’t exist or if it is empty.
- [ -f «$file» ] : true if the file is a regular file. Not a directory or a device file or a symlink etc.
- rm «$file» : delete the file.
The && ensure that the next command is only run if the previous one was successful so this will only delete empty, regular files.
Источник
Linux delete file with size 0 [duplicate]
The community reviewed whether to reopen this question 27 days ago and left it closed:
Not suitable for this site Update the question so it’s on-topic for Stack Overflow.
How do I delete a certain file in linux if its size is 0. I want to execute this in an crontab without any extra script.
Something like this?
8 Answers 8
This will delete all the files in a directory (and below) that are size zero.
If you just want a particular file;
To search and delete empty files in the current directory and subdirectories:
-type f is necessary because also directories are marked to be of size zero.
The dot . (current directory) is the starting search directory. If you have GNU find (e.g. not Mac OS), you can omit it in this case:
If no files to search are specified, the current directory (.) is used.
You can use the command find to do this. We can match files with -type f , and match empty files using -size 0 . Then we can delete the matches with -delete .
On Linux, the stat(1) command is useful when you don’t need find(1):
The stat command here allows us just to get the file size, that’s the -c %s (see the man pages for other formats). I am running the stat program and capturing its output, that’s the $( ) . This output is seen numerically, that’s the outer (( )) . If zero is given for the size, that is FALSE, so the second part of the OR is executed. Non-zero (non-empty file) will be TRUE, so the rm will not be executed.
Источник
Efficiently delete large directory containing thousands of files
We have an issue with a folder becoming unwieldy with hundreds of thousands of tiny files.
There are so many files that performing rm -rf returns an error and instead what we need to do is something like:
find /path/to/folder -name «filenamestart*» -type f -exec rm -f <> \;
This works but is very slow and constantly fails from running out of memory.
Is there a better way to do this? Ideally I would like to remove the entire directory without caring about the contents inside it.
19 Answers 19
Using rsync is surprising fast and simple.
@sarath’s answer mentioned another fast choice: Perl! Its benchmarks are faster than rsync -a —delete .
or, without the stat (it’s debatable whether it is needed; some say that may be faster with it, and others say it’s faster without it):
Someone on Twitter suggested using -delete instead of -exec rm -f<> \;
This has improved the efficiency of the command, it still uses recursion to go through everything though.
What about something like: find /path/to/folder -name «filenamestart*» -type f -print0 | xargs -0rn 20 rm -f
You can limit number of files to delete at once by changing the argument for parameter -n . The file names with blanks are included also.
Expanding on one of the comments, I do not think you’re doing what you think you’re doing.
First I created a huge amount of files, to simulate your situation:
Then I tried what I expected to fail, and what it sounds like you’re doing in the question:
But this does work:
I had the opportunity to test -delete as compared to -exec rm \ <\>\; and for me -delete was the answer to this problem.
Using -delete deleted the files in a folder of 400,000 files at least 1,000 times faster than rm .
The ‘How to delete large number of files in linux’ article suggests it is about three time faster, but in my test the difference was much more dramatic.
Use rm -rf directory instead of rm -rf * .
We were initially doing rm -rf * while in the directory to clear the contents and thought that was as fast as it could get. But then one of our senior engineers suggested we avoid using the asterisks ( * ) and instead pass in the parent directory, like rm -rf directory .
After some heavy debate about how that wouldn’t make a difference, we decided to benchmark it, along with a third method of using find . Here are the results:
rm -rf directory is about 9 TIMES FASTER than rm -rf * !
Needless to say, we bought that engineer a beer!
So now we use rm -rf directory; mkdir directory to delete the directory and re-create it.
About the -delete option above: I’m using it to remove a large number (1M+ est) files in a temp folder that I created and inadvertently forgot to cleanup nightly. I filled my disk/partition accidentally, and nothing else could remove them but the find . command. It is slow, at first I was using:
But that was taking an EXTREME amount of time. It started after about 15 mins to remove some of the files, but my guess is that it was removing less than 10 or so per second after it finally started. So, I tried the:
instead, and I’m letting it run right now. It appears to be running faster, though it’s EXTREMELY taxing on the CPU which the other command was not. It’s been running for like an hour now and I think I’m getting space back on my drive and the partition gradually «slimming down» but it’s still taking a very long time. I seriously doubt it’s running 1,000 times faster than the other. As in all things, I just wanted to point out the tradeoff in space vs. time. If you have the CPU bandwidth to spare (we do) then run the latter. It’s got my CPU running ( uptime reports):
And I’ve seen the load average go over 30.00 which is not good for a busy system, but for ours which is normally lightly loaded, it’s OK for a couple hours. I’ve checked most other things on the system and they’re still responsive so we are OK for now.
Источник