LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 10-19-2007, 04:08 PM   #1
AckSed
LQ Newbie
 
Registered: Oct 2007
Location: Manchester
Distribution: Zenwalk 6
Posts: 3

Rep: Reputation: 1
Recursively zipping directories


I want to zip up a lot of directories recursively into separate zip files,each with the name of the directory as its filename,like so:

foobar/ -> foobar.zip

I've tried this shell code that I found elsewhere on the forums
Code:
for i in * ; do zip -r /zipdir/$i.zip $i ; done
but all I get is
Code:
zip error: Could not create output file (/zipdir/Mahou_Sensei_Negima_191.zip)
zip I/O error: No such file or directory

zip error: Could not create output file (/zipdir/zipdir.zip)
Would/could you use something like 'dirname'?
 
Old 10-19-2007, 07:14 PM   #2
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
Why use zip and not gzip or bzip2? The latter are better supported in Linux.

You could use two find commands. The first one to return the directories, and the second one to return files in that directory.

Code:
for dir in $(find basedir/ -type d); do 
   find $dir/ -maxdepth 1 -type f -print0 -exec tar czvf ${dir}.tar.gz '{}' +; done
Using zip the zip command and options will be different. Also read the docs in /usr/share/loc/zip. There may be a filesize limit (for files you are archiving) and an archive size limit; that you need to keep in mind.

However, why not simple archive the entire directory recursively?
Otherwise you end up with files mixed in with archives as a result.

Last edited by jschiwal; 10-19-2007 at 07:17 PM.
 
Old 10-19-2007, 07:29 PM   #3
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
Looking back on your post, it looks like you do want to archive the directories in a base director recursively.

To get a list of directories, you can use:
for dir in */; do

Note the slash after the asterisk.

Code:
for dir in */; do zip -r $(basename $dir) ${dir}; done
Quote:
for i in * ; do zip -r /zipdir/$i.zip $i ; done
Another problem with what you tried is the slash before zipdir. This will try to create a zip file in root where you don't have write permissions.

Last edited by jschiwal; 10-19-2007 at 07:34 PM.
 
  


Reply

Tags
compression, recursive, shell scripting, zip


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
recursively remove directories iAlta Linux - Software 7 10-06-2007 09:25 AM
Remove directories recursively with same name cboyd Linux - Newbie 5 01-14-2007 09:48 PM
chmod directories recursively mfilippa Linux - Newbie 3 04-17-2006 07:24 PM
Recursively traversing sub-directories ark86 Linux - Newbie 2 01-29-2006 01:15 PM
Can ls recursively list only directories? Vosper Linux - General 3 07-16-2005 03:57 AM


All times are GMT -5. The time now is 07:35 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration