Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

1 Answer 1

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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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