Quick: Unzip multiple files with spaces in their filenames using command line.

Quick: Unzip multiple files with spaces in their filenames using command line.

You might have run into a similar situation; You need to unzip several ZIP files, but running unzip *.zip command results in errors shown about files not being found. Joy.

Fortunately there's way to avoid this. Unfortunately, it's not something you can easily memorize. Or, I certainly cannot. Credit is to Joakim Nohlgård, who posted this in an answer to a question about this question on Superuser.com.

find . -name "*.zip" -exec sh -c 'unzip -n "{}" | head -n 7' \; 
Note; I added -n to the unzip command, which means it will never overwrite files that already exist.

If you'd like to extract all files to a specific folder, you can add -d to the unzip command and provide the path to which it should extract things to, for example:

find . -name "*.zip" -exec sh -c 'unzip -n "{}" -d /path/to/extract/to | head -n 7' \; 

You can modify the command to your liking of course. If, for example, your archives are in 7zip or RAR format, you could replace the unzip command with 7z (7za on Debian-based systems, see footnotes). Here's an example that includes specifying what path to extract to:

find . -name "*.rar" -exec sh -c '7z x "{}" -o/path/to/extract/to | head -n 7' \;

Creating multiple archives

I have had situations where I needed to create individual archives of each folder inside a certain directory. If you ever need to do something similar, here is an example using 7zip to do exactly that:

find ./* -maxdepth 1 -type d -print0 | while read -d $'\0' file; do 7z a -mmt2 -saa $file "$file/"; done

If you are running a Debian-based Linux distribution (ie. Ubuntu), you can easily install whichever tool you need to use. For ZIP files you could rely on unzip as the examples above show, or you could install p7zip for compatibility with more archive types (ie. 7z, RAR, etc.). On macOS unzip is already pre-installed, and you can install 7z using Brew.