Logic Assembly Software Logo
Linux Tips Index --> Linux - How to Decompress a .tar.gz file into its own directory
For these instructions, the file will be referred to as archive.tar.gz.

General Method
Navigate to the directory where you want the file(s) extracted, such as:

cd /mnt/sourcecode/

Then you can un-tar and un-zip the file with a single tar command:

tar -vxzf /mnt/mydownloads/archive.tar.gz

The options stand for verbose, extract, pass thru gzip first, specify file.

If all goes well, the archive will generally extract to its own directory usually with the same or similar name as the archive file (e.g. archive).

Alternate Method (safer)
Although uncommon, you may run across an archive file that does not extract itself cleanly into its own directory. For this case, it is safer to extract the archive in a temporary directory (e.g. temp), to avoid potentially hundreds of files not being placed into their own directory and thrown into the current directory with all of your downloaded files; as in this example anyway. Then you have the tedious task of deleting or moving each file and/or directory that does not belong.

To use the alternate method, after the cd command in the section above, follow the replacement steps below. You may substitute temp for your own temporary directory name.

mkdir temp
cd temp
tar -vxzf /mnt/mydownloads/archive.tar.gz

After looking at the tar command's output or running the ls command, you can see if there exists a single clean directory where the file(s) were extracted to. If yes, move that directory to its final destination and remove the temporary directory with the following steps:

mv archive ..
cd ..
rmdir temp

Otherwise, if you see a bunch of files extracted to the current directory (the temporary directory you just created) follow the sets of steps below:

cd ..
mv temp archive

This command renames the temporary directory to the final destination directory for your archive.
Please report comments or errors on this page using the Contact Form. Thank you!
Copyright © 2006-2014, Logic Assembly, All Rights Reserved.