How to extract a single file / directory from tar archive
On UNIX or Linux, you can extract selected files or directories from a tarball without first extracting everything. This is more convenient and saves space when extracting files or directories.
To extract selected files or directories from a tar archive:
-
Check whether the file or directory you want exists in the archive with:
[root@ezylinux ~]# tar -vztf tarball.tar.gz -
Once you confirm it exists, use:
[root@ezylinux ~]# tar -xzvf tarball.tar.gz path-filename [root@ezylinux ~]# tar -xzvf tarball.tar.gz path-directory-nameHere, path-filename and path-directory-name are the file or directory paths shown by the first command.
If path-filename or path-directory-name is incorrect, extraction will fail, so check the paths carefully first. To extract selected files or directories to a specific path, add -C followed by that path. For example: To extract selected files or directories into /tmp, run:
[root@ezylinux ~]# tar -xzvf tarball.tar.gz -C /tmp path-filename
[root@ezylinux ~]# tar -xzvf tarball.tar.gz -C /tmp path-directory-name