将rar文件解压缩到相同的文件夹名称

时间:2017-08-18 05:40:28

标签: shell unix unrar

我在特定目录中有许多.rar个文件夹。我想在同一目录中提取每个rar文件夹的内容,并且rar文件夹的所有解压缩文件都应放在新文件夹中,其名称与rar文件夹的名称相同名。

例如,如果有两个rar文件:one.rartwo.rar,则脚本应创建两个同名文件夹:one和{{1 }}。名为two的文件夹应包含从one中提取的文件,名为one.rar的文件夹应包含从two中提取的文件。

命令: unrar e $ filename 提取rar文件的所有内容,但不创建目标文件夹。

如果我使用two.rar,那么由于可能有许多unrar e $filename $DESTINATION_PATH个文件,因此在目标路径中手动创建文件夹名称将花费大量时间。如何使用shell脚本实现此目的?

到目前为止,我只写了这些内容:

rar

我不知道如何创建与loc="/home/Desktop/code"` # this directory path contains all the rar files to be extracted <br/> for file in "$loc"/* do unrar e $file done 名称相同的文件夹名称,并在新创建的同名文件夹中提取该rar的所有文件。

任何帮助将不胜感激。在此先感谢!!

1 个答案:

答案 0 :(得分:0)

您可以使用sed从档案中删除文件扩展名。查看以下脚本,将destination设置为相应的名称。

#!/bin/sh

for archive in "$(find $loc -name '*.rar')"; do
  destination="$( echo $archive | sed -e 's/.rar//')"
  if [ ! -d "$destination" ] ; then mkdir "$destination"; fi
  unrar e "$archive" "$destination"
done

如果您正在使用bash,那么您只需使用

即可
#!/bin/bash

for archive in "$(find $loc -name '*.rar')"; do
  destination="${archive%.rar}"
  if [ ! -d "$destination" ] ; then mkdir "$destination"; fi
  unrar e "$archive" "$destination"
done
相关问题