1

A directory has files with names ending 001 to 100. How can you delete 2,4,6,8...100. And can that be written as a script. As soon as the directory becomes week old, this has to run. And after a month again. The goal is to delete half of the not-recent data.

3

you can use a bash for loop with a step (if its bash v4)

so it would be

#!/usr/bin/bash

cd /path/to/directory/

for i in `ls *{0..100..2}`
do 
    rm $i
done

this can be put in your crontab to run every week/month

00 11 1 * * /path/to/script.sh will run it on the first day of the month at 11am

00 11 * * 1 /path/to/script.sh will run it every monday

  • I like the idea. What if I want to sort the files according to name. And for the first week, and then after a month, and then at quarter, and then a year, : delete every other file in the sorted list. – Anvesh Kurapati Sep 10 '14 at 12:05
  • Thats good. What if the names are not fixed, but are sorted alphabetically, and the directory has to go through this loop after a week && a month && quarter && year. So if I have 1024 files at first, after a week I'll have 512, then 256, then 128, and after a year 64. So that the recent data stays in high resolution, while old data is not totally removed. – Anvesh Kurapati Sep 10 '14 at 12:08
  • to be honest that would probably be better being asked as a separate question – Fegnoid Sep 10 '14 at 13:26
1

Something like this should work (can't test right now so test on duplicate data first) -

 find /path/to/directory -type f -name '*[02468].jpg' -delete

or with rm -

 find /path/to/directory -type f -name '*[02468].jpg' -exec rm {} +

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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