How to format hard drive using terminal Mac?

Formatting a drive using the Terminal on a Mac provides a simple way to completely erase and reconfigure a disk from the command line. While disk utility provides a graphical interface, using Terminal gives you more control and allows you to securely erase data if needed.

Knowing how to format drives from Terminal is useful for several reasons:

– You can format disks even if you don’t have access to the graphical Disk Utility interface

– You can customize options like file system type and scheme that may not be available in Disk Utility

– You can use advanced features like secure erase to thoroughly delete sensitive data

– Automating disk tasks is easier from the command line interface

– Troubleshooting disk issues may require lower-level Terminal access

This guide will walk through the basic process of identifying, unmounting, formatting, and remounting a disk using the flexible “diskutil” command in Terminal on a Mac.

Prerequisites

Before formatting a hard drive using the command line on a Mac, there are a couple prerequisites to ensure the process goes smoothly:

You need admin access to format a drive. Formatting erases all data, so it requires admin privileges. Make sure you are logged into an admin account on your Mac before proceeding.

Ensure there is no critical data on the drive you want to format. Formatting erases all contents, so backup any important files first. Once a drive is formatted, prior data cannot be recovered.

If the drive is your startup disk, you’ll need to boot from another drive to format it. You cannot erase the active startup disk.

Have the correct disk identifier ready for the drive you want to format. This is used to specify the correct disk in the terminal command.

Step 1 – Identify the Drive

The first step is to identify the external drive you want to format. To do this, open the Terminal app and type the following command:

“diskutil list”

This will show all available drives connected to your Mac, along with their identifiers. Look for the external drive you want to format, and note its identifier. It will look something like “disk2” or “disk3”. The disk identifiers go in numerical order based on when the drives were connected to your Mac.

For example, your Mac’s main internal drive is usually “disk0”, so an external drive connected after that will be “disk1”, the next external drive will be “disk2”, and so on. Identify the disk number for the external drive you want to format.

For more details on finding disk identifiers, see this guide: How to Find a Disk ID & Device Node Identifier in Mac.

Step 2 – Unmount the Drive

Before formatting the drive, you need to unmount it. Unmounting a drive detaches it from the file system to allow changes to be made. To unmount a drive from the command line, use the diskutil unmountDisk command.

For example, to unmount disk2:

diskutil unmountDisk /dev/disk2

If the drive fails to unmount due to it being in use, you can force unmount it with:

diskutil unmountDisk force /dev/disk2

This will forcibly unmount the drive, even if it is currently in use (make sure to save your work first). Unmounting the drive is an essential step before formatting to avoid data loss.

Source: https://osxdaily.com/2013/05/13/mount-unmount-drives-from-the-command-line-in-mac-os-x/

Step 3 – Format the Drive

To format the drive, we will use the diskutil eraseDisk command in Terminal. This allows us to erase and format the drive from the command line.

Some common volume formats to choose from are APFS (Apple File System) and Mac OS Extended (HFS+). For example:

diskutil eraseDisk APFS "My Drive" /dev/disk2

This will format the drive at /dev/disk2 with APFS and name it “My Drive”. Replace APFS with “Journaled HFS+” to format as Mac OS Extended instead.

According to OS X Daily, the full syntax for diskutil eraseDisk is:

diskutil eraseDisk Format Name Identifier

Where Format is APFS, Mac OS Extended, etc, Name is the volume name, and Identifier is the disk path like /dev/disk2.

This will perform a complete erase and format of the drive to prepare it for use with the chosen volume format.

Step 4 – Rename the Drive

After formatting the drive, it’s a good idea to give it a descriptive name so it’s easy to identify. This can be done using the diskutil rename command in Terminal.

The syntax is:
diskutil rename diskX NewName

For example:

diskutil rename disk2 MyExternalDrive

Will rename “disk2” to “MyExternalDrive”.

To find the disk identifier, you can use the diskutil list command again to see a list of all disks and their identifiers.

Giving the newly formatted drive a descriptive name like “MyExternalBackup” or “TimeMachineBackup” makes it easy to identify the correct disk when working in Terminal or the Finder. Avoid generic names like “disk2” or “untitled” that don’t provide any context.

For more details on renaming drives using Terminal, see this Apple support article: Rename files, folders, and disks on Mac.

Step 5 – Mount the Formatted Drive

Once the drive has been formatted, we need to remount it so that it becomes accessible in the Finder again. To do this, we’ll use the diskutil mountDisk command.

The syntax is:

diskutil mountDisk /dev/diskX

Where /dev/diskX is the identifier for your external drive. This can be found by running diskutil list and looking for the external drive.

For example:

diskutil mountDisk /dev/disk2

After entering this command, your external drive should remount and be accessible once again in the Finder (provided the mount process completed successfully).

You can confirm it is available by opening a new Finder window and looking for it in the sidebar under Devices.

Step 6 – Verify Successful Format

After formatting the drive, the next step is to verify that the process completed successfully. This can be done in a few ways through Finder:

First, open a new Finder window and look for the drive in the sidebar under Devices. It should show the new name you gave it during formatting. Click on it to view details.1

In the Get Info window, check that the drive shows the expected capacity and format type (such as Mac OS Extended). If so, the formatting was completed properly.2

You can also open Disk Utility again and select the drive from the sidebar. In the main window, it should show the drive name, format, and full capacity. If everything looks correct here, the formatting via terminal was successful.

Visually verifying the drive details in Finder is an easy way to confirm the formatting process worked as expected before storing data on the newly formatted volume.

Troubleshooting

Formatting a hard drive via the terminal does not always go smoothly. Here are some common issues and solutions:

If you receive an error while trying to unmount the drive such as “Resource busy” or “Device busy”, make sure all windows and applications accessing the drive are closed. You can use the lsof | grep /Volumes/DRIVE command to identify processes using the drive. Quit these applications before retrying the unmount command.

If the format process seems excessively slow, it could indicate bad sectors or physical issues with the drive. You can stop the format with Ctrl-C and run diskutil verifyVolume /Volumes/DRIVE to scan for errors. Consider replacing the drive if there are multiple issues reported.

In some cases, the newly formatted drive may not automatically mount and appear in Finder after the format finishes. Run diskutil list to verify if the drive appears. If so, mount it manually with diskutil mountDisk /dev/diskX substituting X with the disk number. If it is still not listed, disconnect the drive and reconnect it to force remounting.

For additional troubleshooting tips, refer to this guide or Apple’s support documentation on using Terminal.

Conclusion

Formatting a hard drive in Mac using the Terminal provides a simple yet powerful way to prepare storage devices for usage. By following the step-by-step process outlined, the drive can be fully erased and reformatted to work optimally with a Mac system.

The key steps covered include identifying the target drive, unmounting it from the system, selecting the desired format, renaming, remounting and verifying successful completion. Executing these commands in Terminal gives users direct control over the format process.

Compared to using the Disk Utility app, Terminal offers a more advanced way to manage drives. Key advantages are the ability to rename, the choice of format types and the flexibility to customize options. With some practice, Terminal formatting becomes quick and efficient. Users gain technical skills in the command-line interface as well.

Overall, formatting a hard drive via Terminal on a Mac is a worthwhile approach for tech-savvy users to optimize storage devices and enhance their command-line abilities. The steps outlined above provide a clear methodology to achieve successful results.