In this tutorial we will learn on how to extract tar files in Linux. Tar is a tool/utility to generate a backup on a Linux machine. It has a wide range of choices that one may utilize to define the goal of the activity.
One thing to know is that tar files may be extracted to a directory of your choice, not only the one you’re currently working in.
Before continuing with this post, you may learn more about the tar backup software in GNU Tar
Syntax
The tar utility’s general syntax for file extraction is as follows:
tar -xf package_name.tar -C /desired/folder
tar -xf package_name.tar.gz --directory /desired/folder
The -C option is used in the syntax above to specify a directory other than the current working directory.
Extracting to specific directory
I’ll extract the files from packages.tar in the first example and save them in the location /opt/utility . Make sure the directory you wish to extract the tar file into is always present.
Create the desired directory
mkdir /opt/utility
Now to extract the files in packages.tar
to /opt/utility
, run the command below:
tar -xvf packages.tar -C /opt/utility
Here -v is used to monitor the progress of the tar extraction.
Extracting tar.bz2, .tar.bz, .tbz or .tbz2 Files
Let us create one directory /opt/tar2
mkdir /opt/tar2/
Extract the files now
tar -jvxf packages.tbz2 -C /opt/tar2/
Extracting only specific or selected files from tar
You may specify which files you wish to extract from a.tar file only using the tar application. In the following example, I will use a tar file to extract particular files to a designated directory:
mkdir /opt/tar_files
tar -xvf centOS7.tar etc/issue etc/ntp.conf etc/mysql/ -C /opt/tar_files
Conclusion
That concludes the process of extracting certain files from a tar file and extracting tar files to a specified directory. Please leave a comment if this tutorial is useful to you, if you have any more questions, or if you have any further information. For more Linux related commands you may also like to visit the Commands Cheat Sheet