2

I'm trying to find a (very old) file, from which I'm sure it is somewhere on my NAS. Unfortunately I have some of my old files stored inside tar.gz, zip or 7z.

Is there any way to search all files and folders including the content of these archives?

Maybe find or locate can be instructed to consider these archives as well and take a look inside on their way down the directory tree?

1 Answer 1

4

You can search within each particular archive type as follows:

cd /dir/where/you/want/to/start/search
find . -iname '*.zip' -exec unzip -l {} \; | grep 'file_pattern'
find . -iname '*.tar.gz' -exec tar tvf {} \; | grep 'file_pattern'
find . -iname '*.7z' -exec 7z l {} \; | grep 'file_pattern'

You could simplify its use by always search starting in the current directory, and accepting the file matching pattern (which could include regular expressions or simply be all or part of the filename) as a command-line parameter:

Create a script (named, e.g. "arcsearch") in your executable path (e.g. /usr/local/bin), making sure you "chmod +x" it to make it executable, with this content:

find . -iname $1
find . -iname '*.zip' -exec unzip -l {} \; | grep $1
find . -iname '*.tar.gz' -exec tar tvf {} \; | grep $1
find . -iname '*.7z' -exec 7z l {} \; | grep $1

Then you can simply type:

arcfind whatever_you_want

and it'll spit out the results, both "normal" files and those within archives.

5
  • Thanks, thats a good start! Is there a way to combine all this (plus "normal file" search) into one command? Or do I need to perform the search for every archive/file type over again?
    – kerner1000
    Aug 2, 2015 at 21:56
  • Added it to the answer. Aug 4, 2015 at 18:12
  • To also include all regular files, another line in this script would just be find . -iname $1, I assume?
    – kerner1000
    Aug 5, 2015 at 19:00
  • .. But this script would anyways search the whole tree multiple times..
    – kerner1000
    Aug 5, 2015 at 19:00
  • Yes and yes. Since the find command is only traversing the directory structure and not actually having to look inside all the files each run, though, a lot of it's cached so the extra executions run a lot faster -- but, of course, if it's reading through a lot of archives then that all will tend to flush the disk cache... Aug 6, 2015 at 20:09

You must log in to answer this question.

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