cp master date
March 15, 2012 4:06 AM   Subscribe

I need help copying a master file to a file with a new name. Complication new name should be the date of the first of the month

I have an idea for a program for myself, but I was pulling out my hair trying to do this within python. So, on advice from a friend, I'm pulling that part out and making a cron job to do this on the first of the month using bash.

I can easily do this by hand. I don't want to. This is a tool to learn. I read the cp man page and I don't see an easy way to do it (though I'm also working through the mental fog of fibromyalgia here too).

I see there's a date command as well. Obviously this isn't going to be a bash one liner for me (though I'm sure 10 people will chime in how to do it).

Can I use `date` and write it to a variable and then use that variable to copy to a new file?

Please limit answers to bash and/or python. I know many of you like perl, but for me, perls are things you put on necklaces (as in I've never written a line of it).

I'm sorry if this seems disorganized and dumb, I'm quite ill at the moment, but I need something to get my mental muscles moving too. I wouldn't wish fibro on my worst enemy.
posted by kathrynm to Computers & Internet (4 answers total) 1 user marked this as a favorite
cp file file-`date "+%Y-%m-01"`
posted by scruss at 4:24 AM on March 15, 2012

scruss' solution is the only I thought of, but I have a question: does the date you need on the file need to be the first of the current month? Or the first day of the month the file was created? That is if file.txt was created on 1988-05-20 do you want file-1988-05-01.txt?
posted by Fortran at 5:29 AM on March 15, 2012

The first of the current month. The python part is going to be used to track my fibro symptoms for my doctor.

So in April is would be symptoms-2012-04-01.

Alas, I forgot about how to use backticks in bash.

Thanks, now if people want to play they can :-) I'd be interested to see how it's done in python.
posted by kathrynm at 5:58 AM on March 15, 2012

Pretty much the same idea:
from datetime import datetime
from shutil import copy

copy('file', 'file-{:%Y-%m-01}'.format(datetime.now()))
Note that if you're using Python 2.6 or earlier you'd have to write {0:...} for the format string, as the ability to leave off the number and rely instead on the order was added as a 2.7 feature.
posted by Rhomboid at 12:47 PM on March 15, 2012

« Older Classic dating blunder   |   Why do my knees hurt when cycling? And then... Newer »
This thread is closed to new comments.