Tunde recently introduced me to a useful OS X command “ditto”. It’s used to recursively copy directories and files. It creates directories and files that don’t already exist in the destination, and merges (replaces) those that do exist. It’s similar to “cp -r *” but differs in that it preserves resource forks and metadata by default.
For example, if we have
~/source/dir_1/text_a.txt /text_b.txt /dir_2/text_d.txt
we can use ditto in the following way:
ditto ~/source ~/destination
this will result in:
~/destination/dir_a/text_a.txt <- replaced /text_b.txt <- new /text_c.txt <- same /dir_b/text_d.txt <- new
The following yields similar results:
cp -r ~/source/* ~/destination
but if you look at “Date Modified” of the new files in the destination directory, you’ll notice that the files will maintain the same timestamp found in the source directory when using “ditto”. On the other hand, the timestamps will be updated with whenever you ran the command when using “cp -r”.
After looking up the documentation for “cp”, it seems like you can preserve metadata by adding the “-p” flag. I haven’t done extensive research around this, but I’m guessing that you can mimic the exact behavior of “ditto” with “cp” by playing around with the flags that you pass in.
Anyways, “ditto” comes in handy when adding assets during Android development. I rely on this site https://romannurik.github.io/AndroidAssetStudio/icons-generic.html to generate images and icons to support multiple screen sizes and densities. The icon generator tool produces a zip file that contains images placed in different directories for different densities. An output might look like this:
~/res/drawable-hdpi/new_icon.png /drawable-mdpi/new_icon.png /drawable-xhdpi/new_icon.png /drawable-xxhpi/new_icon.png /drawable-xxxhpi/new_icon.png
Using “ditto”, we can easily merge these with the appropriate directories in the Android project. It makes things especially easier when you have to play around with the size, padding, and color in order to generate the icon and things become repetitive.