“tar --目录"不会工作 [英] "tar --directory" won't work

查看:50
本文介绍了“tar --目录"不会工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个 tarball 文件,但不包括目录名称.我知道还有另一种方法可以做到,但我想知道为什么这种方法不起作用.

I want to create a tarball of files, but not include the directory name. I know there's another way to do it, but I want to know why this way is not working.

如果我运行以下命令来创建由特定文件组成的 tarball,则它可以工作:

If I run the following to create the tarball comprised of a specific file, then it works:

tar -vcf file.tar -C <PATH TO DIR> file1

但是,如果我在通配符上运行它以包含多个文件,则会失败:

However, if I run it on a wild card to include multiple files, then it fails:

tar -vcf file.tar -C <PATH TO DIR> *

我收到一条错误消息,对于 current 目录中的每个文件(不是 -C 中指定的目录),tar: <FILE>:Cannot stat: No such file or目录

I get an error saying, for each file in the current dir (not the dir specified in -C), tar: <FILE>: Cannot stat: No such file or directory

知道为什么在通配符和文件名上运行上述命令的行为不同吗?

Any idea why running the above command on a wild card vs a file name behaves differently?

推荐答案

* 通配符在 tar 被调用之前被 shell 展开.tar 然后更改目录(因为您要求它),但随后找不到 shell 当前目录中的文件.

The * wildcard is expanded by the shell before tar is invoked. tar then changes directory (because you asked it to), but then can't find the files that were in the shell's current directory.

当然,在shell中更改目录意味着您无法打开原始当前目录中的输出文件.因此,您必须将 tar 的输出重定向到子外壳之外,如下所示:

Of course, changing directories in the shell means that you can't open the output file in the original current directory. So you have to redirect the output of tar outside of the subshell, like this:

(cd $DIR; tar -vc *) > file.tar

这篇关于“tar --目录"不会工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆