0

To get the content of a zipped file, you can do unzip -p archive.zip file1.txt | less as said here

However, for that is for the case where you are "positioned" in the path where your .zip is. If you are not (for the case the command being executed in a shell script for example), I guess I would need to pass in the absolute paths for the .zip and the file, but a filename not matched: error is thrown.

For a .zip in the home directory I tried this commands:

unzip -p ~/archive.zip ~/archive.zip/file1.txt | less
unzip -p ~/archive.zip ~/archive/file1.txt | less

but I get the filename not matched: error.

How can I get the content of a txt file, without unzipping using absolute paths? (meaning not having to be in the specific directory of the .zip to run the command or to be able to perform that task form other path or a shell script)

1
  • The second argument referencing file1.txt as if from your home directory (with ~/) is incorrect. Try unzip -l ~/archive.zip to see the exact path of file1.txt within the zipfile, not in the file system. I.e., probably just unzip -p ~/archive.zip file1.txt | less (though it could be subdir/file1.txt) Mar 8 at 22:55

1 Answer 1

1

The -p flag extracts files to pipe insted of unzipping into actual files. You don't need to use any additional path. Use this:

unzip -p ~/archive.zip file1.txt | less

I tested it in Centos 8:

enter image description here

3
  • Thanks for your answer. I do still get the error. I also tried hardcoding the path, changing ~/archive.zip to /home/pi/archive.zip. same result. In case it makes any diffrence this is a raspberrypi OS Sep 22, 2021 at 8:22
  • seems that this does the job unzip -p /home/pi/archive.zip '*/file1.txt' Sep 22, 2021 at 13:44
  • It's weird, maybe some different implementation of the unzip command?
    – Kar.ma
    Sep 23, 2021 at 9:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .