Home > Back-end >  Bash creating tar file with append
Bash creating tar file with append

Time:09-09

in the manual, it says that tar -r appends a file to an existing tar file, but when I code, for example:

tar -rf hello.tar hello

and hello.tar doesn't previously exist, it is created automatically. Thus, is it the same to use -r or -c for creating a tar file?

CodePudding user response:

If the file does not exist, the behaviour of -c, --create and -r, --append is identical.

However, behaviour differs if the file already exists. In that case, -c, --create will overwrite the file, whereas -r, --append will append to the end of the file.

Note that appending might have some unexpected effects. If you execute your command above twice, you will end up with a tar archive that contains two files 'hello':

$ tar rf hello.tar hello
$ tar rf hello.tar hello
$ tar tf hello.tar
hello
hello

tar stands for "tape archiver" and comes from the times, when physical tapes were still commonly used as backup medium. Appending to a tape makes sense, but when dealing with tar-files instead of tapes, it only makes sense, if the files are really large, otherwise recreating the archive is often the better option.

Another use-case for -r, --append, as chepner pointed out, is when you want to add files from different directories, but don't want the archive to reflect the same directory structure you currently have on your file system. In this case, using -c, --create from the first directory and then switching directories and using -r, --append for adding additional files makes sense.

  • Related