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 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. |
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 For rar... you are on your own! |
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. |
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. |
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. |