rocx
rocx

Reputation: 285

Unzip only limited number of files in linux

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.

Upvotes: 6

Views: 4115

Answers (4)

chadwackerman
chadwackerman

Reputation: 815

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)

Upvotes: 5

rocx
rocx

Reputation: 285

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

Upvotes: 1

Slartibartfast
Slartibartfast

Reputation: 1700

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.

Upvotes: 1

zealot
zealot

Reputation: 247

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

Upvotes: 0

Related Questions