[ncolug] Re: Bash One Liner for Backup File

  • From: Chuck <cstickelman@xxxxxxxxxx>
  • To: ncolug@xxxxxxxxxxxxx
  • Date: Thu, 11 Feb 2016 16:32:13 -0500

On Thu, 2016-02-11 at 15:13 -0500, Chuck wrote:

On Wed, 2016-02-10 at 20:09 -0500, Mike wrote:
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 to 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
I'm rewriting it to be much cleaner.  I really want a way to convert
all files specifications into Fully-Qualified File Names and then
move on. 

I'll share when I'm done.  
I have the function mostly done. Though, it doesn't currently preserve
the files' extension, so I'm working on that as well...  Here's what I
have so far:
 cp-date86 () 

    declare -r datetime=$(date +%Y%m%d.%H%M%S);
    declare -a files;
    for file in $@;
    do
        dir=$(dirname $file);
        cd $dir && dir=$(pwd) && cd - &> /dev/null;
        file=$(basename $file);
        files[${#files[*]}]=$dir/$file;
    done;
    [[ -d ${files[-1]} ]] && dest_dir=${files[-1]} && unset files[-1];
    for file in ${files[@]};
    do
        if [[ -f $file ]]; then
            [[ ! -v dest_dir ]] && dest_dir=$(dirname $file);
            ext=${file##*.};
            echo "Copying $file to $dest_dir/${file##*/}.$datetime";
            cp -Pp $file $dest_dir/${file##*/}.$datetime;
        fi;
    done
}
Here are the tests I've used to isolate extensions:
# file=something.ext1
# echo $file && echo ${file%.*} &&  echo ${file##*.} && [[ ${file%.*}
== ${file##*.} ]] && echo "No extension"
  something.ext1
  something
  ext1
# file=something
# echo $file && echo ${file%.*} &&  echo ${file##*.} && [[ ${file%.*}
== ${file##*.} ]] && echo "No extension"
something
something
something
No extension
# file=something.ext1.ext2
# echo $file && echo ${file%.*} &&  echo ${file##*.} && [[ ${file%.*}
== ${file##*.} ]] && echo "No extension"
something.ext1.ext2
something.ext1
ext2
More soon...

Attachment: signature.asc
Description: This is a digitally signed message part

Other related posts: