0

I'm trying to understand how to use tar in a Bash script to copy a project directory to another location while excluding the node_modules subdirectory. There was a potentially useful comment posted here, which I've rewritten for my use case like so:

tar -cf - --exclude="node_modules" "${source}" | tar -xf - -C "${target}"

However, I'm reluctant to run this command before understanding a few things about it. Here are my questions -

  1. What is the purpose of the lone hyphen before the --exclude and the lone hyphen before the -C?
  2. Is a .tar file actually created somewhere in the file system? If so, where and what is it named?

1 Answer 1

1
  1. The - argument is defined by many utilities as standard input/output depending on the context, and for GNU tar it isn't even needed, as the default file it uses for input/output is stdio. According to that, tar -cf - --exclude="node_modules" "${source}" writes a tar archive to standard output, which is actually a pipe whose other end is given to the following command tar -xf - -C "${target}" as standard input for extraction.
  2. In your pipeline standard output is never redirected to a file so no, no tar archive is created is actually created.
2
  • Regarding 1...the - argument isn't needed for GNU tar...does this mean the hyphens could be dropped completely from the statement like this: tar -cf --exclude="node_modules" "${source}" | tar -xf -C "${target}"?
    – knot22
    Commented Aug 1, 2022 at 2:40
  • 1
    -f is a tar option with an argument specifying the path of the input/output archive. in both commands the value of this option is -. if you want to omit it you must not include the option, that is tar -cf - --exclude="node_modules" "${source}" becomes tar -c --exclude="node_modules" "${source}"
    – don_aman
    Commented Aug 1, 2022 at 3:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .