Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Archiving from the command line UNIX
Stuffit Deluxe is a great tool for most archiving duties, but not too good if you have a large number of individual file archives to create. I had this problem with uploading quiz results to individual students. To save space, I needed to ZIP each of 23 files and to do that with a GUI is pretty clumsy. However, I discovered that two CLI tools, zip and unzip, can make this job much easier.

unzip works just like you'd expect:
  unzip [zipfilename]
zip, on the other hand, took a little figuring out:
   zip -h                              {...dumps online help listing}
zip [zipfilename] [file to zip] {...zips and retains the original file}
zip -m [zipfilename] [file to zip] {...zips and removes the original file}
Best of all, I created a script called smush to help me out even more.
# smush - a quick and dirty script to zip pdfs

for i in *.pdf
echo "zipping ${i}"
zip -m $ $i
I cd to the directory, type smush and it all works automagically. Sweet!

[Editor's note: You'll need to enter the above lines into a terminal editor such as pico, save the file, make it executable (chmod 755 filename), and then make sure it's on your path to use the script.]
  • Currently 0.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (0 votes cast)

Archiving from the command line | 13 comments | Create New Account
Click here to return to the 'Archiving from the command line' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Authored by: mazucka on Nov 05, '01 10:42:41AM

Mr. Sinclair's comment says 'quick and dirty script to gzip pdfs'. Bear in mind that the 'zip' command produces zip files while the 'gzip' commend produces gzip files. The two are different, though most of the popular decompression/etraction utilities can manage either format.

My questions is will this deal with files that have resource forks?

[ Reply to This | # ]
Why not use DropZip with open?
Authored by: houchin on Nov 05, '01 11:08:29AM

zip -h will tell you that this is the original old zip app that's been around on Unix systems
for years. In fact, this particular version was last modified in 1996. I used to use this
on Unix system, and my recollection is that it didn't even maintain any directory structure.

Instead of using this, why don't you just use DropZip from the terminal. Just create an alias in /usr/share/init/tcsh/aliases.mine (so all users can use it):
alias dropzip "open -a '/Applications/Utilities/StuffIt Deluxe 6.0/StuffIt Drag and Drop/DropZip'"

You could also use gzip, but you might have some interoperability problems with Windows users, and you would also have to use tar to archive everything into a single file (gzip only compresses single files).

[ Reply to This | # ]
Authored by: Louis on Nov 05, '01 11:15:28AM

why do we have to use pico?

i now use bbedit 6.5 with its unix line tool then i save with unix line endings.

is this ok to do as well?


[ Reply to This | # ]
Authored by: tomsinclair on Nov 05, '01 04:05:21PM

The script was originally designed to use gzip, but I found my students couldn't
open the gzipped files. I switched to zip and forgot to change the comment.

Sorry about the confusion.

(It did say 'quick and dirty', though.)

[ Reply to This | # ]
Just set the preference!
Authored by: sjonke on Nov 05, '01 11:26:17AM

In DropStuff just go into preferences and click the checkbox next to "Stuff each items as an individual archive". Then it's just a command-a in the folder and drag onto drop stuff. No command-line needed. For extra convenience, drag DropStuff into the Finder toolbar so it's right there for use. Same thing can be done with DropZip if you prefer .zip format. Lastly, note that if you hold the option key while dropping files/folders onto DropStuff or DropZip it will bring up the prefs dialog for temporary settings so that you can just use this setting as needed rather than setting it as the default.

[ Reply to This | # ]
Thanks, nice tip!
Authored by: tomsinclair on Nov 06, '01 06:21:39PM

It just goes to show that "There's always more than one way to do it".

I did it my way because, as an old UNIX hack, I'm more comfortable scripting tasks
like this. But now I'll try out your trick as well...

[ Reply to This | # ]
Authored by: fireproof on Nov 05, '01 12:00:39PM

The example you've given is useful to me for other reasons - I'm a long time Mac guy, and sometimes even the most obvious UN*X tricks seem obscure, so an example of "for i in *.txt" is gold.

After seeing this I was able to set up a small script to use makedoc on every text file in a directory (once #!/bin/sh had been added to the top of the script). Now I've just got to figure out how to move through sub-directories, and my entire Palm doc library will be ready to go...

[ Reply to This | # ]

Authored by: osxpounder on May 07, '04 12:17:53PM

Hmm, what is makedoc?


[ Reply to This | # ]
resource forks
Authored by: jimr on Nov 05, '01 12:26:45PM
My questions is will this deal with files that have resource forks?

my, that is a good question.

the zip format will not. maybe the new version of tar will(in 10.1) or you should get the Gnutar from sourceforge which supposedly supports the /rsrc flag.

there is a perl script called wtar which has been mentioned on these pages. that will work, but not so good for sending files out to people who don't have either the correct tar utility or who might be unaware of the need to throw the flag...

there is a new version of pax in the works called hfspax which might also solve the resource problem and if you undo it on another platform it will put a #resource and #finderInfo folders next to the items...
On the other hand, while pax supports nearly everything, you might have to dig around inside to find out what was being supported if you don't have pax.

Stuffit gets around (sort of) the resource problem, by Using MacbinaryIII to preprocess files in a zip operation. Then windows users could have some problem with files which either didn't need the treatment or are anyway mac fles....

the Devtools contain a few utilities which might be used together to hack together a
shell scripted means of zipping or gzipping mac files.

such a way would also require a undoing script as in the wtar example above.

so, the best thing would be to create a console application based on the Stuffit Engine SDK
or find some way to port the Linux based app over to BSD.

in the meantime, there is not really much "awkward" about the GUI apps
and you should be able to call the apps with some filename arguments by some trick (osascript???)

or you might just consider putting your files into a .dmg via hdiutil

and this works (apparently from anywhere)

open -a DropStuff ~/myfile

[ Reply to This | # ]
Best archival tools on the command line
Authored by: dws on Nov 05, '01 04:37:06PM

Why not use a combination of tar and gzip? They're the best for quick archiving and compatible with Stuffit Expander.

tar -zcf archive.tar.gz [files.....]

then: tar -zxf archive.tar.gz, and you'll have all of your folders/files.

If you just want to compress files and not folders use gzip:

gzip filename

That gives you a file with the name filename.gz and your original is removed.

Say you wanted to find all .pdf files in a give directory and automatically compress them? No need for a shell script just do the following:

find /path/to/directory -name '*.pdf' -exec gzip {} ';'

After that all files named {something}.pdf will be gzipped up!

[ Reply to This | # ]
zip and backup techniques
Authored by: shacker on Nov 05, '01 10:22:40PM
FWIW, I wrote a pretty complete guide to using zip for backups at OSXHints' sister site, The BeOS Tip Server. The zip tip there includes lots of info on the various zip flags for creating incremental backups, examing archive contents without decompressing, etc.

Read it here.

[ Reply to This | # ]

zip and backup techniques
Authored by: osxpounder on May 07, '04 12:23:16PM

Your link to the zip tips is dead -- I am hoping you or someone else sees this and knows where those tips can be found. I am puzzling over a zip problem in Panther right now -- a huge archive I created with OSX won't unzip with 3 different zip-compatible utilties I've tried so far. I'll be posting in the forums, here, with details on that and a request for advice, but long story short: a 4GB zip file created by OSX won't open with anything I've tried [except by double-clicking it in the Finder, to use OSX to unzip it].


[ Reply to This | # ]
Where to get zip/unzip tools
Authored by: James Harvard on Jul 10, '03 08:56:23PM
If, like me, you read this and wonder why you can't find zip/unzip on your system, it's because they are only installed as part of Apple's Developer Tools. However you can install them on their own with the package from this Version Tracker page. HTH!

[ Reply to This | # ]