I have a 7z archive file which, at the "root" level, contains a couple of files and then a directory, which in turn contains both files and folders, like this:
- file1.txt
- file2.txt
- my_dir
- file3.txt
- file4.txt
- another_dir
- file5.txt
- file6.txt
I would like to know if there is a single command that allows me to extract the content of my_dir
inside a directory of my choice so that the end result is:
- target_dir
- file3.txt
- file4.txt
- another_dir
- file5.txt
- file6.txt
I have tried these commands:
7za x -y archive.7z -o/path/to/target_dir my_dir
7za x -y archive.7z -o/path/to/target_dir 'my_dir/*'
but both created this directory structure:
- target_dir
- my_dir
- file3.txt
- file4.txt
- another_dir
- file5.txt
- file6.txt
Best Answer
Is there a single command that allows extracting my_dir to a specified directory?
Yes. Use the
e
option instead ofx
:(
x
is Extract with full paths)Source e (Extract) command
But in fact the folder in the archive contains subfolders which I'd like to preserve
In this case you need to use the original command (with
x
), and then usemove
to move the contents ofmy_dir
up a level.Something like the following batch file (not tested):
From the command line:
But I'm using Linux!
Then the commands to use are:
Or:
Which can probably be simplified by someone who knows
bash
better than I do.Further Reading