How to create an empty tgz file?
I tried
tar czvf /tmp/empty.tgz --from-file /dev/null
tar: Option --from-file is not supported
The switch you're looking for is --files-from or -T:
tar czvf /tmp/empty.tgz --files-from=/dev/null
Related
I'm new to Bash and trying to unzip a tarball. Code so far:
#!/bin/bash
tar="/cdrom/java/jre1-8u181-x64tar.gz"
# Unpack tarball
gunzip < $tar | tar xf -
This extracts the archive in current directory. How can I specify a location?
Using Solaris 10, Bash 3.2.51
This works pretty well everywhere - including Solaris, and as you only change directory in a sub-shell, it doesn't affect your location in the current session:
gunzip < $tar | ( cd /some/where/else && tar xf -)
To extract the file to a specific directory
gunzip < $tar | tar -xf - --directory /path/to/extract/to
or
gunzip < $tar | tar -xf - -C /path/to/extract/to
As you wrote your command is unpacking in the current directory:
gunzip < $tar | tar xf -
Add the "-C" option to give it an alternate target directory:
gunzip < $tar | tar xf - -C /another/target/directory
Note that the Solaris tar does not understand the --directory option.
See the Solaris tar manpage.
Just for the sake of completeness if you have Gnu-Tar (which is available for Solaris too) you can use this simpler command (which unzips and unpacks in one go):
tar xzf $tar -C /another/target/directory
On a side note:
many people use a leading dash for the tar command parameters. That is redundant.
See the answers to this question if you are interested.
The -xf part of tar means to extract into the "f" file. try changing the tar command to something like
Edit
...| tar -xf - -C /path/to/your/desired/result/folder
sorry, #pitseeker is correct. The -C option tells tar to change directory then do the extract
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.
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
How to append file to tar, e.g. file located in /usr/file.txt?
I want to append it in tar to var/file.txt for future extract it into [tar location]/var/file.txt instead of [tar location]/usr/file.txt, using the
tar --append --file foo.tar bar
Is it possible to put file in tar this way without replacing /usr/file.txt to /var/file.txt before archiving?
If you're using GNU tar, there's a --transform option for that, which takes a sed-like expression as argument:
tar --append --file foo.tar --transform='s,^usr/,var/,' /usr/file.txt
The only way I can think of is to use a symlink var/file.txt -> /usr/file.txt.
mkdir var && ln -s /usr/file.txt var
tar --dereference --append --file foo.tar var
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