Quickly delete a large number of files in a directory under Linux system

Time:2022-6-20

Whatever it isoperationsystem, it is a terrible thing that there are too many files in the same level directory, no matter when reading or deleting.

 

What if this unfortunate thing happens to you and you can’t completely delete the entire directory?

 

You can use RM -f * Log, but you will soon receive an error message: the parameter list is too long

 

An hour ago, I was still narrowing the scope of universal matching characters, for example, I executed them in turn

 

rm -f a*.log

rm -f b*.log

……

 

It’s very laborious. If this doesn’t work, you can continue to refine wildcards. It may take a whole day to delete all log files.

 

That’s a lot of nonsense. In fact, it can be done with just one command

 

ls *.log | xargs rm -f

 

Just killed a directory containing tens of thousands of files (/var/log/samba). Try it.

 

Note: RM -fr (path + file to be deleted) can also be used.