How to eject a CD/DVD from the command line
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.
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.
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'