仅解压缩特定扩展

仅解压缩特定扩展

本文介绍了仅解压缩特定扩展的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含 .jpg、.png、.gif 图像的 zip 存档目录.我想解压缩每个存档,只获取图像并将它们放在一个带有存档名称的文件夹中.

I have a a directory with zip archives containing .jpg, .png, .gif images. I want to unzip each archive taking the images only and putting them in a folder with the name of the archive.

所以:

files/archive1.zip
files/archive2.zip
files/archive3.zip
files/archive4.zip

打开 archive1.zip - 获取 sunflower.jpg、rose_sun.gif.创建一个文件夹 files/archive1/并将图像添加到该文件夹​​中,即 files/archive1/folder1.jpg、files/archive1/rose_sun.gif.对每个存档执行此操作.

Open archive1.zip - take sunflower.jpg, rose_sun.gif. Make a folder files/archive1/ and add the images to that folder, so files/archive1/folder1.jpg, files/archive1/rose_sun.gif. Do this to each archive.

我真的不知道如何做到这一点,欢迎提出所有建议.我有 600 多个档案,自动解决方案将是救星,最好是 linux 解决方案.

I really don't know how this can be done, all suggestions are welcome. I have over 600 archives and an automatic solution would be a lifesaver, preferably a linux solution.

推荐答案

大致如下:

#!/bin/bash
cd ~/basedir/files
for file in *.zip ; do
    newfile=$(echo "${file}" | sed -e 's/^files.//' -e 's/.zip$//')
    echo ":${newfile}:"
    mkdir tmp
    rm -rf "${newfile}"
    mkdir "${newfile}"
    cp "${newfile}.zip" tmp
    cd tmp
    unzip "${newfile}.zip"
    find . -name '*.jpg' -exec cp {} "../${newfile}" ';'
    find . -name '*.gif' -exec cp {} "../${newfile}" ';'
    cd ..
    rm -rf tmp
done

这是经过测试的,将处理文件名中的空格(zip 文件和提取的文件).如果 zip 文件在不同目录中具有相同的文件名,则可能会发生冲突(如果您要扁平化目录结构,则无法避免这种情况).

This is tested and will handle spaces in filenames (both the zip files and the extracted files). You may have collisions if the zip file has the same file name in different directories (you can't avoid this if you're going to flatten the directory structure).

这篇关于仅解压缩特定扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 13:52