10

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
  • Does this work : 7z x archive.7z my_dir -y -r -otarget_dir ? – harrymc Oct 14 '18 at 10:05
  • Sadly no, same result as the commands I tried. – Matteo Tassinari Oct 14 '18 at 10:06
  • Then extract it and then move. – harrymc Oct 14 '18 at 18:04
12

Is there a single command that allows extracting my_dir to a specified directory?

Yes. Use the e option instead of x:

7za e -y archive.7z -o/path/to/target_dir my_dir

(x is Extract with full paths)

e (Extract) command

Extracts files from an archive to the current directory or to the output directory. The output directory can be specified by -o (Set Output Directory) switch.

This command copies all extracted files to one directory. If you want extract files with full paths, you must use x (Extract with full paths) command.

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 use move to move the contents of my_dir up a level.

Something like the following batch file (not tested):

@echo off
7za x -y archive.7z -o/path/to/target_dir my_dir
move /y my_dir\* /path/to/target_dir
rd /s my_dir
endlocal

From the command line:

7za x -y archive.7z -o/path/to/target_dir my_dir && move /y my_dir\* /path/to/target_dir && rd /s my_dir

But I'm using Linux!

Then the commands to use are:

#!/bin/bash
7za x -y archive.7z -o/path/to/target_dir my_dir
mv -f my_dir/* /path/to/target_dir
rmdir my_dir

Or:

7za x -y archive.7z -o/path/to/target_dir my_dir && mv -f my_dir/* /path/to/target_dir && rmdir my_dir

Which can probably be simplified by someone who knows bash better than I do.


Further Reading

  • Hi David, thanks for your help, but I fear I may have over-simplified my question, in fact the folder in the archive contains subfolders which I'd like to preserve, see the edit. – Matteo Tassinari Oct 14 '18 at 9:03
  • @MatteoTassinari Then you will have to use the original x option and then use move to move the contents of my_dir up a level. – DavidPostill Oct 14 '18 at 9:14
  • That's what I feared, I really hoped it could be done in a single command – Matteo Tassinari Oct 14 '18 at 9:24
  • @MatteoTassinari Answer updated with a suggested batch file. – DavidPostill Oct 14 '18 at 10:28
  • Thanks for the script, however my OS is CentOS, not Windows, that's why I put the "linux" tag on the question. I'll have no problem converting that to its linux equivalent, though. – Matteo Tassinari Oct 14 '18 at 10:32

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.