2

Can it be done with tar? Do I have to write a bash script?

2 Answers 2

9
tar -xzf foo.tar.gz -C bar/

This extracts a gzipped foo.tar.gz after changing directory to bar

2

From tar 1.28 you can use --one-top-level option.

tar zxvf archive_name.tar.gz --one-top-level

This extracts archive_name.tar.gz to a directory named archive_name.

Or make a bash function in your .bashrc file, sou you can use it anywhere. I called it tar2dir.

tar2dir() {
        mkdir $(basename $1 .tar.gz)
        tar xf $1 -C $(basename $1 .tar.gz)
}

You must log in to answer this question.

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