The quickest way to eject media from the command line is to use the eject command on Linux/macOS (e.g., eject /dev/sr0) or the diskutil eject command on macOS (e.g., diskutil eject /dev/disk2). On Windows, use wmic path win32_cdromdrive where "Drive='D:'" call eject or PowerShell’s (New-Object -com Shell.Application).Namespace(17).ParseName("D:").InvokeVerb("Eject"). This article explains the commands, how they work, and how to handle common problems.
Background: Why Eject from the Command Line?
Most users eject media by clicking the icon in the file manager or pressing the physical button on the drive. But there are times when the command line is faster or necessary:
- You are working over SSH and need to eject a disc on a remote server.
- The graphical interface is frozen or not available (e.g., a headless server).
- You want to automate the ejection of multiple drives or schedule it.
- You are writing a script that needs to unmount and eject after a backup or burn.
The command line gives you direct control, and the tools are already present on most systems.
How the eject Command Works (Linux)
On Linux, the eject command is part of the util-linux package, which is installed by default on most distributions. It works by sending a SCSI START/STOP command to the device, which tells the drive to open its tray (or eject a disc).
Basic usage:
- Find your device name:
lsblkorblkidwill list block devices. Optical drives are usually/dev/sr0,/dev/sr1, etc. - Eject it:
eject /dev/sr0 - If the drive is mounted, you may need to unmount first:
eject -m /dev/sr0(the-mflag unmounts then ejects).
You can also eject by label or mount point: eject /media/cdrom or eject LABEL=MYDISC.
To close the tray (on drives that support it): eject -t /dev/sr0. To toggle (eject if closed, close if open): eject -T.
macOS: Using diskutil
macOS does not include the eject command by default, but it has diskutil, which is more powerful and safer for external drives.
To eject a disk:
- List disks:
diskutil list - Eject by device node:
diskutil eject /dev/disk2 - Eject by volume name:
diskutil eject "My USB"
Note: diskutil eject unmounts and then ejects the media. If you only want to unmount (so you can remount later), use diskutil unmount. For a CD/DVD, diskutil eject will open the tray.
If you prefer the classic eject syntax, you can install eject via Homebrew, but diskutil is the native, reliable way.
Windows: PowerShell and WMIC
Windows does not have a simple eject command, but you can use PowerShell or WMIC.
PowerShell (most reliable):
(New-Object -com Shell.Application).Namespace(17).ParseName("D:").InvokeVerb("Eject")
Replace D: with your drive letter. This uses the Windows Shell COM object to trigger the same eject action as the context menu.
WMIC (older, but still works on many systems):
wmic path win32_cdromdrive where "Drive='D:'" call eject
For USB drives, the PowerShell method works, but you may need to unmount first: mountvol D: /p (this dismounts the volume).
Choosing the Right Command
Which command you use depends on your OS and what you are ejecting:
- Linux + optical drive:
eject /dev/sr0– simple and direct. - Linux + USB drive:
eject /dev/sdbworks, but it’s better to unmount first withumount /dev/sdb1and theneject /dev/sdbto power off the device safely. - macOS + any drive:
diskutil eject /dev/disk2– handles unmounting and ejecting. - Windows + any drive: PowerShell COM method – no extra tools needed.
If you are unsure of the device name, always list first: lsblk (Linux), diskutil list (macOS), Get-Volume (PowerShell).
Common Problems and Solutions
Here are typical issues you might face:
- “Device or resource busy” – The drive is still mounted or in use. Unmount first:
umount /dev/sr0(Linux) ordiskutil unmount /dev/disk2(macOS). - “Cannot eject because it is in use” (Windows) – Close any programs using the drive, or run PowerShell as Administrator.
- No
ejectcommand found (Linux) – Install it:sudo apt install eject(Debian/Ubuntu) orsudo dnf install util-linux(Fedora). - Drive does not open – Some drives have a mechanical lock; try
eject -r(Linux) to use the CD-ROM ioctl, ordiskutil ejectagain. If it still fails, the drive may be faulty. - USB drive does not appear – Check
lsusb(Linux) orsystem_profiler SPUSBDataType(macOS) to see if the system sees it at all.
Safety and Best Practices
Always unmount before ejecting to avoid data corruption. The eject command on Linux will not unmount automatically unless you use -m. On macOS, diskutil eject does unmount first. On Windows, the PowerShell method does not unmount, so you should close all files and folders first.
For external drives, it’s better to use eject (Linux) or diskutil eject (macOS) because they also power off the device, which is gentler on the hardware. Simply unplugging without ejecting can cause filesystem errors.
Frequently Asked Questions
Can I eject multiple drives at once?
Yes, you can write a loop. In Linux: for dev in /dev/sr0 /dev/sr1; do eject $dev; done. In PowerShell, you can loop over drive letters. It’s a common script for media centers.
What is the difference between unmount and eject?
Unmounting detaches the filesystem from the system, making it safe to remove. Ejecting goes further: it stops the device (spins down the disc, opens the tray, or powers off the USB device). You can unmount without ejecting (e.g., to remount later), but ejecting implies unmounting first.
Does eject work on USB flash drives?
Yes, on Linux and macOS, eject and diskutil eject work on USB drives, but they may just unmount and power off the device. On Windows, the PowerShell method works for USB drives as well, but you may need to use mountvol /p to dismount first.
Why is there no eject command on Windows?
Windows never had a built-in command-line tool for ejecting media. The COM method via PowerShell is the standard workaround. Alternatively, you can use third-party tools, but the PowerShell one-liner is safe and does not require installation.
Can I eject a disc from a remote Linux server?
Yes, if you have SSH access, you can run eject /dev/sr0 on the remote machine. This is useful for servers with optical drives that are not physically accessible.
What if my drive does not have a tray (slot-loading)?
The same commands work – they send the eject signal to the drive. For slot-loading drives, the disc will be pushed out partway, and you can pull it out manually.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.
More cards in this drawer
- Campus Tech Innovation Hubs: What They Are
- Poco C51 Airtel Prepaid Locked Variant India?
- Jenkins vs GitHub Actions: Which CI/CD Fits Your Team?
- Turn Off Backup Encryption in iTunes?
- Shared VPS Hosting: Is It Better?
- Disk Defrag Pro: What It Does and Whether You Need It