tenzor
tenzor

Reputation: 85

How I can pack into tar-file only last directory from path?

How I can pack into tar-file only last directory from path? For example, I have several paths

/usr/local/files/dir1/

   file1.txt
   file2.txt
   file3.txt

/usr/local/files/dir2/

   file3.txt
   file4.txt
   file5.txt

if I run command:

tar czf my_arch.tar.gz -C /usr/local/files/dir1 .

I gain only containment of dir1 catalog, without itself.

So I have - my_arch.tar.gz/file1.txt, file2.txt, file3.txt, But I need structure like that inside my archive -

my_arch.tar.gz/dir1/file1.txt, file2.txt, file3.txt

How I can do this?

Thank you.

Upvotes: 5

Views: 10435

Answers (5)

FarK
FarK

Reputation: 636

If you specify -C then you directory path is ./. Probably the following works like you want:

$ touch asdf/foo/bar/{1,2,3}
$ tree asdf/
asdf/
└── foo
    └── bar
        ├── 1
        ├── 2
        └── 3

2 directories, 3 files
$ tar -cv -C asdf/foo/bar/ -f asdf.tar ./
./
./3
./2
./1
$ tar tf asdf.tar
./
./3
./2
./1

Upvotes: -1

anthumchris
anthumchris

Reputation: 9072

$ tar vczf tmp/export/files.tar.gz -C tmp/export src

structure for files.tar.gz

src
src/app
src/app/main.js
src/app/util
src/app/util/runtime.js

Upvotes: 5

mr.g
mr.g

Reputation: 31

you cannot do this directly through tar. here's my suggestion :

#!/bin/bash
mydir=/my_dir/whit/long_and/complicated_path/the_stuff_is_here
dirname=$(dirname $mydir )
basename=$(basename $mydir )

 tar cvf /tmp/$basename.tar    -C $dirname $basename  

Upvotes: 3

John
John

Reputation: 1462

try

cd /usr/local/files
tar -cvzf my_arch.tar.gz dir1

The -C directive will make you change into dir1 and thus not archive the folder, but its contents:

 -C, --directory DIR
     change to directory DIR

Upvotes: 3

Al Pacifico
Al Pacifico

Reputation: 890

If I understand what you are asking correctly, you want your tar file to contain the directory.

Try it without the -C flag as in:

tar -czf my_arch.tar.gz /usr/local/files/dir1

Upvotes: 0

Related Questions