How to overcome argument list too long error message in Linux ?

A Process supports limited length of arguments in command line.

Suppose we have too many arguments and we pass all this to a command, then we will get the error “Argument list too long”

1. To get the Maximum argument length supported in Linux, we can use getconf command.

[root@nglinux ~]# getconf ARG_MAX
2621440

It counts to more than 2MB of arguments, already quite large.

2. How to increase the argument length ?
We can not increase it immediately.
ARG_MAX parameter is configured in kernel configuration and ideally we dont increase this value.

3. If we can’t increase this, how to overcome this limitation ?
We can overcome this limitation by using xargs command and passing arguments to this command instead of one single command.

4. An example to overcome “Argument list too long error”
First lets create a directory and create few Lakh empty files.

[root@nglinux ~]# mkdir testing
[root@nglinux ~]# cd testing/
[root@nglinux testing]# for i in {1..100000}; do touch ${i}200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000; done &
[1] 18355
[root@nglinux testing]# for i in {1..100000}; do touch ${i}300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000; done &
[2] 18920
[root@nglinux testing]# for i in {1..100000}; do touch ${i}400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000; done &

Once the above command completed, check if all files created:

[root@nglinux lvm]# ls -ltr ~/testing/ | tail -4
-rw-r--r--. 1 root root    0 Jan  4 03:04 15887400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
-rw-r--r--. 1 root root    0 Jan  4 03:04 16566200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
-rw-r--r--. 1 root root    0 Jan  4 03:04 15959300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
-rw-r--r--. 1 root root    0 Jan  4 03:04 15888400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
[root@nglinux lvm]# 

Now lets try to copy or delete the files:

[root@nglinux testing]# mkdir hello
[root@nglinux testing]# cp -prf * hello/
-bash: /bin/cp: Argument list too long
[root@nglinux testing]# 

[root@nglinux testing]# rm -rf *
-bash: /bin/rm: Argument list too long
[root@nglinux testing]# 

Oh it seems we have argument list too long error appeared.

Overcome argument list too long error with xargs command

[root@nglinux testing]# find . -print | xargs rm -rf 
rm: cannot remove directory: `.'
[root@nglinux testing]# ls
[root@nglinux testing]# 

See finally we have deleted all files 🙂 with xargs.

The beauty of xargs command.

I hope you liked the article, please do post your comments.

Leave a Reply

Your email address will not be published.