cancel
Showing results for 
Search instead for 
Did you mean: 
Tahaik
Mission Specialist
Mission Specialist
  • 5,636 Views

Tar command without specifying compression tool

Jump to solution
Hi all,
I'm very new to linux, planning to go for rhcsa first. I went to do some small lab exercise. I come across this question
Eg run the tar command and uncompress and restore both archives without specifying the compression tools used
I'm not sure what this really meant, from what I know is
Tar -xzvf example.tar.gz
Tar -xjvf example.tar.bz2
Labels (1)
Tags (1)
0 Kudos
3 Solutions

Accepted Solutions
Lisenet
Starfighter Starfighter
Starfighter
  • 5,632 Views

You can use "tar -xf filename". I personally don't specify a compression method when extracting files from an archive.

View solution in original post

Tahaik
Mission Specialist
Mission Specialist
  • 5,618 Views
Hi @Lisenet and tracy_baker, firstly I want to thank both of you to clear my doubt, sorry to ask a dumb question here
So you mean to say that eg
Tar -czvf /home/root2/example.tar.gz /etc
Then I can uncompress using this
Tar -xf /home/root2/example.tar.gz

View solution in original post

0 Kudos
Tracy_Baker
Starfighter Starfighter
Starfighter
  • 5,597 Views

@Tahaik 

You said:

Tar -czvf /home/root2/example.tar.gz /etc
Then I can uncompress using this
Tar -xf /home/root2/example.tar.gz

------

Well, kind of. I'm going to be specific here.

First, the command in tar, not Tar

If a tarball was created with this command: tar -czvf /home/root2/example.tar.gz /etc, you would want to decompress it into an empty directory.

To decompress:

mkdir -p /home/root2/example-backup
cd /home/root2/example-backup
tar -xf /home/root2/example.tar.gz

So... the -z (gzip) option is used when creating the compressed tarball, but is not used when extracting it.

Program Lead at Arizona's first Red Hat Academy, est. 2005
Estrella Mountain Community College

View solution in original post

0 Kudos
5 Replies
Lisenet
Starfighter Starfighter
Starfighter
  • 5,633 Views

You can use "tar -xf filename". I personally don't specify a compression method when extracting files from an archive.

Tracy_Baker
Starfighter Starfighter
Starfighter
  • 5,627 Views

As @Lisenet mentioned, tar -xf <filename> will extract the compressed archive, even though the compression option is not specified.

With good practices, you'll already know the compression type because of the file's extension: example.tar.gz or example.tar.bz2

If you need to know what the compression type is (maybe there's no extension), use:
file <filename> and it will tell you, for example:

[root@clientx ~]# file etc-backup
etc-backup: gzip compressed data, last modified: Wed Jun 10 21:50:16 2020, from Unix, original size 27176960

(Although I may be mistaken, I thought older versions of the tar command required the proper compresstion option type to be specified when extracting a tarball; in other words, it would not auto-detect the compression that was used to create the file. Even so, it doesn't hurt to include the option, as long as it is the correct one. I used to do it all the time by habit.)

Program Lead at Arizona's first Red Hat Academy, est. 2005
Estrella Mountain Community College
Tahaik
Mission Specialist
Mission Specialist
  • 5,619 Views
Hi @Lisenet and tracy_baker, firstly I want to thank both of you to clear my doubt, sorry to ask a dumb question here
So you mean to say that eg
Tar -czvf /home/root2/example.tar.gz /etc
Then I can uncompress using this
Tar -xf /home/root2/example.tar.gz
0 Kudos
Tracy_Baker
Starfighter Starfighter
Starfighter
  • 5,598 Views

@Tahaik 

You said:

Tar -czvf /home/root2/example.tar.gz /etc
Then I can uncompress using this
Tar -xf /home/root2/example.tar.gz

------

Well, kind of. I'm going to be specific here.

First, the command in tar, not Tar

If a tarball was created with this command: tar -czvf /home/root2/example.tar.gz /etc, you would want to decompress it into an empty directory.

To decompress:

mkdir -p /home/root2/example-backup
cd /home/root2/example-backup
tar -xf /home/root2/example.tar.gz

So... the -z (gzip) option is used when creating the compressed tarball, but is not used when extracting it.

Program Lead at Arizona's first Red Hat Academy, est. 2005
Estrella Mountain Community College
0 Kudos
Tahaik
Mission Specialist
Mission Specialist
  • 5,583 Views
Hi tracy_baker
Thanks for the tip, I guess when I type tar it went to Tar
0 Kudos
Join the discussion
You must log in to join this conversation.