Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

How to find and delete older files (older than 7 days) in a folder using a bash script? It's a folder in a server which contain automatic backups. Need to delete old ones automatically so there's space for new ones.

share|improve this question
up vote 0 down vote accepted
#!/bin/bash
ssh user@machine "find /path/to/files/ -mtime +7 -exec rm {} \;"

name the script as some_file_name.sh and give execute permissions.

 chmod +x file_name.sh

Run it as ./file_name.sh or bash file_name.sh

share|improve this answer
    
Perfect... thanks :) – THpubs Nov 14 '12 at 9:12

Using the below command, first confirm the file list:

find . -mtime +7

If the list is correct, using below the files can be deleted.

find . -mtime +7 -exec rm '{}' \;
share|improve this answer

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.