1. 39

  2. 12

    At least on Debian’s build of tar, f - is redundant as the default input and output are stdin and stdout. I would be somewhat surprised if anyone built it differently. Also, every tar I looked up the manpage for (GNU, BSD, Solaris) supports the -C option to set tar’s working directory, eliminating the need to cd in a subshell. I would write that command as tar c -C src . | tar x -C dest

    I use this technique over nc somewhat regularly as a quick-and-dirty unauthenticated scp. One thing to be aware of is that tar faithfully preserves user ids (if it can), which can result in files at the destination with no owner (or with an unexpected owner!) if source and destination are on different machines with different sets of users.

    1. 6

      bsdtar does the following:

       -f file, --file file
               Read the archive from or write the archive to the specified file.
               The filename can be - for standard input or standard output.  The
               default varies by system; on FreeBSD, the default is /dev/sa0; on
               Linux, the default is /dev/st0.
      1. 2

        Solaris’ tar (at least Solaris <= 10; Solaris 11 userland mostly switch to GNU) also defaults to the tape device. It is Tape ARchive, after all :-)

      2. 2

        note that c style options are not POSIX:


        better would be tar -c -C src .

        1. 4

          tar is not posix. Anybody implementing tar had better implement tar options too.

        2. 1

          I actually like the fact that even if tar supports a non-standard way of invoking, the standard way (prefixing options with -) still works. I also think that the command line invocation given in the blog is rather more intuitive than the command line you provide when the user is not that familiar with intricacies of tar command (especially the -C option and the cd command in a subshell.).

        3. 5

          I thought that looked very familiar, and I found it in the manual page: http://man.cat-v.org/plan_9/1/tar

                Tar can be used to copy hierarchies thus:
                     @{cd fromdir && tar c .} | @{cd todir && tar xT}
                Dircp does this.
          1. 2

            I first learned this technique when I watched someone use it to copy files between servers. rsh accepts standard input, so you can just pipe to it. I used it recently when I wanted to copy a directory of files to a remote machine - though I used ssh, as rsh wasn’t around (funny that!)

            1. 7

              I often find myself piping data over ssh using dd when there’s no scp. e.g.

              dd if=local-file | ssh <host> "dd of=/path/to/remote-file"

              Maybe I should switch to using tar piped over ssh so it handles directories, preserves permissions (and presumably doesn’t need the destination filename repeating if its the same as the source filename).

              1. 1

                When I do that and write to a removable device (e.g. SD card) on the remote I get a feeling that there is some great power in what’s achieved with so little (user visible complexity)

              2. 1

                Yeah, it’s a great technique. That the std* streams are preserved across rsh (ssh) is a wonderful feature.

              3. 1

                On boxen with gnu cp, couldn’t you just use cp -fa?

                1. 2

                  If you’re copying files locally, yes. The tar pipe will work between machines, too (as long as the relevant numeric userids line up).

                  1. 1

                    Of course, but I was referring to the example in the article, and that example looks very much like local file copy to me.

                2. 1

                  A great tool I often use with the stdin/stdout pipe trick that not too many people seem to use anymore is afio, which is a cpio-like archiver with a very long history and even longer list of features.