大多数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
Run Code Online (Sandbox Code Playgroud)
编辑:
两种解决方案都很棒,我之所以选择以下解决方案是因为我要求使用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
Run Code Online (Sandbox Code Playgroud)