I have a zipped file containing 10,000 compressed files. Is there a Linux command/bash script to unzip only 1,000 files ? Note that all compressed files have same extension.
Unzip only limited number of files in linux
Asked Answered
unzip -Z1 test.zip | head -1000 | sed 's| |\\ |g' | xargs unzip test.zip
- -Z1 provides a raw list of files
- sed expression encodes spaces (works everywhere, including MacOS)
You can use wildcards to select a subset of files. E.g.
Extract all contained files beginning with b:
unzip some.zip b*
Extract all contained files whose name ends with y:
unzip some.zip *y.extension
You can either select a wildcard pattern that is close enough, or examine the output of unzip -l some.zip
closely to determine a pattern or set of patterns that will get you exactly the right number.
I did this:
unzip -l zipped_files.zip |head -1000 |cut -b 29-100 >list_of_1000_files_to_unzip.txt
I used cut to get only the filenames, first 3 columns are size etc.
Now loop over the filenames :
for files in `cat list_of_1000_files_to_unzip.txt `; do unzip zipped_files.zip $files;done
I would recommend for the second command: cat list_of_1000_files_to_unzip.txt | xargs unzip zipped_files.zip There are some issues with this answer (file names with spaces for example), but it should perform at least as well in all respects than the for loop. –
Derward
Thanks ! That makes the command shorter! If you look closely, there isn't any space in any filename. 1) list_of_1000_files_to_unzip.txt 2) zipped_files.zip are the only filenames used here. What other issues do you see? –
Yokoyama
I was referring to spaces in the names of the zipped files. xargs (and for) will split on spaces, making "C:\Documents and Settings\Ferret food recipies.doc" into: C:\Documents, and, Settings\Ferret, food, and recipies.doc (5 separate files) –
Derward
Some advices:
- Execute zip to only list a files, redirect output to some file
- Truncate this file to get only top 1000 rows
- Pass the file to zip to extract only specified files
© 2022 - 2024 — McMap. All rights reserved.