unpack 'tar' but change directory name to extract to
Use -C
and --strip-components
(See man tar
).
Example:
mkdir FOLDER
# for remote tar file
curl -L ’remote_tar_file' | tar -xz - -C FOLDER --strip-components=1
# for local tar file
tar -xzf FILENAME -C FOLDER --strip-components=1
Explanation:
The -C
flag assumes a directory is already in place so the contents of the tar file can be expanded into it. hence the mkdir FOLDER
.
The --strip-components
flag is used when a tar file would naturally expand itself into a folder, let say, like github where it examples to repo-name-master
folder. Of course you wouldn’t need the first level folder generated here so --strip-components
set to 1
would automatically remove that first folder for you. The larger the number is set the deeper nested folders are removed.
You can also use the --transform
option for a bit more flexibility. It accepts any sed replacement (s) operation.
For example, this is how I extract a Linux tarball to a new directory so I can apply a patch:
tar -xjf linux-2.6.38.tar.bz2 --transform 's/linux-2.6.38/linux-2.6.38.1/'