Search code examples
bashubuntuzipunzip

Extract only files with specific extension via bash


For a first time I need to create an .sh for do something. My aim is to unzip a lot of zip folders, so I've wrote the script below:

for zipfiles in /downloads/*.zip; do unzip $zipfiles; done

I can unzip all but I noticed that there are some files with the same name and typing y I can ultimate the process.

There is a way to extract only files with a specific extension, like .docx, instead of the entire zip folder? I'm absolutely sure that there aren't .docx with the same name.


Solution

  • You can specify a pattern:

    for zipfiles in /downloads/*.zip; do unzip "$zipfiles" '*.docx'; done
    

    Tested to work with UnZip 6.00.

    You can also specify the -x option to exclude.