[ncolug] Re: Bash One Liner for Backup File

  • From: Mike <bellyacres@xxxxxxxxx>
  • To: ncolug@xxxxxxxxxxxxx
  • Date: Wed, 10 Feb 2016 20:09:59 -0500

On 02/10/2016 06:06 PM, Chuck wrote:

On Wed, 2016-02-10 at 07:43 -0500, Mike wrote:
On 02/10/2016 12:38 AM, Rob Gibson wrote:
Chuck, There is a one-liner that I have seen you use to make a copy of a file you are modifying with a datestamp appended to the filename, and I have been trying to rack my brain for that one-liner. If I recall, it used 'cp' with a single argument? Thanks, Rob

Not Chuck, but this should work...

cp devmem2.c devmem2.c-$(date +'%Y-%m-%d')

Mike

To unsubscribe send toncolug-request@xxxxxxxxxxxxx 
<mailto:ncolug-request@xxxxxxxxxxxxx>  with 'unsubscribe' in the Subject field.


That's very close to what I used to use:
cp -p somefile{,.$(date +%Y%m%d.%H%M%S}

to copy somefile.txt to somefile.txt.CCYYMMDD.hhmmss

I didn't like the way that handled the extension, so at work I built a shell function that can accept a list of files followed by a directory and it will insert the .CCYYMMDD.hhmmss. part in front of the last extension for each and place the copy in the named folder, stripping off directories. I named the function cpdate-8.6 and if I call it like:

cpdate-8.6 * ./Archives/

it will make a copy of each file in the current directory, all with the same time-stamp, and store them in the Archives directory. Works nice. It uses cp -pPUI I think. The function became much larger than I anticipated once I started accepting any random path. I finally cheated and used a library from the Internets to convert all file names to fully-qualified file names and then I had to have some logic for proper handling of file anme extensions, in case the file didn't have one, or had multiple.

It's a nice exercise. I may try it on this laptop now without the library...

Any chance this is sharable?

Mike

Other related posts: