5

I have a directory with a lot of .7z files, I need to repack each of them as zip archive, how can I do this automatically on linux?

2 Answers 2

7

Use the following script and run it from the directory where your .7z files are:

#!/bin/bash

TMPDIR=tempdir_$$

for x in `ls *.7z`; do
    mkdir $TMPDIR
    cd $TMPDIR
    cp ../$x .
    p7zip -d $x
    zip -r ../${x%.7z}.zip *
    cd ..
    rm -rf $TMPDIR    
done

This will leave your .7z files where they are and create .zip files with the same name.

The script copies the .7z files into the temporary directory before extracting them because they normally are deleted after decompressing the files.

I've kept the script as simple as possible so you can easily figure out how it works.

The script will only work with .7z files that have no spaces or other special characters in their name.

2
  • You don't need for x in `ls *.7z`;. You can simply do: for x in *.7z;. Bonus: the second version will correctly handle filenames with spaces, while the first one wont. Jan 3, 2022 at 0:05
  • Speaking of whitespace, the variables should be wrapped by double quotes.
    – Loupax
    Apr 30, 2023 at 18:19
10

You could also simply use the arepack tool that's part of the atool suite of tools. It's typically a yum install atool or apt install atool. You may also need to install the p7zip-full and p7zip-rar packages if they are not already installed. Once these are available you can simply do this:

$ arepack -e -F zip *.7z

This will convert all the .7z files as .zip files. You'll still need to delete the *.7z files, but that can be done simply like so:

$ rm -f *.7z

Additional arepack options

Options:
  -e, --each               execute command above for each file specified
  -F, --format=EXT         override archive format (see below)
  -O, --format-option=OPT  give specific options to the archiver
  -D, --subdir             always create subdirectory when extracting
  -f, --force              allow overwriting of local files
  -q, --quiet              decrease verbosity level by one
  -v, --verbose            increase verbosity level by one
  -V, --verbosity=LEVEL    specify verbosity (0, 1 or 2)
  -p, --page               send output through pager
  -0, --null               filenames from standard in are null-byte separated
  -E, --explain            explain what is being done by atool
  -S, --simulate           simulation mode - no filesystem changes are made
  -o, --option=KEY=VALUE   override a configuration option
      --config=FILE        load configuration defaults from file

Archive format (for --format) may be specified either as a
file extension ("tar.gz") or as "tar+gzip".

You must log in to answer this question.

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