MarcK

Lets try this blog thing again…

Using ditto to create and extract archives

without comments

In OS X for several years now you could right click a file of folder on the desktop and select Archive or Compress and create a .zip archive. When you do this it’s more like creating a PKZip archive and all contents of the directory, in this case, are compressed. This includes meta data and .DS_Store files and probably a few other things I don’t know about. If you use the command line ‘unzip -l file.zip’ to list the contents of a Mac compressed file you will see something like this.

$ unzip -l folder.zip
Archive: /Users/me/Desktop/folder.zip
Length Date Time Name
-------- ---- ---- ----
0 06-21-13 11:16 Folder of stuff/
6148 06-21-13 11:15 Folder of stuff/.DS_Store
0 06-21-13 11:16 __MACOSX/
0 06-21-13 11:16 __MACOSX/Folder of stuff/
82 06-21-13 11:15 __MACOSX/Folder of stuff/._.DS_Store
104314 06-21-13 11:16 Folder of stuff/Lorem Ipsum.docx
82 06-21-13 11:16 __MACOSX/Folder of stuff/._Lorem Ipsum.docx
52064 07-01-10 15:19 Folder of stuff/man.jpg
11 06-21-13 11:16 Folder of stuff/Testfiles.txt
171 06-21-13 11:16 __MACOSX/Folder of stuff/._Testfiles.txt
10538 03-06-11 11:14 Folder of stuff/Untitled-1.jpg
-------- -------
173410 11 files

Note all of the “__MACOSX” directories. If you were to use the standard unzip command to extract the .zip you will get a directory named “__MACOSX” as well as your unarchived directory. Your newly unzipped directory does not contain any meta data or hidden resource files that may be necessary for some files to function properly. So how do you resolve this issue from the command line?

ditto to the rescue
If you don’t know the ‘ditto’ command and you manage Macs you should get to know it it’s fantastic for moving files and apparently even for archiving them. Check out the man page for ditto and you will get a number of useful examples. Until recently I didn’t know you could use the command for archiving files. Here is what the man page says about compression

The command:
ditto -c -k --sequesterRsrc --keepParent src_directory archive.zip
will create a PKZip archive similarly to the Finder's Compress functionality.

So in my example you could do the following to compress the “folder” directory.

ditto -c -k --sequesterRsrc --keepParent /Users/me/Desktop/folder /Users/me/Desktop/folder.zip

To extract folder.zip do this.

ditto -x -k /Users/me/Desktop/folder.zip /Users/me/Desktop

This will extract the archive and create a folder on the desktop named “folder”. It will replace anything with the same file/folder names or add items if names have changed.

Why is this important. You can use the command line to archive and extract anything including user directories and preserve ACLs and other meta data info. Much better than using zip.

Ditto has other quarks you should know about so read the man page and the examples then experiment before using it.

Written by Marc Kerr

June 21st, 2013 at 11:53 am

Posted in Apple,Tech,Work