LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   Does that archive contain a directory? (https://www.linuxquestions.org/questions/linux-software-2/does-that-archive-contain-a-directory-4175578604/)

lucmove 04-29-2016 01:22 AM

Does that archive contain a directory?
 
I often want to unpack a zip file (or other archive format) in a directory with many other files and I can do that very quickly except for one problem: it may spread lots of files among the others already existing in the current directory. Some zip files contain a top-level directory, but many don't. So I have to open it with Xarchiver every time to check if it has a top-level directory and it's safe to expand it or if I have to drag it into a new directory so it won't make a mess.

There is a way around that:

Code:

for i in *.zip
    unzip -d (basename $i .zip) $i
end

The problem with that code is that zip files that already contain a top-level directory will expand to a directory that contains a directory that contains what I really want. In other words, an additional, unnecessary and undesired level will be created in the hierarchy. And I would like to avoid that.

Is there any programatic way to tell if an archive contains a top-level directory? I mean, short of expanding it then going into the directory and inspecting the contents, then testing each entry whether it's a file or directory, etc? I can't find anything like that in the zip/unzip manuals. Other archive formats would be interesting, but the ones I deal with are almost entirely zip files, sometimes the occasional rar.

astrogeek 04-29-2016 01:36 AM

You don't have to open it, use unzip -l to see what is inside.

man unzip

Here is a quick example of the format. In this case you can see that everything is under parse/...
Code:

unzip -l ex.zip
Archive:  ex.zip
  Length      Date    Time    Name
---------  ---------- -----  ----
        0  04-28-2016 14:05  parse/
        0  04-27-2016 22:41  parse/extras/
    19170  09-20-2014 17:25  parse/extras/bison.jpg
        0  04-28-2016 18:42  parse/dwgs/
    63220  04-28-2016 17:55  parse/dwgs/class_bison_C_members.png
    14434  04-27-2016 23:52  parse/dwgs/seq_bison_basic.png
    32101  04-28-2016 18:47  parse/dwgs/seq_bison_cpp_class.png
        0  04-28-2016 17:58  parse/uml/
    12378  04-28-2016 18:47  parse/uml/seq_bison_cpp_class.xpi
    2204  04-28-2016 18:47  parse/uml/class_bison_C_members.xpi
    5815  04-28-2016 01:59  parse/uml/seq_bison_basic.xpi
        0  04-28-2016 15:46  parse/notes/
      293  04-28-2016 15:46  parse/notes/cpp_notes
      891  04-28-2016 14:07  parse/notes/bison_extend.txt
---------                    -------
  150506                    14 files

For tar files it is -t (man tar).

For rar... you are on your own!

lucmove 04-29-2016 01:42 AM

That amounts to the same as opening it with Xarchiver. Not what I want.

I want a programatic way to analyze the archive that I can use in a script, which will decide on its own whether to create the additional directory or not.

astrogeek 04-29-2016 02:22 AM

So without you having to put eyeballs on it?

In that case, still use the unzip -l and pipe the output into sed or awk to look at the last field and see if it has any "/" characters in it. If any lines do not contain at least one "/" then they are not within a parent directory.

That would still allow for more than one parent directory, so if you want to restrict it strictly to a single top level directory you would need to set some rule to enforce one and only one, but that should be easy enough as well.

astrogeek 04-29-2016 02:46 AM

Out of curiosity I just wrote an expression that works on the above example.

It only looks at lines between the ----... lines and drops any lines with "/"

If it returns an empty string then all lines have a parent directory, otherwise it returns any line without a parent directory.

Simple enough, give it a try!


All times are GMT -5. The time now is 09:18 PM.