How to delete file: Argument list too long in Linux
Introduction
Deleting files in Linux is a common task, but sometimes you may encounter an error message stating “argument list too long”. This error occurs when you try to delete a large number of files at once. In this blog post, we will discuss what causes this error and how to overcome it.
Causes of the Error
The “argument list too long” error occurs because there is a limit to the number of arguments that can be passed to a command in Linux. This limit is determined by the value of the ARG_MAX variable. When you try to delete a large number of files at once, the list of arguments passed to the rm
command exceeds the value of ARG_MAX, and the error is raised.
use find
find . -name filename | xargs rm -f
## or
find . -name filename -delete
find . -name "*.txt" -exec rm {} \;
too many files may also cause the argument list too long problem in find
. Could use ls
and grep
instead.
ls |grep YOUR_FILE_PATTERN |xargs rm -f
with xargs
ls | xargs -n 50 rm -rf
xargs
could control the batch size of an output. In this case, 50 elements in a batch/group, and pass to rm to process progressively.