How to quickly delete all files with specific names

Every computer or tablet user deletes from time to time files large sizes . Typically these are video files, audio files, pictures, archives, including disk images, program installers, etc.

Large files gradually accumulate on a hard drive or flash drive, thereby reducing their volume.

By the way, people are increasingly buying computers from online stores. In Ukraine, the leader in this segment is Rozetka; you can buy a computer in Minsk on the Buy.Here website.

Computers make people lazy to some extent. If earlier people went to the cinema much more often, now most people download the desired film to their computer or watch it online. Even if you bought a computer with a terabyte hard drive for video, music, photos or games - this does not mean that this amount of memory will be enough for your computer “until death”.

The hour will come when you want to save a file, but there is no space for it. It is at such moments that most users begin to turn over the entire heap of files and folders in order to find old and rarely used large files.

Deleting large files using WinDirStat

The entire above process can be accelerated and automated using a small utility called WinDirStat. You can download this program from the link http://windirstat.info/download.html

Using the WinDirStat utility

Install WinDirStat. Launch it. The utility will scan the disk you selected and give you a list of folders (files) and under them fragmentable disk structure. Each square is a file.

Click on the large squares and the program will show you large files. See which ones can be removed painlessly. You can delete files directly in WinDirStat.

Get acquainted with the capabilities of this program. It is very easy to use. Even a schoolboy can figure it out.

An enchanting dotting of i’s in the matter of deleting files from an overcrowded directory.

I read the article and was very surprised. Is it really not in the standard Linux toolkit? simple means to work with full directories, you need to resort to such low-level methods as calling getdents() directly.

For those who are not aware of the problem, brief description: if you accidentally created in the same directory huge amount files without hierarchy - i.e. from 5 million files lying in one single flat directory, it will not be possible to quickly delete them. In addition, not all utilities in Linux can do this in principle - they will either heavily load the processor/HDD or take up a lot of memory.

So I set aside some time, set up a test site, and gave it a try. various means, both suggested in the comments, and found in various articles and your own.

Preparation

Since you don’t want to create an overflowing directory on your HDD of your work computer and then go through the hassle of deleting it, let’s create a virtual file system in separate file and mount it via a loop device. Fortunately, this is easy on Linux.

Create an empty file of 200GB in size
#!python f = open("sparse", "w") f.seek(1024 * 1024 * 1024 * 200) f.write("\0")

Many people advise using the dd utility for this, for example dd if=/dev/zero of=disk-image bs=1M count=1M , but this works much slower, and the result, as I understand it, is the same.

Format the file in ext4 and mount it as a file system
mkfs -t ext4 -q sparse # TODO: less FS size, but change -N option sudo mount sparse /mnt mkdir /mnt/test_dir
Unfortunately, I learned about the -N option of the mkfs.ext4 command after experimenting. It allows you to increase the limit on the number of inodes on FS without increasing the size of the image file. But, on the other hand, standard settings- closer to real conditions.

Create a set empty files(will work for several hours)
#!python for i in xrange(0, 13107300): f = open("/mnt/test_dir/(0)_(0)_(0)_(0)".format(i), "w") f .close() if i % 10000 == 0: print i
By the way, if at the beginning files were created quite quickly, then subsequent ones were added more and more slowly, random pauses appeared, and the kernel’s memory usage increased. So storage large number files in a flat directory is in itself a bad idea.

We check that all inodes on the FS are exhausted.
$ df -i /dev/loop0 13107200 13107200 38517 100% /mnt
Directory file size ~360MB
$ ls -lh /mnt/ drwxrwxr-x 2 seriy seriy 358M Nov. 1 03:11 test_dir
Now let's try to delete this directory with all its contents in various ways.

Tests

After each test we reset the cache file system
sudo sh -c "sync && echo 1 > /proc/sys/vm/drop_caches"
in order not to quickly take up all the memory and compare the removal speed under the same conditions.

Removing via rm -r

$ rm -r /mnt/test_dir/
Under strace, it calls getdents() several times in a row (!!!), then calls unlinkat() a lot, and so on in a loop. Took 30MB RAM is not growing.
Deletes content successfully.
iotop 7664 be/4 seriy 72.70 M/s 0.00 B/s 0.00 % 93.15 % rm -r /mnt/test_dir/ 5919 be/0 root 80.77 M/s 16.48 M/s 0.00 % 80.68 %
Those. It is quite normal to delete overfull directories using rm -r /path/to/directory.

Removal via rm ./*

$ rm /mnt/test_dir/*
Launches a child shell process that has grown to 600MB
Obviously, the asterisked glob is processed by the shell itself, accumulated in memory and passed to the rm command after the entire directory has been read.

Removal via find -exec

$ find /mnt/test_dir/ -type f -exec rm -v () \;
Under strace it only calls getdents() . the find process has grown to 600MB, nailed by ^C. Didn't delete anything.
find acts the same as * in the shell - it first builds full list in memory.

Deleting via find -delete

$ find /mnt/test_dir/ -type f -delete
Grew up to 600MB, nailed by ^C. Didn't delete anything.
Same as the previous command. And this is extremely surprising! I pinned my hopes on this team from the very beginning.

Removing via ls -f and xargs

$ cd /mnt/test_dir/ ; ls -f . | xargs -n 100 rm
the -f option tells you not to sort the list of files.
Creates the following process hierarchy:
| - ls 212Kb | - xargs 108Kb | - rm 130Kb # rm's pid is constantly changing
Removes successfully.
iotop # jumps a lot 5919 be/0 root 5.87 M/s 6.28 M/s 0.00% 89.15%
In this situation, ls -f behaves more adequately than find and does not accumulate a list of files in memory unnecessarily. ls without parameters (like find) - reads the entire list of files into memory. Obviously for sorting purposes. But this method is bad because it constantly calls rm , which creates additional overhead.
This leads to another way - you can redirect the output of ls -f to a file and then delete the contents of the directory using this list.

Removing via perl readdir

$ perl -e "chdir "/mnt/test_dir/" or die; opendir D, "."; while ($n = readdir D) ( unlink $n )" (took)
380Kb memory does not grow.
Removes successfully.
iotop 7591 be/4 seriy 13.74 M/s 0.00 B/s 0.00% 98.95% perl -e chdi... 5919 be/0 root 11.18 M/s 1438.88 K/s 0.00% 93.85%
It turns out that using readdir is quite possible?

Removal via C program readdir + unlink

//file: cleandir.c #include #include #include int main(int argc, char *argv) ( struct dirent *entry; DIR *dp; chdir("/mnt/test_dir"); dp = opendir("."); while((entry = readdir(dp)) ! = NULL) ( if (strcmp(entry->d_name, ".") && strcmp(entry->d_name, ".."))( unlink(entry->d_name); // maybe unlinkat ? ) ) )
$ gcc -o cleandir cleandir.c
$./cleandir
Under strace, it calls getdents() once, then unlink() many times, and so on in a loop. Took 128Kb memory does not grow.
Removes successfully.
iotop: 7565 be/4 seriy 11.70 M/s 0.00 B/s 0.00% 98.88% ./cleandir 5919 be/0 root 12.97 M/s 1079.23 K/s 0.00% 92.42%
Again, we are convinced that using readdir is quite normal, if you do not accumulate the results in memory, but delete files immediately.

Conclusions

  • You can use a combination of the readdir() + unlink() functions to remove directories containing millions of files.
  • In practice, it is better to use rm -r /my/dir/ , because it does something more clever - it first builds a relatively small list of files in memory by calling readdir() several times, and then deletes files from that list. This allows you to alternate read and write loads more smoothly, which increases the deletion speed.
  • To reduce system load, use in combination with nice or ionice. Or use scripting languages ​​and insert small sleep() in loops. Either generate a list of files via ls -l and pass it through

In the post "" I told you how to get rid of unnecessary files loading the server.

We needed to remove created by WordPress preview. But deleting each file one by one or selecting it in groups is a long and tedious task. How to delete all files at once from certain names? An excellent file manager Total Commander.

Total Commander – paid program, but even after a month free use the authors, understanding that not everyone has money, are not against you continuing to use it. The only “inconvenience” is that you need to press one of three buttons when starting the program. So, we delete many files at once with certain names with using Total Commander.

We launch the program and find the desired folder on the server (we will tell you how to use Total Commander as an FTP manager in one of the future posts). To select a specific group of files that meet any condition, you need to select from the menu: Selection – Select group or use " hotkey"Num+.


Now you need to specify a specific file mask. We create our template - click on the button of the same name.

Since all unnecessary previews have the form “file_name–size_x_size”, we indicate the “x” sign as a difference between the files. Click the Save button to save the template.


We name our created template and click OK.


Now to select files (Num+), select the created template ("Preview"). Click OK.

That’s basically it – all files matching the specified template are selected. Now you need to carefully review them so as not to delete an accidental required file, containing the sign "x". To deselect it, click while holding down the Ctrl key.


All you have to do is click on Delete key, confirm the deletion and wait until all unnecessary files corresponding to the given conditions. At quick removal many files with specific names or specific extension(format) you can use several templates.

If you liked it here, subscribe to blog updates! For dessert, I suggest you watch the funny video “A simple solution to complex problems”

IN operating system Windows the same operation can be performed in different ways. Some people are used to managing computer resources using a mouse, others using a keyboard. To quickly delete a folder, you just need to understand which method is the easiest and most convenient for you.

Instructions

  • The bulk of files and folders are not deleted from the computer immediately, but are placed in the trash. If you need to completely delete a folder, any of the following actions should end with emptying the Recycle Bin.
  • To delete files from the Recycle Bin, move your mouse cursor to the “Recycle Bin” icon on the desktop, right-click on it and select “Empty Recycle Bin” from the drop-down menu. Confirm your actions in the request window. Alternative option: Open the Trash item and select from the panel typical tasks the same team.
  • The folder itself can be deleted in the following ways. Move your mouse cursor over unnecessary folder, right-click on its icon and select context menu"Delete" command. In the request window, confirm the operation by clicking on the “Yes” button. The folder will be placed in the trash.
  • Another option is more suitable if you need to delete several folders at once. While holding down left button mouse, select the folders you want to delete. Repeat the steps described in the previous step.
  • Another way to delete using the mouse: move the cursor to the folder icon, hold down the left mouse button, and drag the folder icon to the trash icon on the desktop. Confirm your actions in the request window.
  • If you are more used to working on the keyboard, select the folder you want to place in the Trash and press the Delete key. When the system asks you to confirm the operation, press Enter.
  • In the event that you cannot remember in which directory the folder is located, first use the “Search” component. Click on the "Start" button or Windows key, select “Search” from the menu. In the window that opens, set the search criteria and click the “Find” button. When the folder you are looking for is found, delete it using any of the methods described above, directly from the search engine window.
  • Rate the article!

    Have you ever been faced with the task of deleting a large amount of information from your computer? Surely it has stood at least once, and if not, then it will definitely happen, believe me. And anyone who has encountered this can tell you for sure that these are real hemorrhoids.

    After all large volume- this is a lot of files located in many folders. These files, individually, different formats and sizes, some of them may be hidden, some limited access or even worse - password protected. And that's when you take it delete it all in the usual way , then all sorts of questions start popping up, like, “Are you sure you want to delete it?” or “This folder has a password, are you sure you want to delete it?” But it happens that you “hid and password-protected” all this a year ago and now you don’t remember anything, and you don’t even need to remember them, you just want to get rid of them. What if there are several dozen or even more such files?

    This situation is familiar to me, since I do a lot of things on my computer and it often gets cluttered. Either I wanted to become a kung fu master and downloaded 50 gigabytes of videos, then I decided to learn English, downloading 80 gigabytes of various courses, then I wanted to move objects with my hands like a Jedi, and downloaded some other rubbish for 90 gigabytes. All in all, delete large amounts of information I have to often, because I’m always looking for myself in something new :) (and I’m not going to stop). the usual way sometimes takes several hours.

    But, fortunately for me, I found a program with which you can delete a lot of information quickly. This program is called Fast Folder Eraser Free.

    First you need to download it, for example from here.

    After downloading, a window like this will pop up. Click "Run"


    After which it goes standard installation which shouldn't cause any difficulties.

    After the installation is completed and you click ready (or Finish), such a window will pop up in front of you. Click Accept.



    In it, we must select the folder that we want to delete. To select a folder, click on the ellipsis button circled in red in the picture above.

    After which a selection window will open in front of us.


    The "insides" of the folder are opened double click, but when you get to the one you want, click on it once, and then click on OK.

    That's it, we have selected the folder, now click Delete and the deletion process will begin.


    Wow, I wanted to take a photo of the process, but didn’t have time! Everything was removed in less than two seconds! There were 242 songs, and they weighed more than a gigabyte. Let me give you an example. In the usual way, i.e. Right click mouse -> Delete, all this would have been deleted for me in about 5 minutes! So judge for yourself.

    This is such an interesting program, and for some, like me, it’s also necessary.

    That's all. Bye.