Introduction to Terminal for Disk Maintenance
If you’re following me on Twitter, you know about my tweets about my hate for Disk Utility. For some reason, even off clean installs, Disk Utility, whenever used to manage partitions, always will have a spaz attack mid-operation and cause a kernel panic. This would not be such a big deal all in itself, but since a kernel panic is an even worse version of a unclean shutdown, file corruption is always a likely outcome. To me, it’s not worth risking data loss, seeing as a I have irreplaceable data on my hard drive, such as the hundreds of photographs I’ve taken. I plan to show you the basic things most people use Disk Utility for and how to do those things much more reliably in Terminal.
Finding Terminal
Since Terminal is a free utility included in Mac OS X, it can be found in /Applications/Utilities/Terminal.app. You can also access Spotlight to open Terminal, by typing ⌘+Space. Note that on the newer Apple keyboards “Cmd” takes the place of “⌘”.
What is Terminal?
Terminal is a shell interface for Mac OS X. It allows you to access the power of UNIX via the command line. Terminal is not a GUI application, meaning there are no buttons or sliders or menu options (but only to access the preferences for the application, not its commands), you type in the commands.
Verifying Disk Integrity
Using the UNIX command “fsck“, an acronym for “file system check“, will check over the sectors of your hard drive to fix problems. Since you’re booted onto the drive you’re going to be checking, we aren’t going to repair it just yet if repairs are necessary. But before we do all that, you need to find the name of the drive you’re going to be checking.
Finding Filesystem ID
In order to find your filesystem ID, in Terminal, type in the following.
df -hl
Then the output should look something like this:
Filesystem Size Used Avail Capacity Mounted on
/dev/disk0s2 90Gi 84Gi 5.2Gi 95% /
/dev/disk1s1 7.5Gi 1.3Gi 6.2Gi 18% /Volumes/JOSH_CRANE
/dev/disk2s2 205Gi 172Gi 33Gi 84% /Volumes/Josh’s Backup Disk
/dev/disk2s3 27Gi 23Gi 4.7Gi 83% /Volumes/iPhoto Library
In this case, I’m going to be running filesystem check on my partition on my external hard drive, on the partition “iPhoto Library”. In this case, the partition has a filesystem ID of /dev/disk2s3.
Verifying and Repairing the Filesystem
Next, I’m going to type the following in Terminal, where /dev/disk2s3 is your filesystem ID to be verified. This will tell fsck that I want to verify the filesystem, and if it need repair, to repair it. The following command will only work for filesystems formatted as HFS+ with journaling.
sudo fsck_hfs -fy /dev/disk2s3
Next, press return and you should be prompted for your administrator password. Enter your password and press return. Be sure to know that since when you enter you password, this is a secure entry and no bullets, asterisks, or even blank spaces will appear for each character of your password.
fsck Output
Since this command used the flag f and y, f forcing fsck to check filesystems it thinks are otherwise “clean”, and y telling fsck to answer “yes” to all questions about fixing, repairing, or salvaging corrupt data. The only way for fsck to proceed is to answer yes to all questions, so using the y flag should always be done unless you only want to verify a volume, not to repair it (see below). In my case, the following output was displayed.
** /dev/rdisk2s3
** Verifying volume when it is mounted with write access.
** Checking Journaled HFS Plus volume.
** Checking Extents Overflow file.
** Checking Catalog file.
** Checking multi-linked files.
** Checking Catalog hierarchy.
** Checking Extended Attributes file.
** Checking volume bitmap.
** Checking volume information.
** The volume iPhoto Library appears to be OK.
Using fsck for Verification Only
If you don’t want to repair the given filesystem for whatever reason, or you’re going to be verifying the drive/partition you’re currently booted onto, you can tell fsck to operate in read only mode with the flag “-l“. To put this into practice, use the command below.
sudo fsck_hfs -l /dev/disk2s3
The output should then look something like the following.
** /dev/rdisk2s3 (NO WRITE)
** Performing live verification.
** Checking Journaled HFS Plus volume.
** Checking Extents Overflow file.
** Checking Catalog file.
** Checking multi-linked files.
** Checking Catalog hierarchy.
** Checking Extended Attributes file.
** Checking volume bitmap.
** Checking volume information.
** The volume iPhoto Library appears to be OK.
Repairing Root Partition
This section of the guide is for people who have a corrupt boot partition (Mounted on /). Now, Apple says to use your Leopard (or whatever version of OS X you have) install disk, then run disk utility from that. Chances are, if you’re like me, you don’t want to wait 5 minutes for your computer to boot off a DVD, and would rather use a keyboard shortcut at boot. Besides, you’re using fsck either way.
Now, what you want to to is power off your computer, then wait a second or two, then turn it back on. When you hear the boot tone, press and hold ⌘+S to access “Single User Mode”, which is the CLI frontend for Mac OS X. Wait for the text to scroll, then when loading is complete, you will have access to a root shell. You will see a “#” sign. After this, type in the following.
sbin/fsck -fy
If fsck found issues with your filesystem, the following prompt will be displayed after information about the filesystem verification and/or repair.
***** FILE SYSTEM WAS MODIFIED *****
If fsck did find issues, and the message above appears, repeat the fsck command you used earlier until fsck tells you that your volume appears to be OK, as first-pass repairs may uncover filesystem additional issues, so this is a normal and safe thing to do. When your disk is verified as OK, type reboot at the shell prompt. You will now boot into OS X normally.
Disclaimer
I am not responsible in any way for any data loss of any kind, file corruption, or accidental destruction of equipment caused by the article above. The article is provided to the third party “as is” with any warranty of any kind. This information is licensed under the Creative Commons license “Attribution-Noncommercial-Share Alike 3.0 United States License”.

Introduction to Terminal for Disk Maintenance by Josh Crane is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License.
Based on a work at joshcranephoto.com.

No comments yet.