Finding files inside an archive with Terminal

Looking for files inside an archive, without having to extract them first? Using Apple’s Terminal, or any Unix / Linux bash shell:

find /dir/ -iname archive.tar -print0 | xargs -0 tar t -f | grep "filename you are searching for"
find /dir/ -iname archive.zip -print0 | xargs -0 unzip -l | grep "filename you are searching for"
find /dir/ -iname archive.rar -print0 | xargs -0 unrar l | grep "filename you are searching for"

Command breakdown

find /dir/
use the powerful find command inside the folder where the archive is
-iname archive.tar
look for the archive (case insensitive search)
-print0
when files are found, spit them out in a list with a NULL at the end instead of a new line – this helps with the next step
|
pass the list generated by find to the next command…
xargs -0
…which is xargs, used to take a list of results and pass them on. The -0 is telling xargs to use NULL as the separator
tar t -f
use tar to list (t) the content of the archive, files only (-f)
unzip -l
use unzip to list the content of the archive
unrar l
use unrar to list the content of the archive
|
pass the list generated by find to the next command…
grep "filename you are searching for"
only show the items in the list that match the fillename, using grep

 

2 comments

  1. PatForg

    Thanks for the command.

    Perhaps I missed a step but if you are searching multiple archives how can you tell which one contains the file.

    Also if I’m looking for a file named “config” it will match all files containing the work config.

    Not sure how to go about solving these issues. Let me know if you have any thoughts on solutions.

Post a comment

You may use the following HTML:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>