我有一个包含很多 .7z 文件的目录,我需要将每个文件重新打包为 zip 存档,如何在 linux 上自动执行此操作?
您也可以简单地使用arepack属于 atool 工具套件的工具。它通常是yum install atool或apt install atool。如果尚未安装p7zip-full和p7zip-rar软件包,您可能还需要安装它们。一旦这些可用,您可以简单地执行以下操作:
$ arepack -e -F zip *.7z
Run Code Online (Sandbox Code Playgroud)
这会将所有 .7z 文件转换为 .zip 文件。您仍然需要删除 *.7z 文件,但这可以像这样简单地完成:
$ rm -f *.7z
Run Code Online (Sandbox Code Playgroud)
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".
Run Code Online (Sandbox Code Playgroud)
使用以下脚本并从文件所在的目录运行它.7z:
#!/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
Run Code Online (Sandbox Code Playgroud)
这将使您的.7z文件.zip保持原样并创建具有相同名称的文件。
该脚本.7z在解压缩文件之前将文件复制到临时目录中,因为它们通常在解压缩文件后被删除。
我已经使脚本尽可能简单,以便您可以轻松弄清楚它是如何工作的。
该脚本仅适用.7z于名称中没有空格或其他特殊字符的文件。