untar filename.tr.gz to directory "filename"

Jorre picture Jorre · Jun 1, 2010 · Viewed 37.6k times · Source

I would like to untar an archive e.g. "tar123.tar.gz" to directory /myunzip/tar123/" using a shell command.

tar -xf tar123.tar.gz will decompress the files but in the same directory as where I'm working in.

If the filename would be "tar233.tar.gz" I want it to be decompressed to /myunzip/tar233.tar.gz" so destination directory would be based on the filename.

Does anyone know if the tar command can do this?

Answer

Randy Proctor picture Randy Proctor · Jun 1, 2010

With Bash and GNU tar:

file=tar123.tar.gz
dir=/myunzip/${file%.tar.gz}
mkdir -p $dir
tar -C $dir -xzf $file