2

I have a folder which contains many files. I want to delete files older than 30 days, so I use the command:

find cache/ -mtime +30 -exec rm {} \;

But my SSH session disconnects before the command completes.

How can I limit the number of files that are deleted at one time? For example:

find cache/ -mtime +30 -LIMIT 10000 -exec rm {} \;
2
  • What do you mean by "timeout"? Jan 15, 2012 at 4:15
  • wait a longtime and putty show disconnect
    – dinh
    Jan 15, 2012 at 4:29

4 Answers 4

5

Run it in a screen session. That way when putty disconnects the job is still running on the server. Just reconnect and use screen -r to re-attach the screen session to see the progress.

2
  • how to run in screen session?
    – dinh
    Jan 15, 2012 at 9:22
  • just run the screen command and it will create a new screen session for you. It's probably best to skim through the man page for screen, but I guarantee you it will quickly become one of your most used tools. Good luck!
    – d34dh0r53
    Jan 15, 2012 at 10:45
4

You could also run the command with nohup, like this:

nohup find cache/ -mtime +30 -exec rm {} \; &

Then cat nohup.out to keep an eye on the ouput.

3

You could do something like:

find cache/ -mtime +30 | head -n 10000 | xargs rm

This would work if the filenames were fairly "vanilla" (no spaces, special characters, etc) and short.

But if you'd rather just wait for your original command to complete, you can just tell PuTTY to send SSH keepalive messages every so often; see the PuTTY docs for the details. I usually use a value between 45–60 seconds, myself.

2
find cache/ -mtime +30 -print0 | xargs -0 rm -v

Then it'll output every file that is deleted, that should prevent your session from timing out .

Or, put this in cron, as it's something that sounds like it should be automated.

You must log in to answer this question.

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