Deleting files older than X days

A couple of different ways to purge files older than a certain amount of days.

find -type f -mtime +350 -name 'CopyCluster*' -execdir rm -- '{}' \;

If you want to be very precise with the number of days ago you want to delete from then you can find the number of days from now using:

days=$(( ( $(date '+%s') - $(date -d '1 months ago' '+%s') ) / 86400 ))
echo $days

and then use the variable to delete

find /logs/CopyCluster* -mtime +$days -type f -delete

To do this all in one command

find /tmp/*.log -type f \
-mtime "+$(( ( $(date '+%s') - $(date -d '1 months ago' '+%s') ) / 86400 ))" -delete

 

 

Advertisements

Mongo – Querying documents within documents

Some of our collections in Mongo are built with embedded documents, This means the actual raw data is held in the following heirarchy:

OurMongoDB (DB)
OurMongoCollection (Collection)
                                _id          (document)
                                Car         (document)
                                                _id                       (embedded document)
                                                Channel             (embedded document)
                                                Make                   (embedded document)
                                                Model                  (embedded document)
                                                …
                                userId   (document)

This means that normal mongo db find statements will not work if you are searching based on criteria…

To search for embedded document data – use dot notation (http://www.mongodb.org/display/DOCS/Dot+Notation+%28Reaching+into+Objects%29

--return only where channel is Test1
db.ourMongoCollection.find({"car.channel" : "Test1"});

--return only where channel is Test2
db.ourMongoCollection.find({"car.channel" : "Test2"});

--return count of all documents with channel as Test2
db.ourMongoCollection.find({"car.channel" : "Test2"}).count();

--return count of all channel Test1 documents with a make of Honda 
--(search criteria is case sensitive)
db.ourMongoCollection.find({"car.channel" : "Test1", "car.make" : "HONDA"}).count();