10

I have directory with files from 2010 year.. I want to delete all files older than 500 days and I tried this:

find /var/log/arc/* -type f -mtime +500 -delete {}\;      

But I get this:

-bash: /usr/bin/find: Argument list too long

As I know this means that there are too many files and find can't handle them. But even if I put +2000 which is 3+ years I still getting this.

What I'm missing here?

S.I.
  • 435

2 Answers2

18

You're missing that find doesn't need a list of files as input. The problem is that the glob /var/log/arc/* expands to too many files. However, find will recurse into subdirectories by default, so there's no need to use the glob at all:

find /var/log/arc/ -type f -mtime +500 -delete

-delete is a non-standard predicate. If your find implementation doesn't support it, you can use:

find /var/log/arc/ -type f -mtime +500 -exec rm -f {} +

instead.

terdon
  • 242,166
  • Thank's but now got this: find: invalid predicate-delete'` – S.I. Jul 29 '16 at 11:42
  • 1
    @Garg first, sorry, I just copied your command and hadn't noticed your syntax is wrong. The -delete doesn't take {}. See update. However, your error message seems to suggest that your version of find doesn't support -delete. If you're not using GNU find (which in most cases you won't unless you're using Linux), you need to use -exec -rm {} \; instead. – terdon Jul 29 '16 at 11:47
  • Thank you. -exec rm {} \; do the trick. – S.I. Jul 29 '16 at 11:52
  • 1
    @hobbs I don't see why not, -exec is defined by POSIX. – terdon Jul 29 '16 at 18:35
  • @terdon my mistake. – hobbs Aug 03 '16 at 22:05
0

Disclaimer: I'm the current aurhot of rawhide (rh) (see https://github.com/raforg/rawhide)

What you are missing is that find only needs the directory (/var/log/arc), not all the files in that directory (/var/log/arc/*). Removing the /* and {}\; (which shouldn't be there) would fix it for GNU find. For non-GNU find, you also need to replace -delete {}\; with -exec /bin/rm '{}' \;.

FWIW, An alternative to find is my rawhide (rh) program. With it, you can do:

rh -UUU /var/log/arc 'f && old(500*days)'

It's much the same, but shorter and more readable.

-UUU unlinks/deletes/removes matches.

f (or file) matches regular files.

old(500*days) matches files modified at least 500 days ago.

raf
  • 171