仅当 tar 中存在可提取到我的 CWD 的文件时,才将 .tgz 提取到特定子文件夹中
大多数 tar 文件都会解压到自己的子文件夹中(因为编写开源实用程序的人都是了不起的人)。
有些提取到我的 cwd 中,这使所有内容变得混乱。我知道有一种方法可以查看 tar 中的内容,但我想编写一个 bash 脚本,从本质上保证我最终不会将 15 个文件提取到我的主文件夹中。
有什么指点吗?
伪代码:
if [listing of tar files] has any file that doesn't have a '/' in it:
mkdir [tar filename without extension]
tar xzvf [tar filename] into [the new folder]
else:
tar xzvf [tar filename] into cwd
编辑:
两种解决方案都很棒,我选择以下解决方案是因为我要求 bash 脚本,并且它不依赖额外的软件。
然而,在我自己的机器上,我使用 aunpack 因为它可以处理很多很多格式。
我将它与 shell 脚本一起使用,该脚本可以一次下载并解压所有内容。这是我正在使用的:
#!/bin/bash
wget -o temp.log --content-disposition $1
old=IFS
IFS='
'
r=`cat temp.log`
rm temp.log
for line in $r; do
substring=$(expr "$line" : 'Saving to: `\(.*\)'\')
if [ "$substring" != "" ]
then
aunpack $substring
rm $substring
IFS=$old
exit
fi
done
IFS=$old
Most tar files extract into their own subfolder (because the people that write open source utilities are amazing people).
Some extract into my cwd, which clutters everything up. I know there's a way to see what's in the tar, but I want to write a bash script that essentially guarantees I won't end up with 15 files extracted into my home folder.
Any pointers?
pseudo code:
if [listing of tar files] has any file that doesn't have a '/' in it:
mkdir [tar filename without extension]
tar xzvf [tar filename] into [the new folder]
else:
tar xzvf [tar filename] into cwd
EDIT:
Both solutions are great, I chose the below solution because I was asking for a bash script, and it doesn't rely on extra software.
However, on my own machine, I am using aunpack because it can handle many, many more formats.
I am using it with a shell script that downloads and unpacks all at once. Here is what I am using:
#!/bin/bash
wget -o temp.log --content-disposition $1
old=IFS
IFS='
'
r=`cat temp.log`
rm temp.log
for line in $r; do
substring=$(expr "$line" : 'Saving to: `\(.*\)'\')
if [ "$substring" != "" ]
then
aunpack $substring
rm $substring
IFS=$old
exit
fi
done
IFS=$old
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
atool 包中的
aunpack
命令执行以下操作:The
aunpack
command from the atool package does that:您可以使用
tar
选项的组合来实现此目的:用于列出的
tar
选项是:tar
提取到不同目录的选项是:因此,在您的脚本中,您可以列出文件和文件。检查列表中是否有任何文件没有“/”,并根据该输出,您可以使用适当的选项调用
tar
。供您参考的示例如下:
在某些情况下,tar 文件可能包含不同的目录。如果您发现查找由同一 tar 文件提取的不同目录有点烦人,那么可以修改脚本来创建新目录,即使列表包含不同的目录。稍微高级的示例如下:
希望这会有所帮助!
You can use combination of
tar
options to achieve this:tar
option for listing is:tar
option to extract into different directory is:So in your script you can list the files & check if there are any files in the listing which do not have "/" and based on that output you can call
tar
with appropriate options.Sample for your reference is as follows:
In some cases the tar file may contain different directories. If you find it a little annoying to look for different directories which are extracted by the same tar file then the script can be modified to create a new directory even if the listing contains different directories. The slightly advanced sample is as follows:
Hope this helps!