Personal tools
You are here: Home News Items Tips and Tricks Linux tip: Too many arguments
Navigation
Log in


Forgot your password?
New user?
 
Document Actions

Linux tip: Too many arguments

by Herbert Wolverson last modified 2008-09-22 13:55

Sometimes you need to delete a lot of files, and "rm *" fails with the error "too many arguments". This tip will help!

When you use a wildcard in Linux (or FreeBSD/etc.), the * is turned into arguments for the command. For example, rm * on a folder containing 10 items would be translated into:

rm item1 item2 item3 item4 item5 item6 item7 item8 item9 item10

Normally, this is not a problem - and it's a very efficient way of doing things. Unfortunately, if you exceed the number of arguments that the command can handle - you will receive the error "too many arguments" and the command won't run at all! If, for example, you are cleaning up a mail spool directory, this can be quite maddening - using rm "aa*", "rm ab*" etc. each time to reduce the number of files matching each wildcard becomes tedious, fast!

Fortunately, *NIX provides a solution in the form of the xargs command. xargs takes a piped list of items, and executes a command with each item as an argument. The find command provides a convenient way to list relevant files. In this case, the following command will delete any number of files:

find . | xargs rm

WARNING: This is a dangerous command! Be really sure you are in the correct directory when you execute it.

As a safety net, you might want to filter the list of files a bit. Fortunately, you can add a grep condition:

find . | grep "myfilename" | xargs rm

This is a good example of the power of *NIX piping, and the philosophy of combining small tools to provide a powerful solution.


Powered by Plone CMS, the Open Source Content Management System

This site conforms to the following standards: