Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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 {} \;
share|improve this question
    
What do you mean by "timeout"? –  David Schwartz Jan 15 '12 at 4:15
    
wait a longtime and putty show disconnect –  dinh Jan 15 '12 at 4:29
add comment

4 Answers

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.

share|improve this answer
    
how to run in screen session? –  dinh Jan 15 '12 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 '12 at 10:45
add comment

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.

share|improve this answer
add comment

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.

share|improve this answer
add comment
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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.