简体   繁体   中英

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.

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

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  

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
$ 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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM