How many files can rm delete at once?

8,563

The maximum length of the command line is set by the system and is sometimes 128KiB.

If you need to remove many, many files, you need to call rm more than once, using xargs:

find /var/log -type f -print0 | xargs -0 rm --

(Careful, this will find and delete all files in the subdirectories of /var/log etc. - if you do not want that use find /var/log/ -type f -maxdepth 1). The find lists the files, 0-delimited (not newline), and xargs -0 will accept exactly this input (to handle filenames with spaces etc.), then call rm -- for these files.

Use rm -f -- (with caution) if you are asked whether files should be removed, and you are sure you want to remove them.

Share:
8,563

Related videos on Youtube

VinoPravin
Author by

VinoPravin

I'm a Debian user who wants to know all about the linux world. If there's a problem with some software or hardware under this operating system, I can fix it, of course I need some time to do that. I don't know many things, but sooner or later I always develop an OpenSource solution and make things work whether they like it or not.

Updated on September 18, 2022

Comments

  • VinoPravin
    VinoPravin almost 2 years

    One of my friends wanted to have more logs in the /var/log/ directory, and after some time of using the system, he tried to access the folder and list it, but instead he got the following error:

    bash: /bin/rm: Argument list too long
    

    Does anyone know how many files can be added to this rm list?

    • Ned64
      Ned64 about 9 years
      Part of the question is probably a duplicate, but the answer also contains a solution to the task at hand, while the other question does not.
    • VinoPravin
      VinoPravin about 9 years
      Yes it solves partially. But I think it's enough for me.
  • VinoPravin
    VinoPravin about 9 years
    Ok, so it's for all commands, not just the one.
  • Ned64
    Ned64 about 9 years
    The limit is one for the current shell, for the current command line. If you open many shells each will have this limit. xargs will call rm as many times as necessary, distributing the many file names that are fed to it by the find command such that the limit is not exceeded (each of the rm commands will be limited by the commandline length limit).