How to eject a CD/DVD from the command line

172,945

Solution 1

In order to eject a disk from the drive, whether it's a CD or a DVD, open a terminal and simply execute the eject command.

Solution 2

To open the CD drive / eject the CD:

  • Open Terminal using Ctrl+Alt+T, and type eject
  • To close the tray, type eject -t
  • And to toggle (if open, close and if closed, open) type eject -T

All these commands can be typed into the run dialogue (Alt+F2)

For more options, type eject -h into Terminal.

Solution 3

Opening the Tray

Commands:

  • open tray: eject
  • close tray: eject -t

Easy Function for .bashrc

alias opentray='eject'

A few issues arise when ejecting drives. Sometimes they don't want to eject, because they are mounted etc. You can override this with eject -l /media/mountpoint or (/mnt/mountpoint). I wrote a function that can be called by simply typing opentray on your command line.

Notice

This works only if

  • you setup a permanent mount point for your drive /dev/sr0 (same thing as /dev/cdrom, which is just symbolically linked to /dev/sr0)
  • your mount point is automatically created when you insert a disk into the drive. (This can be ignored if you remove/comment out all lines where rm -r "${mountdir}" exists that way the mount point will never be removed automatically)
  • Must run as root unless you changed the permissions manually of mounting functions (I have never tried this)

function opentray ()
{
    mountdir="/media/DVD"
    if [ -d "${mountdir}" ] # If directory ${mountdir} exists
    then
        if [ $(mount | grep -c "${mountdir}") = 1 ] # If drive is mounted, then
        then
            echo "/dev/sr0 is now mounted to ${mountdir}. I'll try to unmount it first and eject/open the tray."
            umount -l "${mountdir}"
            rm -r "${mountdir}"
            sysctl -w dev.cdrom.autoclose=0 # Ensure drive doesn't auto pull tray back in.  
            eject
            exit
        else
            echo "/dev/sr0 is not mounted. Opening the tray should be easy. Ejecting/opening now."
            rm -r "${mountdir}"
            sysctl -w dev.cdrom.autoclose=0 # Ensure drive doesn't auto pull tray back in.  
            eject
            exit
        fi
    else
        echo 'The directory "${mountdir}" does not exist. Ejecting/opening the tray.'
        sysctl -w dev.cdrom.autoclose=0 # Ensure drive doesn't auto pull tray back in.
        eject
        exit
    fi
}

Closing the Tray

For completeness, you can add this alias to your .bashrc ( or .bash_aliases file) to pull the tray back in from the command line. You do not need to be root.

alias closetray='eject -t'

Solution 4

The eject command might fail if it does not recognize your external cdrom drive. In that case, you will have to identify the /dev device manually and run the explicit command. A good candidate if you have an external USB drive is

eject /dev/sr0

Solution 5

The man page for eject on Ubuntu contains no force options (either -F or --force).

You may eject a "busy" (in use) DVD:

eject -m

This worked for me to replace a defective dvd with a freshly burned one to continue an installation.

Share:
172,945

Related videos on Youtube

Kulfy
Author by

Kulfy

Born in June, 2015, I am a 5 year old pug (good boi?), whose name is derived from a dessert (Not to be confused with @dessert). I use Ubuntu 16.04, 18.04 and 20.04 with my little paws (believe me). I generally give instructions to my master to write up answers or ask questions as I dictate him (typing could be boring sometimes). For hugs and pugs ping me @ AUGR or ROLD or [email protected]. Being a pug I love Kisses. ;-) Never let them know where your food is 😉

Updated on September 18, 2022

Comments

  • Kulfy
    Kulfy almost 2 years

    I've just right clicked on the DVD icon in the Unity Launcher in order to eject it, but instead of hitting the 'Eject' button, I missed and hit the 'Unlock from Launchpad' option instead.

    How do I go about ejecting the disk from the drive now that the Launcher option is missing?

  • Sanam Patel
    Sanam Patel over 11 years
    Does the icon come back next time you insert a disc?
  • David Tonhofer
    David Tonhofer over 11 years
    The icon does not actually come back for that particular disk. A similar one will appear, at a lower resolution and without the eject option, and the full one will appear when you insert a new disk. I'm not sure how to undo this - I'm still looking into that.
  • marto
    marto over 11 years
    I think you need too add the device after the eject command, like eject /dev/sr0 --- It seems the default symlink /dev/dvd is not created anymore by default, and "eject" only works for /dev/dvd when entered without parameters
  • Sopalajo de Arrierez
    Sopalajo de Arrierez over 5 years
    Working on Ubuntu 16.04 via remote shell (SSH). It requires sudo.
  • Victoria Stuart
    Victoria Stuart over 5 years
    eject worked for me then got stopped working, with the following error: "eject: /dev/cdrom: not found mountpoint or device with the given name". After executing eject /dev/sr0 I could again use eject, eject -T etc.
  • karel
    karel about 5 years
    eject --forceeject: unrecognized option '--force'
  • Alberto Salvia Novella
    Alberto Salvia Novella about 5 years
    For older kernels: eject -F
  • Paul
    Paul about 4 years
    The --force doesn't work for me on Ubuntu 18.04, but eject -v /dev/sr0 does give usefull information. (or eject -h)
  • Admin
    Admin almost 2 years
    THIS even works for an Apple Superdrive, that wont eject the disc under MacOS.