Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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

2 Answers 2

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 :) –  EApubs 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.