“tar --directory” won't work [closed]

混江龙づ霸主 提交于 2019-12-12 19:49:10

问题


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.

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

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?


回答1:


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.

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



回答2:


You have to use a directory:

tar -vcf files.tar /path/to/directory

If your files are in your folder, then use the . to reference the current folder:

tar -vcf files.tar .


来源:https://stackoverflow.com/questions/8042963/tar-directory-wont-work

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!