Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have many files one of my scripts generate daily. The file name structure resembles:

capacity_data_2012-12-02.txt , capacity_data_2012-12-03.txt and so on...

I want to create a script which would check for the last month of data. For example today is capacity_data_2012-12-28.txt. I'd create a list of the files for the last 30 days. Any files more than 30 days old (not in my list) would then be moved to another directory.

Any direction would be helpful.

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

If you want to move files older than 30 days, or more precisely, files which as not been written in the last 30 days:

find . -maxdepth 1 -type f -name "capacity_data_???-??-??.txt" -mtime +30 -exec echo mv {} another_directory/ \;

(Remove the echo when you are happy with the output.)

If you want to list the files of the last 30 days:

awk 'BEGIN{now_n = systime() - 30 * 24 * 60 * 60;
           for(n = 30; n >= 0; n--) {
             print "capacity_data_" strftime("%Y-%m-%d", now_n) ".txt"
             now_n += 24* 60 * 60;
           }}'

And if you want to loop on those files:

files=$(awk 'BEGIN{now_n = systime() - 30 * 24 * 60 * 60;
                   for(n = 30; n >= 0; n--) {
                     print "capacity_data_" strftime("%Y-%m-%d", now_n) ".txt"
                     now_n += 24* 60 * 60;
                    }}')
for file in $files; do
  echo $file
done
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.