Linux Commands : Argument list too long

Posted on Mar 26, 2009 in Linux |

There may come a time where you try to run a command on many files in a folder and you’ll get this annoying message similar to this:

-bash: //bin/chown: Argument list too long

The reason for this annoyance is that Linux kernel has a limitation of bytes it can process through as arguments in exec() commands. Try:

[root@x folder]# egrep ARG_MAX /usr/include/linux/limits.h
#define ARG_MAX 131072 /* # bytes of args + environ for exec() */

And you’ll see what I mean.

There are many ways to solve this problem. Most involve piping commands through other commands, or looping through files in a script. I’ve found the most effective solution for my purposes is piping ls or find trough xargs . Here are some examples:

find /some/folder/path -type f | xargs -i chown root:root ‘{}’ Changes owner and group of every file in /some/folder/path
find /some/folder/path -type f | xargs -i rm -f ‘{}’ Removes all files in /some/folder/path
ls -1 | xargs -i mv ‘{}’ /some/other/path/ Moves all files in the current folder to /some/other/path/

Beware that when using ls, you need to be in the current directory as you’re not given full paths to the file, so for example:

cd /some/folder/path ; ls -1 | xargs -i mv ‘{}’ /some/other/path/

Would do the trick for you if you wanted to run it off crontab or another folder.