[英]How I can pack into tar-file only last directory from path?
How I can pack into tar-file only last directory from path?如何仅将路径中的最后一个目录打包到 tar 文件中? 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.我只获得dir1目录的包含,没有它本身。
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/file1.txt、file2.txt、file3.txt,但我需要在我的档案中这样的结构 -
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 指令将使您更改为 dir1,因此不会存档文件夹,而是存档其内容:
-C, --directory DIR change to directory DIR
you cannot do this directly through tar.您不能直接通过 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.如果我理解您的要求是正确的,您希望您的 tar 文件包含该目录。
Try it without the -C flag as in:在没有 -C 标志的情况下尝试,如下所示:
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 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 ./
.如果你指定
-C
那么你的目录路径是./
。 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.