2

As mentioned in title, i'm trying to delete files that are older than one day inside a directory with many many files (without actually deleting the said directory) - the directory is backed up by a remote rsnapshot server so i don't need the source files anymore as soon as they're backed up, the problem is that

find /directory/* -mtime +1 -delete

gives an error 'argument list too long' since there are that many files (they're not even that big, it's just a lot of pictures and small videos from a security camera working with motion software - which is great by the way ;)

I've stumbled over this command that i could use with crontab:

perl -e 'for(<~/test1/*>){((stat)[9]<(unlink))}'

but i don't really understand this command, as i understood it's some kind of loop. It did actually work on a test directory i created (test1 obviously) with a bunch of test files that were all younger than one day, i don't think that the 'older than' argument is even in here..

How would i go about that? Feel free to modify my question if it's not clear.

2 Answers 2

3

find is recursive. You don't need to do find /directory/*, just use:

find /directory/ -mtime +1 -delete
8
  • @steeldriver I considered that.. then I realised that deleting something immediately inside the directory will change the mtime, and since -delete is depth-first, the directory itself will be last thing examined - so either it had some files deleted, and mtime ~now, or all files from <1day and mtime from the last file created (so <1day). Either way, the directory is safe.
    – muru
    May 17, 2017 at 17:37
  • Ahh... cunning ;) May 17, 2017 at 17:40
  • Thanks for the quick answer, well it doesn't really work - tried it even with sudo - and no files are deleted at all (when searching the directory recursively, so without * at the end)..?? It seems to accept the command, but nothing really happens.. May 19, 2017 at 12:18
  • 1
    Ok, tried with -mmin +1440, which only deletes the directory itself, if there is not one single file that stands against the time argument; so if all the files are older than one day, the directory will be deleted as well (cause it's obviously older than one day), otherwise it works exactly how i wanted it to work, and since there will always be files newer than one day, i must not be afraid of the directory being deleted.. Eventually you could highlight that -mtime +1 is actually 2x days (as mentioned in the man find) which makes one use -mmin over -mtime.. Highly appreciated ;) May 19, 2017 at 14:10
  • 1
    @deltagreen88 huh, in that case you can protect the directory: find /directory -mindepth 1 ...
    – muru
    May 19, 2017 at 14:15
0

If find /directory/* -mtime +1 -delete does produce the right files but just too long a list, what about:

find /directory/* -mtime +1 | while read fName; do rm $fName;done

Or write the output to a file first and then process the file. Many roads lead to Rome... :)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .