Working with Compressed Directories and Folders
In addition to extracting individual files, the unzip
command in Linux also allows you to work with compressed directories and folders within a ZIP archive. This functionality is particularly useful when dealing with complex file structures or when you need to preserve the original directory hierarchy.
To extract a specific directory from a ZIP archive, you can use the following command:
unzip <zip_file> <directory_name>/*
This will extract the contents of the specified directory, including any subdirectories and files, to the current working directory.
For example, to extract the contents of a directory named "documents" from a ZIP file called "archive.zip", you would run:
unzip archive.zip documents/*
Preserving Directory Structure
When extracting files from a ZIP archive, you can maintain the original directory structure by using the -d
option to specify the target extraction directory:
unzip <zip_file> -d /path/to/extract/directory
This will extract the contents of the ZIP file, including any subdirectories, to the specified directory, preserving the original file hierarchy.
Listing Directory Contents
To view the contents of a compressed directory within a ZIP archive, you can use the unzip
command with the -l
(list) option:
unzip -l <zip_file> <directory_name>
This will display a list of files and subdirectories within the specified directory, without actually extracting them.
If you only want to extract certain files from a compressed directory, you can combine the directory name and the file names in the unzip
command:
unzip <zip_file> <directory_name>/<file1> <directory_name>/<file2>
This will extract the specified files from the compressed directory within the ZIP archive.
By understanding how to work with compressed directories and folders using the unzip
command, you can efficiently manage complex file structures and ensure that the original directory hierarchy is preserved during the extraction process.