Manage External Hard Drive with Terminal Commands on Mac

The Terminal is a useful application in your macOS that allows you to access macOS features and APIs through the command-line interface. You can open the Terminal app on your Mac by launching Finder from Dock and then navigating to Applications > Utilities > Terminal.


Launch Terminal App

Image: Launch Terminal app

Terminal helps you manage your external hard drive far better than the GUI. All you need to do is enter the required Terminal command, then hit Return. The app will execute the requested command immediately. But, Terminal won’t show any warning while executing any commands.

So, back up your Mac before using Terminal to avoid data loss due to the remove command's accidental use. The backup ensures you can restore data in case of any data loss event. And suppose you didn’t back up your data and executed some unsafe commands that resulted in a data loss disaster. In that case, you can still recover your data using data recovery software.

The next sections describe commonly used Terminal commands and free Mac data recovery software to salvage your lost data.

View Your External Drive Using Terminal

The first thing you would like to do with your external drive is to view it using Terminal. The command line to view the list of all your connected drives is as follows.

diskutil list


diskutil List

Image: diskutil list

This command will return all the mounted or unmounted drives and their partitions. Alternatively, you can look for the external hard drive using the following command:

ls /Volumes

Please note down the external hard drive's identification; a sample DriveID could be /dev/disk2. The correct DriveID is required to manage your external hard drive using Terminal.

Unmount or Mount External Drive Using Terminal

In GUI, you need to plug the external hard drive into your Mac to mount it automatically while drag and drop the drive icon to Trash to eject or unmount it. To remount the drive, you require to unplug the drive and plug it again.

But you can easily unmount and remount your external hard drive using the Terminal command without the need to reconnect the storage device. The Terminal command to unmount an external drive is shown next:

diskutil unmount $DriveID

If the above method doesn’t work, you can force unmount the drive. The Terminal commands to unmount an external hard drive forcefully is shown next:

diskutil unmount force $DriveID


Force Unmount External Drive Terminal

Image: Unmount disk command

To mount the external hard drive once again, enter the following command:

diskutil mount $DriveID


mount external hard drive

Image: Mount disk

The storage drive will mount on your Mac, and you can access all the stored content.

Delete Files/Folders from External Drive Using Terminal

When you remove any file or folder using Terminal, it won’t move to Trash and is removed permanently. So, backup your external hard drive to yet another storage medium to preempt data loss. The Terminal commands to delete a file from an external drive is shown next:

rm $FilePath

Here, replace the $FilePath with the path of the file that you want to remove permanently. In case you want to remove a folder instead of a file, you need to use the remove command recursively. The Terminal commands to delete a folder from an external drive is shown next:


rm-remove file command

Image: use rm command to remove files

rm -r $FolderPath

Replace the $FolderPath with the path of the folder that you want to delete.

Verify and Repair External Drive Using Terminal

When an external hard drive is not working as expected or is corrupt, you can verify it using Terminal and repair it when needed. The command to verify an external hard drive is shown next:

diskutil verifyVolume $DriveID


verify mac external drive terminal

Image: Verify your drive

If the command doesn’t show any message, it means no error is found. Else, you need to repair the Mac drive. The terminal command to repair the external hard drive is as follows:

diskutil repairVolume $DriveID

Repair mac external drive terminal

Image: Repair Mac External Drive

 When the repair succeeds, you can access your storage drive without any issue. Else, erase the drive to create a new file system.

Erase External Drive Using Terminal

You need to be more cautious while erasing your external drive using Terminal. Because the use of the erase command on a wrong drive can result in a data loss disaster. So, back up your data before executing the command. The Terminal commands to erase an external hard drive is shown next:

diskutil eraseDisk $FileSystem $DriveName $DriveID


Erase External Hard Drive

Image: Erase External Hard Drive

Here, replace the entities with the required file system (APFS, JHFS+, HFS+, FAT32, or ExFAT), drive name, and drive identification. The command will erase the external drive and format it to the specified file system. Later, you can restore the backed-up data to the newly formatted drive.

Recover External Drive Data Using Mac Data Recovery Software

When you have accidentally lost data due to unsafe Terminal command, employ free Mac data recovery software to recover it on Mac. The steps are as follows:

  • Install and launch Stellar Data Recovery Free Edition for Mac on your MacBook Air/Pro, iMac (Pro), or Mac mini.
select external drive recovery

Image: Recover external drive

  • Watch this video guide to recover your lost data on Mac running on macOS Big Sur or earlier.
*Stellar Data Recovery Free Edition for Mac is useful for scanning your Mac external drive, previewing files, and recovering up to 1 GB of files without any cost. To recover more,upgrade the software to Professional or Premium edition. Also, the software offers a 30-day money-back guarantee, just in case.

Conclusion

Now you know how to manage your external hard drive with Terminal commands on Mac. You can view, unmount, mount, repair, and erase your external hard drive or delete files from it. In case of accidental data loss, use a free data recovery software for Mac to recover your deleted or lost files.



Was this article helpful?
About The Author
author image
Santosh Kumar Gupta linkdin Icon

Data recovery expert on Mac & Windows platforms, with 10-year experience in writing

Table of Contents

WHY STELLAR® IS GLOBAL LEADER

Why Choose Stellar?
  • 0M+

    Customers

  • 0+

    Years of Excellence

  • 0+

    R&D Engineers

  • 0+

    Countries

  • 0+

    PARTNERS

  • 0+

    Awards Received