解压到与压缩包同名的文件夹

解压到与压缩包同名的文件夹

我有很多rar文件

- Folder/
--- Spain.rar
--- Germany.rar
--- Italy.rar

所有文件都不包含根文件夹,因此只是文件。

提取时我想要实现的是这样的结构:

- Folder/
-- Spain/
---- Spain_file1.txt
---- Spain_file2.txt
-- Germany/
---- Germany_file1.txt
---- Germany_file2.txt
-- Italy/
---- Italy_file1.txt
---- Italy_file2.txt

这样就创建了一个名为存档名称的文件夹,并将存档提取到其中。

我在另一个线程中找到了这个 bash 示例,但它对我不起作用,它试图创建一个以所有文件为名称的文件夹。

#!/bin/bash

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

我有什么想法可以做到这一点吗?

答案1

我的个人档案中有一个脚本可以做到这一点。更准确地说,它将例如提取Spain.rar到一个名为 的新目录Spain,但如果 中的所有文件Spain.rar已经位于同一顶级目录下,则保留该顶级目录。

#!/bin/sh

# Extract the archive $1 to a directory $2 with the program $3. If the
# archive contains a single top-level directory, that directory
# becomes $2. Otherwise $2 contains all the files at the root of the
# archive.
extract () (
  set -e
  archive=$1
  case "$archive" in
    -) :;; # read from stdin
    /*) :;; # already an absolute path
    *) archive=$PWD/$archive;; # make absolute path
  esac
  target=$2
  program=$3
  if [ -e "$target" ]; then
    echo >&2 "Target $target already exists, aborting."
    return 3
  fi
  case "$target" in
    /*) parent=${target%/*};;
    */[!/]*) parent=$PWD/${target%/*};;
    *) parent=$PWD;;
  esac
  temp=$(TMPDIR="$parent" mktemp -d)
  (cd "$temp" && $program "$archive")
  root=
  for member in "$temp/"* "$temp/".*; do
    case "$member" in */.|*/..) continue;; esac
    if [ -n "$root" ] || ! [ -d "$member" ]; then
      root=$temp # There are multiple files or there is a non-directory
      break
    fi
    root="$member"
  done
  if [ -z "$root" ]; then
    # Empty archive
    root=$temp
  fi
  mv -v -- "$root" "$target"
  if [ "$root" != "$temp" ]; then
    rmdir "$temp"
  fi
)

# Extract the archive $1.
process () {
  dir=${1%.*}
  case "$1" in
    *.rar|*.RAR) program="unrar x";;
    *.tar|*.tgz|*.tbz2) program="tar -xf";;
    *.tar.gz|*.tar.bz2|*.tar.xz) program="tar -xf"; dir=${dir%.*};;
    *.zip|*.ZIP) program="unzip";;
    *) echo >&2 "$0: $1: unsupported archive type"; exit 4;;
  esac
  if [ -d "$dir" ]; then
    echo >&2 "$0: $dir: directory already exists"
    exit 1
  fi
  extract "$1" "$dir" "$program"
}

for x in "$@"; do
  process "$x"
done

$PATH用法(在您的名称下安装此脚本extract并使其可执行后):

extract Folder/*.rar

答案2

您可以使用unar代替unrar.当文件夹中存在多个rar文件时,会默认创建一个同名的新目录。

如果只有一个文件,则可以使用该-d选项。

https://packages.ubuntu.com/search?keywords=unar

答案3

#!/bin/bash

# https://ostechnix.com/a-bash-function-to-extract-file-archives-of-various-types/
directory=`/usr/bin/dirname $1`
filename=`/usr/bin/basename $1`
filenameWithExt=$(/usr/bin/basename "$1")
filenameWithoutExt="${filenameWithExt%.*}"

# echo "directory: $directory"
# echo "filename: $filename"
# echo "filenameWithoutExt: $filenameWithoutExt"
# Bash Function To Extract File Archives Of Various Types
extract () {
    /usr/bin/mkdir "$directory/$filenameWithoutExt"
     if [ -f $1 ] ; then
         case $1 in
             *.tar.bz2)   /usr/bin/tar xjf "$1" -C "$directory/$filenameWithoutExt"     ;;
             *.tar.gz)    /usr/bin/tar xzf "$1" -C "$directory/$filenameWithoutExt"     ;;
             *.bz2)       /usr/bin/bunzip2 "$1" -C "$directory/$filenameWithoutExt"     ;;
             *.rar)       /usr/bin/rar x "$1" "$directory/$filenameWithoutExt"      ;;
             *.gz)        /usr/bin/gunzip "$1"      ;;
             *.tar)       /usr/bin/tar xf "$1" -C "$directory/$filenameWithoutExt"      ;;
             *.tbz2)      /usr/bin/tar xjf "$1" -C "$directory/$filenameWithoutExt"     ;;
             *.tgz)       /usr/bin/tar xzf "$1" -C "$directory/$filenameWithoutExt"    ;;
             *.zip)       /usr/bin/unzip "$1" -d "$directory/$filenameWithoutExt"       ;;
             *.Z)         /usr/bin/uncompress "$1"  ;;
             *.7z)        eval "7z e -o$directory/$filenameWithoutExt/ $1"  ;;
             *)           /usr/bin/echo "'$1' cannot be extracted via extract()" ;;
         esac
     else
         echo "'$1' is not a valid file"
     fi
}

extract "$1"

相关内容