A Stefan Klopp Weblog
Random header image... Refresh for more!

Using xargs

xargs can be a very handy tool when you need to use a command on a large number of arguments and you are getting errors such as:

Argument list too long

Using xargs is quite simple, you just need to pipe a command that gets your argument list into the xargs command. For example an easy way to delete all the contents of a directory if there are too many arguments is to do the following:

ls | xargs rm

or if you want to tar all the files in a directory and the list is too large you could do the following:

ls |xargs tar cvf big_file.tar.gz

xargs could also be very useful with the find command. Lets say for example you wanted to tar up all the mp3′s on your computer, you could do it with the following:

find / -name *.mp3 -type f -print | xargs tar -cvzf mp3s.tar.gz

For more on the use of xargs check out:

6 comments

1 Stefan { 01.10.05 at 11:51 am }

In the following example I am listing all files, then using the first xargs statement I am filtering them by search string then in the second xargs statement I am copying them to a new location. The reason for the double xargs is to get around the problem of: Argument list too long.

ls -1|xargs grep -l “search string”|xargs -n1 -I{} cp “{}” /home/newlocation

2 Stefan { 03.08.05 at 4:00 pm }

Here is the latest trick I did with xargs:

locate .htaccess |xargs cat |more

What it does is cat every .htaccess file one screen at a time. I needed to do this because I was in search of a rewrite rule that I couldn’t find.

3 ZZTech { 04.21.07 at 10:39 am }

Be a bit careful of the -c option on tar with xargs – it will restart the tar archive if the argument list (the ls command) is long enough (and you won’t get everything in the tar file that you expect). You can easily avoid this by using the ‘r’ (append) option with tar, though. (You can see http://zztools.blogspot.com for an example).

4 Brendon { 09.10.08 at 11:11 am }

using xargs + tar is a bad idea. Use tar -T, pax, etc instead. You’ll get into trouble with xargs.

5 T.J. Crowder { 11.11.10 at 6:12 am }

You’ll run into trouble with find / -name *.mp3 -type f -print | xargs tar -cvzf mp3s.tar.gz if your filenames have any spaces in them. Try changing -print to -print0 and adding -0 to the xargs command.

6 Soumya Mandi { 10.16.12 at 3:01 am }

just used this. perfect :D

Leave a Comment