将 filename.tr.gz 解压到目录“filename”
我想使用 shell 命令将存档(例如“tar123.tar.gz”)解压到目录 /myunzip/tar123/”。
tar -xf tar123.tar.gz 将解压缩文件,但与我所在的目录相同 如果文件名是“tar233.tar.gz” ,
我希望将其解压缩到 /myunzip/tar233.tar.gz”,因此目标目录将基于文件名。
有谁知道 tar 命令是否可以做到这一点?
I would like to untar an archive e.g. "tar123.tar.gz" to directory /myunzip/tar123/" using a shell command.
tar -xf tar123.tar.gz will decompress the files but in the same directory as where I'm working in.
If the filename would be "tar233.tar.gz" I want it to be decompressed to /myunzip/tar233.tar.gz" so destination directory would be based on the filename.
Does anyone know if the tar command can do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
对于 Bash 和 GNU tar:
With Bash and GNU tar:
您可以在使用
-C
标志提取之前更改目录,但该目录必须已经存在。 (如果您创建特定于文件的目录,我强烈建议不要将它们称为foo.tar.gz
- 扩展名意味着它是一个存档文件,但它实际上是一个目录。将 导致混乱。)You can change directory before extracting with the
-C
flag, but the directory has to exist already. (If you create file-specific directories, I strongly recommend against calling themfoo.tar.gz
- the extension implies that it's an archive file but it's actually a directory. That will lead to confusion.)尝试
如果您使用的是 GNU tar,您还可以给它一个选项
-C "$dir"
这将导致它在解压之前更改为目录。但上面的代码即使使用青铜时代的 tar 也应该可以工作。免责声明:以上代码均未经过测试。
Try
If you're using GNU tar you can also give it an option
-C "$dir"
which will cause it to change to the directory before extracting. But the code above should work even with a Bronze Age tar.Disclaimer: none of the code above has been tested.