tags:

views:

457

answers:

6

Hi ,

I tried to remove around 7,000 log files under a directory by using

rm *.log

but failed with error message "too long argument list" . Is there any way to slove it ?

thanks .

+19  A: 

Try

find . -name '*.log' -delete

Note that this will also delete files in subdirectories. To avoid that, add -maxdepth 1 before the -delete

itsadok
+1 for delete option.
Ikke
`-maxdepth 0` if you want to prevent it from deleting logfiles in subdirectories.
Chuck
Clever way to do it, +1 !
Clement Herreman
@Chuck I thought so too at first, but then I tried it
itsadok
+5  A: 

One way:

find . -name "*.log" -exec rm {} \;
DisplacedAussie
If you don't put the `*.log` in quotes, you're going to have the same problem
itsadok
*.log must be in single quotes, not double quotes, or you still have the same problem.
William Pursell
+3  A: 
find <your-dir> -name '*.log' | xargs rm

is one way

knittl
A: 

I think the reason why this problem is with rm/ls and not with find, is because:

rm *.log appends all the files matching "*.log" to rm as argument so the system complains long argument list. So, i think simply:

ls | grep ".*\.log" | xargs rm 

also does the job.
In case of find, the directory is searched first and files are matched with the pattern and those satisfying it are deleted.

Neeraj
-1: The "problem" is not the fault of `rm` or `ls`. The expansion of `*.ls` is done by the shell BEFORE `rm` is executed. It is the shell that is rejecting the resulting overly long command line ...
Stephen C
A: 

ls *.log | xargs -n 1000 rm

Graham
This has exactly the same problem...the shell glob fails and the shell cannot execute the command.
William Pursell
A: 

Addon : Using xargs, if there's space within the file name reference the below,

find ./ -iname 'someglob*' -print0 | xargs -0 rm -rf
PsyberMonkey