Unrar to folder with same name as archive

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
4
down vote

favorite












I have a lot of rar files



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


All the files contains no root folder so it's just files.



What I want to achieve when extracting is this structure:



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


So that a folder with the name of the archive is created and the archive is extracted to it.



I found this bash example in another thread but it's not working for me, it's trying to create one folder with all the files as name.



#!/bin/bash

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


Any ideas how I can do this?










share|improve this question









New contributor




Dennis Wiencken is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.























    up vote
    4
    down vote

    favorite












    I have a lot of rar files



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


    All the files contains no root folder so it's just files.



    What I want to achieve when extracting is this structure:



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


    So that a folder with the name of the archive is created and the archive is extracted to it.



    I found this bash example in another thread but it's not working for me, it's trying to create one folder with all the files as name.



    #!/bin/bash

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


    Any ideas how I can do this?










    share|improve this question









    New contributor




    Dennis Wiencken is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.





















      up vote
      4
      down vote

      favorite









      up vote
      4
      down vote

      favorite











      I have a lot of rar files



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


      All the files contains no root folder so it's just files.



      What I want to achieve when extracting is this structure:



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


      So that a folder with the name of the archive is created and the archive is extracted to it.



      I found this bash example in another thread but it's not working for me, it's trying to create one folder with all the files as name.



      #!/bin/bash

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


      Any ideas how I can do this?










      share|improve this question









      New contributor




      Dennis Wiencken is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      I have a lot of rar files



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


      All the files contains no root folder so it's just files.



      What I want to achieve when extracting is this structure:



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


      So that a folder with the name of the archive is created and the archive is extracted to it.



      I found this bash example in another thread but it's not working for me, it's trying to create one folder with all the files as name.



      #!/bin/bash

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


      Any ideas how I can do this?







      shell-script archive rar






      share|improve this question









      New contributor




      Dennis Wiencken is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question









      New contributor




      Dennis Wiencken is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question








      edited 2 hours ago









      Gilles

      515k12210241553




      515k12210241553






      New contributor




      Dennis Wiencken is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 3 hours ago









      Dennis Wiencken

      235




      235




      New contributor




      Dennis Wiencken is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      Dennis Wiencken is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      Dennis Wiencken is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          3
          down vote



          accepted










          I have a script in my personal archive that does exactly this. More precisely, it extracts e.g. Spain.rar to a new directory called Spain, except that if all the files in Spain.rar are already under the same top-level directory, then this top-level directory is kept.



          #!/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 () *.tbz2) program="tar -xf";;
          *.tar.gz

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


          Usage (after installing this script in your $PATH under the name extract and making it executable):



          extract Folder/*.rar





          share|improve this answer






















          • This looks pretty awesome and I think this is what I need. But I am having trouble getting it to work. There seems to be something wrong with the way unrar is being called. When I run it, I get the usage example. It creates a lot of tmp folders but the extractions fails. What version of unrar did you use this on?
            – Dennis Wiencken
            1 hour ago










          • @DennisWiencken Evidently I'd never tried this version with anything but zip. Try my fixed version.
            – Gilles
            1 hour ago










          • Works like a charm now! Thank you for the help!
            – Dennis Wiencken
            28 mins ago










          Your Answer







          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "106"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: false,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );






          Dennis Wiencken is a new contributor. Be nice, and check out our Code of Conduct.









           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f476522%2funrar-to-folder-with-same-name-as-archive%23new-answer', 'question_page');

          );

          Post as a guest






























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          3
          down vote



          accepted










          I have a script in my personal archive that does exactly this. More precisely, it extracts e.g. Spain.rar to a new directory called Spain, except that if all the files in Spain.rar are already under the same top-level directory, then this top-level directory is kept.



          #!/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 () *.tbz2) program="tar -xf";;
          *.tar.gz

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


          Usage (after installing this script in your $PATH under the name extract and making it executable):



          extract Folder/*.rar





          share|improve this answer






















          • This looks pretty awesome and I think this is what I need. But I am having trouble getting it to work. There seems to be something wrong with the way unrar is being called. When I run it, I get the usage example. It creates a lot of tmp folders but the extractions fails. What version of unrar did you use this on?
            – Dennis Wiencken
            1 hour ago










          • @DennisWiencken Evidently I'd never tried this version with anything but zip. Try my fixed version.
            – Gilles
            1 hour ago










          • Works like a charm now! Thank you for the help!
            – Dennis Wiencken
            28 mins ago














          up vote
          3
          down vote



          accepted










          I have a script in my personal archive that does exactly this. More precisely, it extracts e.g. Spain.rar to a new directory called Spain, except that if all the files in Spain.rar are already under the same top-level directory, then this top-level directory is kept.



          #!/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 () *.tbz2) program="tar -xf";;
          *.tar.gz

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


          Usage (after installing this script in your $PATH under the name extract and making it executable):



          extract Folder/*.rar





          share|improve this answer






















          • This looks pretty awesome and I think this is what I need. But I am having trouble getting it to work. There seems to be something wrong with the way unrar is being called. When I run it, I get the usage example. It creates a lot of tmp folders but the extractions fails. What version of unrar did you use this on?
            – Dennis Wiencken
            1 hour ago










          • @DennisWiencken Evidently I'd never tried this version with anything but zip. Try my fixed version.
            – Gilles
            1 hour ago










          • Works like a charm now! Thank you for the help!
            – Dennis Wiencken
            28 mins ago












          up vote
          3
          down vote



          accepted







          up vote
          3
          down vote



          accepted






          I have a script in my personal archive that does exactly this. More precisely, it extracts e.g. Spain.rar to a new directory called Spain, except that if all the files in Spain.rar are already under the same top-level directory, then this top-level directory is kept.



          #!/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 () *.tbz2) program="tar -xf";;
          *.tar.gz

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


          Usage (after installing this script in your $PATH under the name extract and making it executable):



          extract Folder/*.rar





          share|improve this answer














          I have a script in my personal archive that does exactly this. More precisely, it extracts e.g. Spain.rar to a new directory called Spain, except that if all the files in Spain.rar are already under the same top-level directory, then this top-level directory is kept.



          #!/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 () *.tbz2) program="tar -xf";;
          *.tar.gz

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


          Usage (after installing this script in your $PATH under the name extract and making it executable):



          extract Folder/*.rar






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 1 hour ago

























          answered 2 hours ago









          Gilles

          515k12210241553




          515k12210241553











          • This looks pretty awesome and I think this is what I need. But I am having trouble getting it to work. There seems to be something wrong with the way unrar is being called. When I run it, I get the usage example. It creates a lot of tmp folders but the extractions fails. What version of unrar did you use this on?
            – Dennis Wiencken
            1 hour ago










          • @DennisWiencken Evidently I'd never tried this version with anything but zip. Try my fixed version.
            – Gilles
            1 hour ago










          • Works like a charm now! Thank you for the help!
            – Dennis Wiencken
            28 mins ago
















          • This looks pretty awesome and I think this is what I need. But I am having trouble getting it to work. There seems to be something wrong with the way unrar is being called. When I run it, I get the usage example. It creates a lot of tmp folders but the extractions fails. What version of unrar did you use this on?
            – Dennis Wiencken
            1 hour ago










          • @DennisWiencken Evidently I'd never tried this version with anything but zip. Try my fixed version.
            – Gilles
            1 hour ago










          • Works like a charm now! Thank you for the help!
            – Dennis Wiencken
            28 mins ago















          This looks pretty awesome and I think this is what I need. But I am having trouble getting it to work. There seems to be something wrong with the way unrar is being called. When I run it, I get the usage example. It creates a lot of tmp folders but the extractions fails. What version of unrar did you use this on?
          – Dennis Wiencken
          1 hour ago




          This looks pretty awesome and I think this is what I need. But I am having trouble getting it to work. There seems to be something wrong with the way unrar is being called. When I run it, I get the usage example. It creates a lot of tmp folders but the extractions fails. What version of unrar did you use this on?
          – Dennis Wiencken
          1 hour ago












          @DennisWiencken Evidently I'd never tried this version with anything but zip. Try my fixed version.
          – Gilles
          1 hour ago




          @DennisWiencken Evidently I'd never tried this version with anything but zip. Try my fixed version.
          – Gilles
          1 hour ago












          Works like a charm now! Thank you for the help!
          – Dennis Wiencken
          28 mins ago




          Works like a charm now! Thank you for the help!
          – Dennis Wiencken
          28 mins ago










          Dennis Wiencken is a new contributor. Be nice, and check out our Code of Conduct.









           

          draft saved


          draft discarded


















          Dennis Wiencken is a new contributor. Be nice, and check out our Code of Conduct.












          Dennis Wiencken is a new contributor. Be nice, and check out our Code of Conduct.











          Dennis Wiencken is a new contributor. Be nice, and check out our Code of Conduct.













           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f476522%2funrar-to-folder-with-same-name-as-archive%23new-answer', 'question_page');

          );

          Post as a guest













































































          Comments

          Popular posts from this blog

          Long meetings (6-7 hours a day): Being “babysat” by supervisor

          Is the Concept of Multiple Fantasy Races Scientifically Flawed? [closed]

          Confectionery