Apple - How can I get back a system file after deleting it from my Mac?
If you have a recent Mac then it has a recovery partition.
Power on your Mac and hold down Command+R until the Apple logo appears. This will cause your Mac to boot using the recovery partition.
Once you are in the recovery partition, you can then reinstall the operating system.
This Apple technical note, OS X: About OS X Recovery, gives more details.
You can reinstall the OS and leave your files intact. Apple removed the erase/install option so you don't have to worry about the Recovery Installer erasing your files. You now have to explicitly launch the Disk Utility tool to erase or reformat/repartition a drive when booted from recovery OS.
If you don't mind or are sure your Recovery HD is patched to match the OS on your main system, you can snag a file and see if it works without needing to do a full reinstall of the OS and without needing a restart or an internet connection / saved installer.
Use the Recovery partition
Many1 system files are also included on the Recovery partition and can be copied over.
Mount the Recovery HD
diskutil mount Recovery\ HD
Mount the BaseSystem.dmg
hdiutil mount /Volumes/Recovery\ HD/com.apple.recovery.boot/BaseSystem.dmg
Copy the file
FILE="/usr/bin/codesign" sudo cp -p /Volumes/OS\ X\ Base\ System/${FILE} ${FILE}
This process can be combined into a single-line command if you have to run it frequently…
FILE="/usr/bin/codesign"; diskutil mount Recovery\ HD && hdiutil mount /Volumes/Recovery\ HD/com.apple.recovery.boot/BaseSystem.dmg && sudo cp -p /Volumes/OS\ X\ Base\ System/${FILE} ${FILE}
Even if you have deleted the cp
binary (which could happen), there is /usr/bin/ditto
, /usr/bin/tar
and several other commands that can stand in for cp
.
Reinstall OS X
If you've got the ability to reinstall OS X through the Recovery HD, Internet Recovery or an OS X install media then reinstalling OS X will 'repair' your install and copy any missing/broken system files back to your system.
I recommend that you do a permissions repair using Disk Utility after copying a file manually and then rebooting. If the fix works, now would be an excellent time for another backup snapshot.
1 Not all system files are on the Recovery HD, but many are. If the copying command doesn't work, check that the file exists on the Recovery HD.
If you are sure your Recovery HD is patched to match the OS on your main system, you can snag a file and see if it works without needing to do a full reinstall of the OS.
Hold ⌘-R during startup until you see a window similar to this:
These are your options:
Choose Utilities>Terminal:
Your OS X disk will be mounted in
/Volumes/<your disk name>
. For example, on my Mac it was mounted in/Volumes/Mavericks
:df Filesystem 1K-blocks Used Available Use% Mounted on /dev/disk0s2 249219484 145891460 103072024 59% /Volumes/Mavericks
Now copy
/bin
to your OS X disk:cp -a /bin /Volumes/<your disk name>
Note that the contents of
/bin
will be outdated, as the recovery disk is not updated with the rest of the OS, so restore/bin
from Time Machine after successfully starting your Mac and logging in.