O'Reilly logo

Beginning Mac OS® X Programming by Drew McCormack, Michael Trent

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

A.11. Chapter 11

A.11.1. Exercise 1 solution

  1. Open a terminal window in the Terminal application.

  2. Change to the Desktop folder using the cd command.

    cd ~/Desktop
  3. Make a directory to temporarily store copied files in. You should choose a directory name that is appropriate for the type of files you are copying. In this case, the directory name is TextFilesBackup.

    mkdir TextFilesBackup
  4. Issue the following find command to locate files with the extension txt that reside in your home directory or a subdirectory thereof. The command copies each file found to the backup directory.

    find ~ -name *.txt -exec /Developer/Tools/CpMac -r {} ~/Desktop/TextFilesBackup \;

    The globbing wildcard character * matches zero or more characters, so *.txt matches any text file. Each file found is copied by the CpMac command passed with the -exec option. CpMac has the advantage of preserving resource forks, whereas cp doesn't. The semantics of CpMac are very similar to cp.

  5. Use the following command to compress the backup folder with ZIP compression, while retaining all resource fork information:

    ditto -c -k --rsrc ~/Desktop/TextFilesBackup ~/Desktop/TextFilesBackup.zip

    The -k option forces ditto to use the universal ZIP format, which can be read on most operating systems. The --rsrc option means that resource fork data should also be preserved. The name of the directory to be compressed is given first after the options, and the archive file path next.

  6. Issue the following three commands to remove the TextFilesBackup ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required