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
Related
I'm trying to create a bash script that creates an archive of one folder (or the folder content) to a specific directory.
My version works but instead to archive one folder it archives me the whole path which I don't want.
Is there a way to solve this problem without using cd? I saw some solutions using -C but I get the following error, no matter where I place it: refusing to create an empty archive
SRCDIR=~/Documents/sub1/sub2/sub3/source/*
DESTDIR=~/Documents/sub1/sub2/sub3/target/backup.tgz
tar czf $DESTDIR --absolute-names $SRCDIR
You can try this:
SRCDIR=~/Documents/sub1/sub2/sub3/source/*
DESTDIR=~/Documents/sub1/sub2/sub3/target/backup.tgz
tar czf $DESTDIR --absolute-names -C $(dirname $SRCDIR) $(basename $SRCDIR)
As you can see in my example I'm using -C option to change the directory in combination with dirname and basename for source directory
OTHER OPTIONS
-C, --directory DIR
change to directory DIR
Usually, when the '-C' is being used, the wild-card expansion does not work well - as it will try to expand the wildcard ('*' in this case) at the current directory where the command is invoked, and NOT at the SRCDIR location.
If you want all files to be included in the repository, use the '.' (it will also include hidden files!).
SRCDIR=~/Documents/sub1/sub2/sub3/source/*
DESTDIR=~/Documents/sub1/sub2/sub3/target/backup.tgz
tar czf $DESTDIR -C "$(dirname $SRCDIR)" .
As a side note, the original post is using '--absolute-names'. You might want to reconsider using this flag, as it make it extremely difficult to restore the files into any other location but the original location of the files.
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
Lets say I am currently in the folder
/home/user1234
Next, there is a file
/home/user1234/files/mylog.log
From the /home/user1234 folder I would like to tar.bz2 mylog.log . What I have done was run the command
tar cjf /home/user1234/files/mylog.log.tar.bz2 /home/user1234/files/mylog.log
This works however in the tar.bz2 file the compressed file is under its fully qualified path. I would like the compressed file to be in the 'root' of the tar.bz2. Can anyone point me in the right direction? I've been looking at the -C option you can pass to tar but I can't seem to get it to work.
Try:
tar cjf /home/user1234/files/mylog.log.tar.bz2 -C /home/user1234/files/ mylog.log
The -C essentially "changes" to that directory before compressing/decompressing the list of files (denoted by mylog.log).
I have hundreds of directories, each containing several zip files. I would like to iterate over each directory and unzip all zip files, placing the contents of the zip files into the same directory as the zip files themselves (without creating new sub-directories). Here's the bash script I have:
#!/bin/bash
src="/path/to/directories"
for dir in `ls "$src/"`
do
unzip "$src/$dir/*"
done
This script does the unzipping, but it creates thousands of sub-directories and dumps them on my desktop! How can I get the desired behavior? I'm on Mac OSX if that makes a difference.
#!/bin/bash
src=/path/to/directories
for dir in "$src"/*
do
(cd "$dir" && unzip '*')
done
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