Friday, October 19, 2012

how to tar an archive without getting the intermediate directories?

how to tar an archive without getting the intermediate directories?

http://www.thegeekstuff.com/2010/04/unix-tar-command-examples/

tar czf filename.tar.gz somefolder/
tar xzf filename.tar.gz





3
How I did it in my jenkins shell script:


cd importtool/src/ && python get_latest_schema.py
cd ../../ && tar cvzf importtool.tar.gz -C importtool/ src/


What this does is, tells:
1) tar to -C change the current directory to importtool/, and then tar its file/folder 'src/'.
2) since the working dir is / , the file importtool.tar.gz comes out at the same level as importtool/







2
Prevent parent directories from being tarred
http://stackoverflow.com/questions/4031416/prevent-parent-directories-from-being-tarred

How do I tar a directory of files and folders without including the directory itself?
http://stackoverflow.com/questions/939982/how-do-i-tar-a-directory-of-files-and-folders-without-including-the-directory-it

Q

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.

A

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$ 

That's somewhat better, but now it ads "." as a parent directory in the tar. I want only the files. – Joe Oct 27 '10 at 16:25
No, the "./" prefix is just notation. It contains the files in the "root" of the archive. – aioobe Oct 27 '10 at 16:51
Ah. I see. My bad. – Joe Oct 27 '10 at 17:01



Q

I typically do:
tar -czvf my_directory.tar my_directory
What if I just want to include everything (including any hidden system files) in my_directory, but not the directory itself? I don't want:
my_directory
   --- my_file
   --- my_file
   --- my_file
I want:
my_file
my_file
my_file


A1

cd my_directory/ && tar -zcvf ../my_dir.tgz . && cd .. 
should do the job in one line. It works well for hidden files as well. "*" doesn't expand hidden files by path name expansion at least in bash. Below is my experiment:
$ mkdir my_directory
$ touch my_directory/file1
$ touch my_directory/file2
$ touch my_directory/.hiddenfile1
$ touch my_directory/.hiddenfile2
$ cd my_directory/ && tar -zcvf ../my_dir.tgz . && cd ..
./
./file1
./file2
./.hiddenfile1
./.hiddenfile2
$ tar ztf my_dir.tgz
./
./file1
./file2
./.hiddenfile1
./.hiddenfile2

This will also work on files with spaces or other special characters. Good job! – PanCrit Jun 3 '09 at 20:45

A2

Use the -C switch of tar:
tar -czvf my_directory.tar.gz -C my_directory .
The -C my_directory tells tar to change the current directory to my_directory, and then .means "add the entire current directory" (including hidden files and sub-directories).

+1 thank you! It was the damn '.' I was missing. so aggravating – JCotton May 5 '11 at 2:08

No comments:

Post a Comment