압축된 패키지와 동일한 이름의 폴더에 압축을 풉니다.

압축된 패키지와 동일한 이름의 폴더에 압축을 풉니다.

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"

관련 정보