Executing a tar command from within a Go program

370 views
Skip to first unread message

Dean Schulze

unread,
Apr 20, 2022, 12:16:37 PM4/20/22
to golang-nuts
I need to execute this tar command 

tar xzf dir1/dir2/somefile.tgz --directory=dir1/dir2/

from within a Go program.  I've verified that it works from the command line.  I've tried using

argStr := "xzf dir1/dir2/somefile.tgz --directory=dir1/dir2/"
output, err := exec.Command("tar", argStr).Output()


but it returns an error code of 2 with no other output indicating what is wrong.

Using exec.Command(name, arg) is always a guessing game as to what the command name should be and what the argument should be.  How do I execute this tar command from within Go?

wagner riffel

unread,
Apr 20, 2022, 12:28:06 PM4/20/22
to Dean Schulze, golang-nuts
On Wed Apr 20, 2022 at 6:16 PM CEST, Dean Schulze wrote:
> I need to execute this tar command
>
> *tar xzf dir1/dir2/somefile.tgz --directory=dir1/dir2/*
>

Did you considered using the packages "archive/tar" and
"compress/gzip" to achive this?

> *argStr := "xzf dir1/dir2/somefile.tgz --directory=dir1/dir2/"output, err
> := exec.Command("tar", argStr).Output()*
>

exec.Command arguments are variadic, each argument is one argv, thus I
think you meant exec.Command("tar", "xzf", "dir1/dir2/somefile.tgz",
"--directory=dir1/dir2/")

-w

wagner riffel

unread,
Apr 20, 2022, 12:36:52 PM4/20/22
to wagner riffel, Dean Schulze, golang-nuts
Also, the Output method only returns what was wrote to stdout, tar
argument parsing errors are probably in stderr, using CombinedOutput()
has better effect to debug.

Axel Wagner

unread,
Apr 20, 2022, 1:06:40 PM4/20/22
to Dean Schulze, golang-nuts
On Wed, Apr 20, 2022 at 6:16 PM Dean Schulze <dean.w....@gmail.com> wrote:
I need to execute this tar command 

tar xzf dir1/dir2/somefile.tgz --directory=dir1/dir2/

from within a Go program.  I've verified that it works from the command line.  I've tried using

argStr := "xzf dir1/dir2/somefile.tgz --directory=dir1/dir2/"
output, err := exec.Command("tar", argStr).Output()

You need to pass the args separately, not as one string:
output, err := exec.Command("tar", "xzf", "dir1/dir2/somefile.tgz", "--directory=dir1/dir2/").Output()
I also tend to set `cmd.Stderr = os.Stderr` to make sure I see error messages, when executing commands.
 


but it returns an error code of 2 with no other output indicating what is wrong.

Using exec.Command(name, arg) is always a guessing game as to what the command name should be and what the argument should be.  How do I execute this tar command from within Go?

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/4c4345b7-2155-4bd9-ba61-7a76b5256d2fn%40googlegroups.com.

Dean Schulze

unread,
Apr 20, 2022, 10:30:13 PM4/20/22
to golang-nuts
The variadic args worked perfectly.  Thanks.

I did not use the archive/tar and compress/gzip approach because that wold be a lot more complicated than just executing a tar command.  Those packages are oriented towards reading/writing the contents of archive/compressed files into the program rather than just extracting a file from a compressed archive.

Reply all
Reply to author
Forward
0 new messages