0

.rar在特定目录中有很多文件夹。我想提取rar同一目录中每个文件夹的内容,文件夹的所有提取文件都rar应该放在与文件夹名称相同的新文件夹中rar

例如,如果有两个rar文件:one.rartwo.rar,那么脚本应该创建两个同名文件夹:onetwo。一个名为的文件夹one应该包含从中提取的文件,one.rar而名为的文件夹two应该包含从two.rar.

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

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

到目前为止,我只写了以下几行:

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

我不知道如何创建与 name 相同的文件夹名称rar并将其所有文件提取rar到新创建的同名文件夹中。

任何帮助将不胜感激。提前致谢 !!

4

1 回答 1

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
于 2017-08-18T06:04:44.417 回答