5

Before you hit me with the obvious, I know, the backup option makes a backup of a file.

But the thing is, the cp command in general backs up a file. One could argue a copy of a file is a backup.

So more precisely, my question is this: what does the -b option do that the cp command doesn't do already?

The cp(1) man page gives the following description of the --backup option:

make a backup of each existing destination file

This definition isn't very useful, basically saying "the backup option makes a backup". This gives no indication as to what -b adds to the cp

I know -b puts some suffix at the end of the name of the new file. But is there anything else it does? Or is that it? Is a -b backup just a cp command that adds something to the end of the filename?

P.S. Do you typically use -b when making backups in your daily work? Or do you just stick to -a?

1

2 Answers 2

4

From the same (GNU coreutils) cp manpage:

       --backup[=CONTROL]
              make a backup of each existing destination file

So, natural, we look for the definition of CONTROL in that document:

       The backup suffix is '~', unless set with --suffix or SIMPLE_BACKUP_SUFFIX.  The version control method may be selected via the --backup option or through the VERSION_CONTROL environment variable.  Here are the values:

       none, off
              never make backups (even if --backup is given)

       numbered, t
              make numbered backups

       existing, nil
              numbered if numbered backups exist, simple otherwise

       simple, never
              always make simple backups

But is there anything else it does?

no.

P.S. Do you typically use -b when making backups in your daily work? Or do you just stick to -a?

Hm. -b sounds like a bad solution for actual backing up data. It's more for avoiding to damage data in non-backup situations. So, no, I've never used -b.

For backup, it really depends on the context, but for private/small filesystem stuff, filesystem snapshots (either through ZFS or by virtue of using thin LVM below) work very nicely. For off-device backups, it's honestly mostly rsync for complete backups. For things where you want incremental backups off-disk, duplicity is OK.

1
  • Annoyingly, the man page doesn't say what the default value of CONTROL is, i.e., which of the four behaviors is active when --backup is given without an argument. Commented Sep 12, 2023 at 18:32
3

It makes a backup copy of each destination file that already exists. The ones that would otherwise get overwritten and lost.

$ mkdir foo; cd foo
$ echo hello > hello.txt
$ echo world > world.txt
$ cp -b hello.txt world.txt
$ ls
hello.txt  world.txt~  world.txt
$ cat world.txt
hello
$ cat world.txt~
world

That world.txt~ being the backup file it created. If you look closely, you'll see that the backup file is actually the original file, just renamed. (i.e. the inode number stays the same, and so do e.g. the permissions of that file.)

2
  • Ooooh, when you make a copy and the copy is named "abc.txt", but a file named "abc.txt" already exists in that folder, instead of simply overwriting the existing "abc.txt" with our copy, or leaving the existing "abc.txt" alone and not making our copy altogether, it instead RENAMES THE EXISTING "abc.txt" to "abc.txt~" (or something else if you configure it to), THEN puts our new copy named "abc.txt" in that folder! The inode of "abc.txt" before the cp is the same as the inode of "abc.txt~" after the cp! IDK why the wording was just not clicking for me before now. Thank you! Commented Feb 2, 2023 at 19:22
  • @backslashenn, exactly! (cp -n would be the one that skips doing the copy if the target exists) Commented Feb 2, 2023 at 19:31

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.