This post will guide you how to extract a tar files into a different directory on CentOS/Ubuntu Linux. How do I extract tar archive files to a specific or different directory using tar command in Linux.
- Extract Tar Files To A Specific Directory
- Extract a single File from A Tar File
- Extract .tar.gz archive File to A Different Directory
You can use tar command to create archive files or decompress archive file to a specific directory in Linux. and How to extract or decompress Tar Files to a different or specific directory in Linux. Just see the below introduction.
Extract Tar Files To A Specific Directory
If you want to extract a tar archive file (test.tar) into a specific directory (/tmp) in your Linux system, you can use the following command:
# tar -xvf test.tar -C /tmp
Or
# tar -xvf test.tar --directory /tmp
Outputs:
devops@devops-osetc:~/working$ tar -xvf test.tar -C /tmp fio1.txt fio2.txt fio3.txt devops@devops-osetc:~/working$ ls /tmp fio1.txt fio2.txt fio3.txt devops@devops-osetc:~/working$ tar -xvf test.tar --directory /tmp fio1.txt fio2.txt fio3.txt devops@devops-osetc:~/working$ ls /tmp fio1.txt fio2.txt fio3.txt
Extract a single File from A Tar File
If you only want to extract a single file or specific files from a tar archive in Linux, you can execute the following command:
# tar -xvf test.tar -C /tmp fio1.txt fio2.txt
Outputs:
devops@devops-osetc:~/working$ tar xvf test.tar -C tmp fio1.txt fio2.txt fio1.txt fio2.txt devops@devops-osetc:~/working$ ls tmp fio1.txt fio2.txt
Note: the single file argument must be last in your command.
Extract .tar.gz archive File to A Different Directory
If you want to extract a .tar.gz or .tgz archive file to a specific directory, you can also use the tar command to achieve it. type the following command:
# tar -zxvf text.tar.gz -c ./tmp
Outputs:
devops@devops-osetc:~/working$ tar -zxvf test.tar.gz -C ./tmp fio1.txt fio2.txt fio3.txt devops@devops-osetc:~/working$ ls ./tmp fio1.txt fio2.txt fio3.txt