Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract list of file names in a zip archive when `unzip -l`

Tags:

linux

unix

ksh

When I do unzip -l zipfilename, I see

1295627  08-22-11 07:10   A.pdf 473980  08-22-11 07:10   B.pdf ... 

I only want to see the filenames. I try this

unzip -l zipFilename | cut -f4 -d" " 

but I don't think the delimiter is just " ".

like image 305
Thang Pham Avatar asked Aug 22 '11 13:08

Thang Pham


People also ask

How do I list the contents of a Zip file?

Also, you can use the zip command with the -sf option to view the contents of the . zip file. Additionally, you can view the list of files in the . zip archive using the unzip command with the -l option.

How do I extract contents of archive?

Right-click on the downloaded archive file and select Extract All... from the context-sensitive menu that displays. The Extract Compressed (Zipped) Folders dialog displays. Click Browse to navigate to the location where you want the files and directories that are contained in the archive to be extracted.

How do I extract data from a zip file?

Right-click the file you want to zip, and then select Send to > Compressed (zipped) folder. Open File Explorer and find the zipped folder. To unzip the entire folder, right-click to select Extract All, and then follow the instructions. To unzip a single file or folder, double-click the zipped folder to open it.

How do I view the contents of a ZIP file in Python?

What you need is ZipFile. namelist() that will give you a list of all the contents of the archive, you can then do a zip. open('filename_you_discover') to get the contents of that file.


2 Answers

The easiest way to do this is to use the following command:

unzip -Z -1 archive.zip 

or

zipinfo -1 archive.zip 

This will list only the file names, one on each line.

The two commands are exactly equivalent. The -Z option tells unzip to treat the rest of the options as zipinfo options. See the man pages for unzip and zipinfo.

like image 195
amicitas Avatar answered Sep 25 '22 11:09

amicitas


Assuming none of the files have spaces in names:

unzip -l filename.zip | awk '{print $NF}' 

My unzip output has both a header and footer, so the awk script becomes:

unzip -l filename.zip | awk '/-----/ {p = ++p % 2; next} p {print $NF}' 

A version that handles filenames with spaces:

unzip -l filename.zip | awk '     /----/ {p = ++p % 2; next}     $NF == "Name" {pos = index($0,"Name")}     p {print substr($0,pos)} ' 
like image 40
glenn jackman Avatar answered Sep 24 '22 11:09

glenn jackman