loituma has asked for the wisdom of the Perl Monks concerning the following question:

Hi!

Is there a way to list the files of a tar archive, but only those of depth 1 ? (sort of equivalent to "find --maxdepth=1")

I am using

my @list_files = $tar->list_files;

and then I would have to parse @list_files to find the one that interest me.

Which is stupid, because $tar contains only one directory, which contains the file I want plus many (many) other. So if i could get directly the name the directory, it would be much faster. Is there a faster/simpler way to do that?

Many thxs!

Replies are listed 'Best First'.
Re: listing files in tar archive, with depth 1
by ambrus (Abbot) on Jun 23, 2011 at 19:30 UTC

    Getting a list of all files seems fine to me. Either way, you'd have to read all of the tar archive to find all the deep 1 filenames.

    You could use tar -t --exclude="*/*" from command-line to get only the depth one names, at least if you have GNU tar – but note that there need not be any such filename: the archive can contain files under some directory but not the directory itself.

    Update: if you're planning to extract the archive, it might be easier to extract it to a subdirectory first, then look at what files got created in that subdirectory, then move them to their final location.

      Ok, I thought I could do something faster, because I know the name of the file I am interested in (I just need the name of the dir). but if not, I'll keep the way I was doing, thxs.

      and thxs for the update advice; I donno yet, but it might be usefull for later