how to delete a lot of files in linux

0
(0)

Mountains of files that periodically need to be deleted can accumulate on servers. For example, logs, compiled versions of files, or any other file cache generated by scripts.
Sooner or later, these mountains have to be cleaned:

$ rm /tmp/logs/*.log

If the number of files is critically large, at some point, instead of deleting files, we will see such a message in the console:

/bin/rm: Argument list too long.

What does this mean?

Problem
The fact is that the use of a mask in commands like rm / cp / find Linux translates into a format convenient for itself, making the command understandable to humans:

$ rm /tmp/logs/*.log

list of files under this mask:

$ rm /tmp/logs/1.log /tmp/logs/2.log /tmp/logs/3.log ...

Problems begin when the arguments of the rm command become greater than the allowable limit. You can check this limit using the getconf command:

$ getconf ARG_MAX
262144

And what do you do anyway?

Use For loop
The easiest way is to execute the command we need in a for loop, which has two important advantages. Firstly, cycles are resource-intensive and do not have limits on the number of arguments. Secondly, it’s easy to wrap additional logic in a loop if you need to do something more complicated than deleting files.
For example, like this, you can delete all files with one command:

$ for f in /tmp/logs/*.log; do rm "$f"; done

Or delete files that are older than seven days:

for f in /tmp/logs/*.log
do
  find $f -mtime +7 -exec rm {} ;
done

Or count, write to a variable and print their number:

FILES_COUNT=`c=0; for f in /tmp/logs/*.log ; do ((c++)); done ; echo $c`
echo "$FILES_COUNT log files left";

The most important thing
* do not forget to clean the logs regularly so as not to clog the file system
* use off-the-shelf tools to avoid inventing your bikes
* re-read the commands before execution, so as not to accidentally delete everything

Similar Posts:

1,539

How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.

Scroll to Top