How to extract tar file in Mac terminal - 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

Related

Extract from tar file to different directory in 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

cannot tar on Mac?

I am trying to compress a log file directory on my mac. The log directory exists.
For example /var/logs/my_log_dir/
I am trying to run something like:
$ tar –cpzf x.tar.gz /var/logs/my_log_dir
I also tried other variations such as compressing only one of the files in the log dir - no luck. All I get from my mac is:
Usage:
List: tar -tf <archive-filename>
Extract: tar -xf <archive-filename>
Create: tar -cf <archive-filename> [filenames...]
Help: tar --help
Thoughts?
Using -p doesn't make sense in the context of compression, since you're already shoving in whatever permissions are on the files by default. When extracting, however, you can use the -p flag if you're concerned about permissions.
Compress: tar –czvf x.tar.gz /var/logs/my_log_dir
Extract: tar –xpzvf x.tar.gz
-p option (preserve file permissions) works in x mode only.

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

Folder Renaming After Tar Extraction

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

Resources