Terminal – How to Preview Archive Contents Without Extracting

archiveterminal

less command on Ubuntu allows to peek inside different kinds of archives from terminal. For example, all of these commands would show me the list of files in each of the four archives:

less archive.zip
less archive.tar.gz
less archive.tar.bz2
less archive.7z

On MacOS, I can use vim for this. However, this is less convenient, because Vim requires four buttons to be pressed to exit it (:q + return) instead of one (q).

Is there an equivalently fast way to see list of files in archives for MacOS?

Best Answer

Manually

  • zip: unzip -v FILE.zip
  • tar.gz: tar tvzf FILE.tar.gz
  • tar.bz2: bunzip2 -c FILE.tar.bz2 | tar tvf -
  • .7z: 7z l FILE.7z

PS: For .7z you need to run brew install p7zip first

Extend less to work the same way it does under Linux

  • Install lesspipe

    brew install lesspipe
    
  • Add the following lines to .bashrc (or the profile of whatever shell you use)

    if [[ -x /usr/local/bin/lesspipe.sh ]]; then
        export LESSOPEN="|/usr/local/bin/lesspipe.sh %s"
        export LESS_ADVANCED_PREPROCESSOR=1
    fi
    
  • Source .bashrc or open a new terminal tab