bash Tar 目录内容无需在存档中创建根文件夹
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/13285737/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Tar Directory Contents Without Creating A Root Folder In Archive
提问by cryptic ツ
I am running the following snippet in a bash script in a folder. This creates an archive where there is no root folder since I am running the script within the folder whose contents I am archiving.
我正在文件夹中的 bash 脚本中运行以下代码段。这将创建一个没有根文件夹的存档,因为我正在存档其内容的文件夹中运行脚本。
tar -pczf $ARCHIVE_NAME --exclude=${ARCHIVE_NAME} --exclude=$(basename tar -pczf $ARCHIVE_NAME --exclude=${ARCHIVE_NAME} --exclude=$(basename --directory=$(dirname $ ls -a xxx
.  ..  aaa  .bbb
$ tar cvf test.tar --exclude test.tar -C xxx .
./
./.bbb
./aaa
)
) .
Archive Contents:
/./a/
/./b/
/./c/
/./.a_hidden_file
/./a_normal_file
) *
Archive Contents:
/a/
/b/
/c/
/a_normal_file
works great, but since I am using the * it is not archiving hidden files in the immediate directory. I did some searching and found archiving (ubuntu tar) hidden directoriesI have then changed it to:
效果很好,但由于我使用的是 *,它不会在直接目录中归档隐藏文件。我做了一些搜索并找到了归档(ubuntu tar)隐藏目录,然后将其更改为:
##代码##still works, but now there is a root folder being created with the name being the '.' character. The contents are then within that. How do I get my previous result, but where it still archives hidden files in the immediate directory?
仍然有效,但现在正在创建一个名为“.”的根文件夹。特点。内容就在其中。我如何获得我以前的结果,但它仍然在直接目录中存档隐藏文件?
Thanks in advance!
提前致谢!
回答by csgwro
Use -C, --directory DIR
使用 -C, --directory DIR
##代码##Example:
例子:
##代码##回答by Joakim Nohlg?rd
While it doesn't affect the unpacking of the archive (since .just refers to the same dir), if it bothers you, you can change the wildcard from *to .[^.]* *to include all the hidden files as well.
虽然它不会影响存档的解压(因为它.只是指同一个目录),但如果它打扰您,您可以将通配符从*to更改.[^.]* *为包含所有隐藏文件。
In addition, if you have hidden files beginning with .., such as ..a, you'll need to add ..?*to the list as well.
此外,如果您有以 开头的隐藏文件..,例如..a,您也需要添加..?*到列表中。

