GNU bug report logs - #11809
document "So how do we just simply make a backup file?"

Previous Next

Package: coreutils;

Reported by: jidanni <at> jidanni.org

Date: Thu, 28 Jun 2012 15:36:02 UTC

Severity: normal

Tags: fixed

Done: Assaf Gordon <assafgordon <at> gmail.com>

Bug is archived. No further changes may be made.

Full log


Message #8 received at 11809 <at> debbugs.gnu.org (full text, mbox):

From: Jim Meyering <jim <at> meyering.net>
To: jidanni <at> jidanni.org
Cc: 11809 <at> debbugs.gnu.org
Subject: Re: bug#11809: document "So how do we just simply make a backup file?"
Date: Thu, 28 Jun 2012 18:01:31 +0200
jidanni <at> jidanni.org wrote:
> (info "(coreutils) Backup options") should add some examples, for
> "So how do we make a backup file of m?"
> $ ls
> m
> $ cp -b m m #no go

Thanks for the suggestion.
I use this zsh/bash shell function:

backup ()
{
  local i
  for i in "$@"; do
    command cp -bf "$i" "$i"
  done
}

but as I inserted the above, I realize it's buggy.
It doesn't propagate failure like you'd expect,
so here's a better one:

backup()
{
  local i fail=0
  for i in "$@"; do
    command cp -bf -- "$i" "$i" || fail=1
  done
  return $fail
}

That's already almost what info coreutils says:

  Make a backup of each file that would otherwise be overwritten or removed.
  As a special case, @command{cp} makes a backup of @var{source} when the force
  and backup options are given and @var{source} and @var{dest} are the same
  name for an existing, regular file.  One useful application of this
  combination of options is this tiny Bourne shell script:

  @example
  #!/bin/sh
  # Usage: backup FILE...
  # Create a @sc{gnu}-style backup of each listed FILE.
  for i; do
    cp --backup --force -- "$i" "$i"
  done
  @end example

I'll adjust that to reflect the above improvement:
Do you think that's enough?

> $ cp m n
> $ mv -b n m




This bug report was last modified 6 years and 279 days ago.

Previous Next


GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.