Tutorial on extracting tar files into different directories on Linux systems

Time:2022-6-4

You don’t have to use the CD command to switch to another directory and extract it. You can extract a file using the following syntax:
grammar

Typical UNIX tar syntax:

   

Copy code

The codes are as follows:

tar -xf file.name.tar -C /path/to/directory

Gnu/tar syntax:

   

Copy code

The codes are as follows:

tar xf file.tar -C /path/to/directory

    
   

Copy code

The codes are as follows:

tar xf file.tar –directory /path/to/directory

Example: unzip files to another directory

In this case. I unzipped $home/etc.backup Tar to the /tmp/data directory. First, you need to create this directory manually. Enter:

   

Copy code

The codes are as follows:

mkdir /tmp/data

To unzip $home/etc.backup Tar to /tmp/data, enter:

   

Copy code

The codes are as follows:

tar -xf $HOME/etc.backup.tar -C /tmp/data

To see the progress, use the -v option:

   

Copy code

The codes are as follows:

tar -xvf $HOME/etc.backup.tar -C /tmp/data

Sample output:
2015611155058145.gif (568×340)

Gif 01: tar command unzips files to different directories

You can also specify the extracted file:

   

Copy code

The codes are as follows:

tar -xvf $HOME/etc.backup.tar file1 file2 file3 dir1 -C /tmp/data

To unzip foo tar. GZ (.Tgz extension file) package to /tmp/bar, enter:

   

Copy code

The codes are as follows:

mkdir /tmp/bar
tar -zxvf foo.tar.gz -C /tmp/bar

To unzip foo tar. Bz2 (.Tbz,.Tbz2 and.Tb2 extension files) package to /tmp/bar, enter:

   

Copy code

The codes are as follows:

mkdir /tmp/bar
tar -jxvf foo.tar.bz2 -C /tmp/bar