4

Maybe this is the pinnacle of first world problems, but after years of angst I just have to air this.

Let's say you have downloaded funtool.zip in your ~/Downloads folder. You have two choices, each with two possible outcomes, depending on whether the archive has a proper root folder.

unzip funtool. Possible outcomes:

  • ~/Downloads/funtool/(files) -- Good
  • ~/Downloads/(lots of files) -- AUGH! Download folder littered

unzip funtool -d funtool. Possible outcomes:

  • ~/Downloads/funtool/funtool/(files) -- Double folders. Annoying!
  • ~/Downloads/funtool/(files) -- Good

I've seen some GUI decompressors have built-in intelligence for this, but I'm a CLI guy. My solution is to check for a root folder by unzip -t funtool and act accordingly. But after years, I've had it.

Is there a script or something that does this automatically, even for rar files, and possibly 7z? At least this is one thing that tar seems to get right every time, maybe because of good conventions.

1

As a CLI frontend to various archive formats, there is unp, unpack (almost) everything with one command.

And it claims to have these options:

   -u Special helper mode.
      For most archive types:
      - create directory <filename without suffix>/
      - extract contents there
      For Debian/Ubuntu packages:
      - extract data.tar.gz after each operation in local directory
      - extract control.tar.gz into control/<package_version_arch>/
   -U Smart mode, acts like -u (see above) if archive contains multiple
      elements but if there is only one file/directory element then it's stored 
      in the current directory.

And that sounds like it's close to the behavior you are looking for.

However the -U mode still litters a single file if it's a ZIP with a single file that is not related in any way whatsoever to the name of the zipfile itself.

  • Yes! The -U switch seems to do it right. This goes to show once more there's always at least one more person on the planet who shares your hangups. – forthrin Apr 11 '18 at 17:42
0

7z can do this, at least if the root directory in the archive (if any) matches the name of the directory you’re extracting to:

7z x -spe funtool.zip -ofuntool

If funtool.zip’s contents are all inside a funtool directory in the archive, 7z won’t duplicate the directory below funtool, giving you the result you’re after.

  • That brings you back to having to list the archive to get that name. It should be smarter about it: If there is a root folder, use that. If not, use the archive base name. And it should do it by default, so you can just do 7z x funtool.zip. Well, there's always aliases... – forthrin Apr 11 '18 at 17:38
0

There's the command line tool dtrx, which is able to extract any sort of compressed file. By default, everything will be written to a dedicated directory that's named after the archive.

Simply run

dtrx <archive_name>

If you have a Debian-based distribution you can find it in the APT repository.

  • Does it create the directory even if the archive has its own root directory? – forthrin Apr 11 '18 at 17:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.