Bash 在 Solaris 上创建 .tar.gz

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16045339/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 05:12:08  来源:igfitidea点击:

Bash create .tar.gz on Solaris

bashgzipsolarisarchivetar

提问by Michal Artazov

I'm writting a bash script, which should create a .tar.gz archive from specified directory including file structure. This is a homework and I need it to work on Solaris system my school uses. That means I can't use tar like this

我正在编写一个 bash 脚本,它应该从指定的目录(包括文件结构)创建一个 .tar.gz 存档。这是一个家庭作业,我需要它在我学校使用的 Solaris 系统上工作。这意味着我不能像这样使用 tar

tar xvzf archive.tar.gz ./

because this system uses some older version of tar without -z option. I use it like this

因为这个系统使用了一些没有 -z 选项的旧版本 tar。我像这样使用它

tar xvf - ./ | gzip > archive.tar.gz

It works fine except a strange fact, that when I examine contents of the archive, it contains itself (in other words, it contains "archive.tar.gz"). How can I prevent this?

它工作正常,除了一个奇怪的事实,当我检查档案的内容时,它包含自身(换句话说,它包含“archive.tar.gz”)。我怎样才能防止这种情况?

回答by BentheFolker

This works: tar cvf - | gzip > archive.tar.gz

这有效: tar cvf - | gzip > 存档.tar.gz

回答by user2256686

Thing is, that file named "archive.tar.gz" is created immediately when you run your command. Meaning, before gzip is called. It's just blank file, but it is in directory. To prevent including it into resulting archive, you can try to modify your script in one of following ways:

事实是,当您运行命令时,会立即创建名为“archive.tar.gz”的文件。意思是,在调用 gzip 之前。它只是一个空白文件,但它在目录中。为防止将其包含在生成的存档中,您可以尝试通过以下方式之一修改脚本:

  1. tar xvf - ./ | gzip > ../archive.tar.gz
  2. tar xvf - {path_to_dir_you_want_to_compress_files_from} | gzip > archive.tar.gz
  1. tar xvf - ./ | gzip > ../archive.tar.gz
  2. tar xvf - {path_to_dir_you_want_to_compress_files_from} | gzip > archive.tar.gz

Sadly, I can't check if either of this scripts works, because I don't have Solaris anywhere. Please, let me know if any of that works.

遗憾的是,我无法检查此脚本中的任何一个是否有效,因为我在任何地方都没有 Solaris。请让我知道这是否有效。