Extract from tar file to different directory in Bash - bash

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

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

why does this tar command fail when passed as string in ssh call?

ssh user#server "sudo tar -czf - -C /path/To/Directory *.properties" | tar xzf -
It fails with following error -
tar: *.properties: Cannot stat: No such file or directory
tar: Exiting with failure status due to previous errors
If I ssh to server and then on server, instead of tar -czf - use tar -czf abc.tgz and remove the piped command at the end, it works correctly i.e. compress all the .properties file from the directory?
I have tried using --wildcards parameter as well as -P and the complete path ending with *.properties, but they didn't work either.
The shell at the far end expands *.properties in user's home directory, before it execs tar. I'm guessing there's no matching files in ~user.
What you probably want is something more like (assuming user can access the directory)
ssh user#server 'cd /path/To/Directory && sudo tar -czf - *.properties' \
| tar xzf -
Or, use tar's --wildcards option, and stop the remote shell expanding the argument:
ssh user#server 'sudo tar -czf - -C /path/To/Directory --wildcards \*.properties'

How to untar all tar files in current directory using Putty

How can I untar all tar files in one command using Putty.
I Tried the following but its not un-tarring (all files start with alcatelS*)
tar -xfv alcatelS*.tar
It is not working i don't get no errors and it is not un-tarring.
Thank you,
-xfv is wrong since v is being referred as the file instead. Also, tar can't accept multiple files to extract at once. Perhaps -M can be used but it's a little stubborn when I tried it. Also, it would be difficult to pass multiple arguments that were extracted from pathname expansion i.e. you have to do tar -xvM -f file1.tar -f file2.tar.
Do this instead:
for F in alcatelS*.tar; do
tar -xvf "$F"
done
Or one-line: (EDIT: Sorry that -is- a "one"-liner but I find that not technically a real one-liner, just a condensed one so I should haven't referred to that as a one-liner. Avoid the wrong convention.)
for F in alcatelS*.tar; do tar -xvf "$F"; done
You can use following command for extract all tar.gz files in directory in unix
find . -name 'alcatelS*.tar.gz' -exec tar -xvf {} \;
Following is my favorite way to untar multiple tar files:
ls *tar.gz | xargs -n1 tar xvf
Can be done in one line:
cat *.tar | tar -xvf - -i

Bash create .tar.gz on Solaris

I'm writting a bash script, which should create a .tar.gz archive from specified directory including file structure. This is a homework and I need it to work on Solaris system my school uses. That means I can't use tar like this
tar xvzf archive.tar.gz ./
because this system uses some older version of tar without -z option. I use it like this
tar xvf - ./ | gzip > archive.tar.gz
It works fine except a strange fact, that when I examine contents of the archive, it contains itself (in other words, it contains "archive.tar.gz"). How can I prevent this?
This works:
tar cvf - | gzip > archive.tar.gz
Thing is, that file named "archive.tar.gz" is created immediately when you run your command. Meaning, before gzip is called. It's just blank file, but it is in directory. To prevent including it into resulting archive, you can try to modify your script in one of following ways:
tar xvf - ./ | gzip > ../archive.tar.gz
tar xvf - {path_to_dir_you_want_to_compress_files_from} | gzip > archive.tar.gz
Sadly, I can't check if either of this scripts works, because I don't have Solaris anywhere. Please, let me know if any of that works.

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