Prevent parent directories from being tarred - shell

Basically I just want to tar all the files in a directory, but not get all the parent directories in the archive.
I've tried -C, but I guess I'm not using it right.
tar -cjf archive.tar.bz2 -C /var/some/log/path ./*
This results in tar trying to add all the files in the CWD. Using the full path as last argument doesn't prevent the dirs from being added.
Seems simple enough, but can't figure it out. Somehow tar does not tar ./* as being relative to -C, although it should change to that dir.
Help appreciated.

The parent directory (/var/some/log) is included, since /var/some/log/path/.. is included when you do ./*. Try just doing
tar -cjf archive.tar.bz2 -C /var/some/log/path .
Test run:
$ find tmp/some_files
tmp/some_files
tmp/some_files/dir1
tmp/some_files/dir1/dir1file
tmp/some_files/hello
tmp/some_files/world
tmp/some_files/dir2
tmp/some_files/dir2/dir2file
$ tar -cvjf archive.tar.bz2 -C tmp/some_files/ .
./
./dir1/
./dir1/dir1file
./hello
./world
./dir2/
./dir2/dir2file
$ cd tmp/unpacked
/tmp/unpacked$ mv /home/aioobe/archive.tar.bz2 .
/tmp/unpacked$ tar -xvjf archive.tar.bz2
./
./dir1/
./dir1/dir1file
./hello
./world
./dir2/
./dir2/dir2file
/tmp/unpacked$ ls
archive.tar.bz2 dir1 dir2 hello world
/tmp/unpacked$

There's a much easier way to do this:
cd down to the directory you wish to be top level, i.e...
cd /var/lib/mysql
Remove parent directories from your tar command
/var/lib/mysql/DATABASE_NAME becomes just DATABASE_NAME
More details can be found in this blog writeup.

Related

How to extract only files and not directory heirarchy [duplicate]

This question already has answers here:
How to create flat tar archive
(2 answers)
Closed 3 years ago.
How to extract only files and not directory heirarchy.
Suppose I have a myarchive.tgz file which has many directories in it and inside each directory their are files.
eg.myarchive.tgz/folder1/file1.txt
/folder2/folder2.1/file2.txt
Now whenever I extract it gets in directory with the same heirarchy it's in archive.
I want to extract like
myarchive/file1.txt
/file2.txt
like this.
How to do it? Please help!
You could unpack the tarball to a temporary folder:
rm -rf /tmp/foo && mkdir /tmp/foo # make sure the folder exists and is empty
tar xf foo.tgz -C /tmp/foo
Then copy over the files to the final location:
rm -rf unpacked && mkdir unpacked # make sure the folder exists and is empty
find /tmp/foo -type f -exec cp -v {} unpacked \;
Note: tar -C is specific to GNU tar. If your tar doesn't understand -C use:
cd /tmp/foo
tar xf foo.tgz

Exclude current directory from tar

I'm trying to exclude the current directory from the tarball without excluding its contents, because when I extract it out using the -k flag I get an exit status of 1 and a message
./: Already exists
tar: Error exit delayed from previous errors.
How do I do this? I've tried the --exclude flag but that excludes the contents also (rightly so). I'm trying to code this for both the OSX/BSD and GNU versions of tar.
Test case:
# Setup
mkdir /tmp/stackoverflow
cd /tmp/stackoverflow
mkdir dir
touch dir/file
# Create
tar cCf dir dir.tar .
# List contents
tar tf dir.tar
gives
./
./file
showing that the current directory ./ is in the tar. This would be fine, but when I do the following:
mkdir dir2
tar xkfC dir.tar dir2
due to the -k flag, I get an exit code of 1 and the message
./: Already exists
tar: Error exit delayed from previous errors.
To exclude the current directory you can create your archive on this way:
tar cf /path/to/dir.tar ./*
use ./*instead of ., this will not match current directory (.) and therefore not include in the archive
This does the trick:
GLOBIGNORE=".:.."
cd dir
tar cf ../dir.tar *
The extra cd is to replace the use of the -C flag, so that we can use a glob. The GLOBIGNORE ignores the current directory, but also sets shopt -s dotglob implicitly to include hidden files. Using * instead of ./* means that the tar file listing doesn't list ./file, but instead lists it as file. The entire listing of the tar is:
file

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

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