Folder Renaming After Tar Extraction - bash

I have a tarball, myarchive.tar.gz. When I uncompress it using "tar -zxvf myarchive.tar.gz", it creates a folder myarchive-x980-2303-ssioo. What's the easiest way to automatically rename the extracted folder to ensure it matches the name of the archive? I've checked tar's manpage, but it doesn't seem to have an option for this.

Manually create folder, and strip components from tarball:
archive=my.tar.gz
mkdir ${archive%.tar*}
tar --extract --file=${archive} --strip-components=1 --directory=${archive%.tar*}

mkdir pretty_name && tar xf ugly_name.tar -C pretty_name --strip-components 1
from https://unix.stackexchange.com/questions/11018/how-to-choose-directory-name-during-untarring

Related

How to extract tar file in Mac terminal

As titled. I want to use some command, like for .zip files I can say
unzip myfiles.zip -d mydirectory
But is there a thing for .tar file on Mac as well?
Yes, you can run:
tar -xvf myfile.tar
For .tar.gz, you can run:
tar -xzvf myfile.tar.gz
If you want to extract to any directory other than your cwd, use -C. e.g:
tar -xvf myfile.tar -C somedirectory
I suggest you read the man page for tar if you wish to do anything further:
man tar

Create tar.gz file for directory but with different name

I can create a file file.tar.gz file from a directory directory using
tar -zcvf file.tar.gz directory
Unpacking it using
tar xzf file.tar.gz
recreates the directory directory. But how to create a file.tar.gz from the directory directory that creates directory-foo when unpacking with the same command (the unpack command needs to be kept)? Renaming the directory directory to directory-foo before packing should be avoided as well as duplicating the directory.
When trying the suggested
OLDNAME=directory
NEWNAME=directory-foo
tar --transform='s,$OLDNAME/,$NEWNAME/,' -x -f file.tar.gz
I'm getting
tar: Option --transform=s,$OLDNAME/,$NEWNAME/, is not supported
Usage:
...
Tar admits a sed expression to modify file names. You may use --transform or --xform.

How to extract tgz file "with" a folder

In OS X, double click X.tgz will generate a folder namedX, putting all files into the folder (the folder name follows the .tgz file's name).
I want to do the same thing in the terminal.
However, tar -xvzf /pathtofile/X.tgz will extract all files and put them in /pathtofile/.
And by tar -xvzf /pathtofile/X.tgz -C /newpath/ the newpath does not automatically follows the .tgz file name(i.e. in this case a folder named "X").
Does anyone know if there is a simple way to do this?
Use the strip-component flag in tar:
tar --strip-components=1
This bash function does what you asked to do, which is basically just creating the directory using the filename without the ".tgz"
extract_to_dir () {
filename=$1
dirname="${filename%.*}"
mkdir ./$dirname
tar -xvzf $filename -C $dirname
}
Usage would be:
extract_to_dir filename.tgz

How to GUNZIP and UNTAR a file in one directory and move to another directory in UNIX

I have a file named 2014-03-19_cis_digital.tar.gz. in a source directory, i will have to first GUNZIP the file and then UNTAR the file and move the untarred files to another directory.
Can anyone help me in writing the shell script commands??
change your working directory first then untar/ungzip.
cd $TARGET_DIR
tar xzf $PATH_TO_FILE
You don't need to gunzip separately. You can do everything in one command:
tar -xzf /source/dir/2014-03-19_cis_digital.tar.gz -C /target/dir

tar command - compress without folders

I use a tar command like this:
tar -cf backupdb/db/2012/11/database.2012-11-25.tar.gz backupdb/db/2012/11/25/*.sql
But when I open the tar file I see a directory backupdb/db/2012/11/25 :/
How to modify the command to compress files backupdb/db/2012/11/25/*.sql but without parent directories.
I know the -C switcher in tar command, but I don't know how to use it to compress files with specify exptension.
You are looking for the -C flag:
tar -C backupdb/db/2012/11/25/ -cf backupdb/db/2012/11/database.2012-11-25.tar.gz *.sql

Resources