1

I have a folder with over 200 .zip files. In each of these files there is only one folder with several types of archives in it (sometimes, this folder is missing). Among them, I want to extract only the .jpg or .jpeg file. The estructure of the zip file is:

AAAA-99999-2015.zip
     AAAA-99999-2015 (optional)
          AAAA-99999-2015.jpg (or .jpeg)
          AAAA-99999-2015.prj
          AAAA-99999-2015.ecw
          readme.txt

¿How can I automate this task without manually have to open all these 200 archives?

1 Answer 1

2

You could do something like this

find . -type f -iname \*zip -print0 | xargs -0 --max-args=1 --replace unzip {} \*.jpg \*.jpeg

This would look in the current folder and sub-folders for files called "*.zip", then for each one of them, individually (--max-args=1) the command unzip is called, appending \*.jpg \*.jpeg, thereby selecting the files to extract.

(If we select both *.jpeg and *.jpg files and some of these do not exist unzip may warn us about that. This can be safely ignored.)

If you wish to test what happens first, insert an echo:

find . -type f -iname \*zip -print0 | xargs -0 --max-args=1 --replace echo unzip {} \*.jpg \*.jpeg

If you with to extract all files in the current folder, irrespective of where they are inside the ZIP file, add the -j option to unzip:

find . -type f -iname \*zip -print0 | xargs -0 --max-args=1 --replace unzip -j {} \*.jpg \*.jpeg

You can also add a target folder by using -d foldername after the -j.

2
  • Thanks. One minor problem with this solution is that I do not get the .jpg or .jpeg file, but a folder with the wanted files if in the .zip those files were inside a folder. I know how to collect then all the image files, but there is a way of doing this in the previous command?
    – fich
    Commented Nov 18, 2020 at 12:15
  • 1
    @fich OK, I updated my answer accordingly.
    – Ned64
    Commented Nov 18, 2020 at 16:56

You must log in to answer this question.

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