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

Create Finder-equivalent zip archives from the Terminal UNIX
I absolutely love the "Create Archive of ..." functionality in Panther, found in the Finder's File menu or in the contextual menu when you control-click on an item in the Finder. It's much faster than Stuffit, and it doesn't require me to install any third-party software. Sometimes I need to be able to create archives from the command line, and I wondered how I could get the same functionality of the "Create Archive of ..." function in the terminal. tar and gzip are out because they don't preserve resource forks and HFS metadata.

The answer: the ditto command. After some experimentation, I found that ditto with the following options will create an archive just like the "Create Archive of ..." function:
ditto -c -k -keepParent -rsrc source destination
where source is the file or directory you want to archive and destination is the name you want to give the new archive file. For example, to create an archive of a directory named Foo to a file named, use:
ditto -c -k -keepParent -rsrc Foo
[robg adds: We've covered ditto before, and the above syntax was included in a handy folder action script hint, but I thought it might be worth sharing the command-line version in its own hint.]
  • Currently 2.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (2 votes cast)

Create Finder-equivalent zip archives from the Terminal | 7 comments | Create New Account
Click here to return to the 'Create Finder-equivalent zip archives from the Terminal' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Create Finder-equivalent zip archives from the Terminal
Authored by: ncianca on Jan 15, '04 05:34:23PM

You can also add an alias to your .cshrc file that will append a timestamp to the zip archive like this:

alias zipme 'ditto -c -k -keepParent -rsrc \!* \!*_`date +"%Y%m%d%S"`.zip'

zipme foldername

will produce a zip file called something like this:

which is handy if you use it to create quick backups of work folders.

[ Reply to This | # ]
zip versus ditto
Authored by: kaptaineric on Mar 08, '04 03:43:31PM
I'm fairly new to the ditto command but have found that it will not allow you to add to an already created archive. Or can it and I just don't know the command?

So what I have been using to archive my data via the command line is the zip command.

zip -r -u -g /Volumes/Media/username/Pictures/ /Volumes/iMac_Data/username/Pictures/"iPhoto Library"
zip -r -u -g /Volumes/Media/username/Pictures/ /Users/username/Library/Preferences/

Now lets say that there is another file that I want to add to my archive, I would repeat the command with the new file:

zip -r -u -g /Volumes/Media/username/Pictures/ /Users/username/Desktop/Homework.pdf

It is the only method that I am aware of that will allow me to add to an existing archive. Is this procedure in any way unorthodox?

[ Reply to This | # ]
Create Finder-equivalent zip archives from the Terminal
Authored by: possen on Jul 17, '04 01:44:53PM

Just for completeness; to uncompress a finder archive, preserving resources, you use this version of the ditto command.

ditto -x -k -sequesterRsrc .

This pulls the resource information out of the __MacOSX folders and reconstructs the entire file.

[ Reply to This | # ]
Create Finder-equivalent zip archives
Authored by: ihelp-mac on Aug 11, '04 07:14:16PM

I'm CERTAIN this capability was part of the OSX 10.3.0 Finder... Highlighting a file or folder and then hitting Command-Option-3 would create a ZIP archive, on the desktop...

However, I cannot replicate this capability in OSX 10.3.5----has Apple taken this function away? I note that the Finder FILE menu still has this command and it still well as having it in the control/right-click contextual menu...

However, there is *NO* Keyboard-shortcut linked to this capability, nor can I figure out a way to get the SystemPreferences:Keyboard&Mouse:KeyboardShortcuts menu to accommodate for this missing keystroke combination

I'd like to have it back! HELP! :-) thanks-in-advance!....

David A. Lewis

[ Reply to This | # ]
Create Finder-equivalent zip archives from the Terminal
Authored by: pwharff on Jan 05, '05 01:43:39PM
Or, create a shell script using the $n special variable and named it my Zip with an uppercase Z and don't forget to
chmod 755 Zip
and add it to /usr/local/bin so that you can execute it anytime:

ditto -c -k -keepParent -rsrc $1 $2
...and to unzip, I created another shell script called Unzip:

ditto -x -k -sequesterRsrc $1 .

[ Reply to This | # ]
--keepParent, not -keepParent
Authored by: victory on Jul 07, '05 11:30:11PM

...or at least it's that way with the version of ditto that comes with 10.4

[ Reply to This | # ]
--keepParent, not -keepParent
Authored by: jmontana on Feb 17, '06 12:43:56PM
Indeed. If you forget that extra -, you'll end up with this seemingly unrelated error message:

ditto: invalid option -- e
Usage: ditto [ <options> ] src [ ... src ] dst

[ Reply to This | # ]