Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is there a way to extract a 7z archive that contains all it's files within a directory without creating this directory in the file system but still keeping the sub-directory structure of the archive (so 7z e ... is not working as desired, because it strips all path info)?

In tar I would use tar -x --strip=1 ... to strip the first path component. Is there some equivalent for 7z or do I have to extract with the unwanted path segment, move the contents to the right place and delete the directory?

share|improve this question
1  
Looking at 7z command line options, I don't it's possible in one shot. Probably use 7z x to create the structure, 7z e to strip path info and then delete the files created by 7z x, tried it, it's still not what you want. – Sathya May 7 '12 at 4:17

No, --strip-components is not supported in 7-zip. But you could use a shell script to do that:

#!/bin/sh

if [ $# -eq 1 ]; then
    inputfile="$1"
    outputfolder="."
elif [ $# -eq 2 ]; then
    inputfile="$1"
    outputfolder="$2"
    if [ ! -d "$outputfolder" ]; then
        mkdir "$outputfolder"
    fi
else
    exit
fi
7z x "$inputfile" -otempdir
folder=$(find tempdir -mindepth 1 -maxdepth 1 -type d | head -1)
find "$folder" -mindepth 1 -maxdepth 1 -exec mv -t"$outputfolder" -- {} +
rm -rf tempdir

Usage:

7zstrip.sh file.7z [folder]

Note: not tested.

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .