This command is a neat way to automate extracting multiple zip files into directories named after the files themselves. It avoids the need to unzip files manually one by one, which is particularly useful in large directories or complex file structures.
find . -iname '*.zip' -exec sh -c 'unzip "{}" -d "${1%.*}"' _ {} \;
Explanation:
find .- The
findcommand searches through directories recursively, starting from the current directory (.). It looks for files that match specific criteria.
- The
-iname '*.zip'- This option tells
findto look for files with names ending in.zip. The-inameflag makes the search case-insensitive, meaning it will match.zip,.ZIP, or any other case combination.
- This option tells
-exec- The
-execoption allows you to run a command on each file thatfindlocates. In this case, it runs a shell script to unzip the files.
- The
sh -c 'unzip "{}" -d "${1%.*}"' _ {}sh -cruns the specified shell command. Here’s what this part does:'unzip "{}" -d "${1%.*}"': This is the actual unzipping command.unzipextracts the contents of the zip file ("{}"refers to the zip file found byfind).-d "${1%.*}": This specifies the target directory where the file should be extracted. The syntax${1%.*}removes the.zipextension from the filename to use the base name of the zip file as the folder name. For example, if the zip file isexample.zip, it will create a folderexampleto extract the contents into.- The first underscore (
_) is a placeholder used bysh -cto refer to the next argument ({}), which is the zip file path thatfindpasses to the command.
{} \;{}represents each file found byfind, and\;terminates the-execcommand.
Example:
Suppose you have the following structure:
/home/user
├── folder1
│ └── file1.zip
├── folder2
│ └── file2.ZIP
Running the command will:
- Find
file1.zipandfile2.ZIP. - Extract the contents of
file1.zipinto a folder namedfile1andfile2.ZIPinto a folder namedfile2.