LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 04-29-2016, 02:22 AM   #1
lucmove
Senior Member
 
Registered: Aug 2005
Location: Brazil
Distribution: Debian
Posts: 1,153

Rep: Reputation: 85
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.
 
Old 04-29-2016, 02:36 AM   #2
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=14, FreeBSD_12{.0|.1}
Posts: 5,794
Blog Entries: 23

Rep: Reputation: 3813Reputation: 3813Reputation: 3813Reputation: 3813Reputation: 3813Reputation: 3813Reputation: 3813Reputation: 3813Reputation: 3813Reputation: 3813Reputation: 3813
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!

Last edited by astrogeek; 04-29-2016 at 02:38 AM.
 
Old 04-29-2016, 02:42 AM   #3
lucmove
Senior Member
 
Registered: Aug 2005
Location: Brazil
Distribution: Debian
Posts: 1,153

Original Poster
Rep: Reputation: 85
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.
 
Old 04-29-2016, 03:22 AM   #4
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=14, FreeBSD_12{.0|.1}
Posts: 5,794
Blog Entries: 23

Rep: Reputation: 3813Reputation: 3813Reputation: 3813Reputation: 3813Reputation: 3813Reputation: 3813Reputation: 3813Reputation: 3813Reputation: 3813Reputation: 3813Reputation: 3813
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.
 
Old 04-29-2016, 03:46 AM   #5
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=14, FreeBSD_12{.0|.1}
Posts: 5,794
Blog Entries: 23

Rep: Reputation: 3813Reputation: 3813Reputation: 3813Reputation: 3813Reputation: 3813Reputation: 3813Reputation: 3813Reputation: 3813Reputation: 3813Reputation: 3813Reputation: 3813
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!

Last edited by astrogeek; 04-29-2016 at 03:49 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
gzip -r -c directory > archive.gz pmora Linux - Newbie 7 03-18-2015 12:20 PM
/etc/lvm/archive Directory sachin.davra Linux - Newbie 1 03-21-2014 11:28 AM
need to careate archive od only the directory procfs Linux - Newbie 4 03-12-2007 01:46 AM
(un)tar a certain directory from an archive Suinatsa Linux - Software 2 06-20-2006 08:09 AM
Trying to archive a 4.6 GB directory kaplan71 Linux - General 2 04-27-2004 11:31 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 10:17 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration