- Unix Commands Reference
- Unix Commands - Home
eject Command in Linux
The eject command in Linux is a command-line utility that allows us to physically remove or eject a removable device, such as a CD-ROM or USB drive. This command can unmount the device before ejecting it, which helps us to prevent data loss by ensuring that all data operations are complete before the device is removed.
The eject command in Linux can also be used to control some multi-disc CD-ROM changers, manage the auto-eject feature on supported devices, and close the disc tray of certain CD-ROM drives.
eject Command in Linux
The eject command can handle several devices, such as CD-ROMs, SCSI, removable floppies, and tape drives, by using different methods to eject them. It attempts each method in sequence until it succeeds. If the device is mounted, the command unmounts it before proceeding with the ejection.
By default, the eject command is pre-installed on most Linux distributions and can be used by following the below-given syntax −
eject [...OPTIONS]
Options are optional parameters that let us modify the command's behavior, such as specifying the device, controlling tray movement, displaying help, etc.
Manual Page of eject Command
To access the manual page for the eject command and get an in-depth overview, you can run the following command −
man eject
The manual page shows detailed information about the eject command, including its options and usage −
Common Options and Usage of eject Command
The eject command accepts several options to perform specific tasks, such as ejecting a device forcefully, controlling tray movement, etc. Here are some commonly used options along with their usage −
-h or --help Option
Run the eject command with the -h option to get a detailed overview of this command, including command usage and its valid options −
eject -h
-v or --verbose Option
The verbose or v option logs out the detailed information about the command’s execution −
eject -v
-V or --version Option
To check the installed version of the eject command, we can run the eject command with the -V or --version options, as shown below −
eject --version
d or --default Option
Execute the eject command with the d or --default option to get the name of the default device name −
eject --default
-a Option
The “-a on|1|off|0 or –auto on|1|off|0” is supported by some devices and is used to control the auto-eject mode. Set this option to "on" or "1" to enable auto-ejection when the device is closed, or to "off" or "0" to disable it −
eject -a on
This command will enable auto ejection.
--c or –changerslot Option
Use the -c or --changerslot option with the eject command to select a specific CD slot from an ATAPI/IDE CD-ROM changer. This feature works on Linux 2.0 or later versions. To select a specific CD slot, ensure that the slot is not already occupied or in use. The slots are numbered starting from 0, with the first available slot referred to as 0 −
eject --changerslot SLOT_NUMBER
Replace the “SLOT_NUMBER” with a valid slot number. This command will select the specified CD slot on an ATAPI/IDE CD-ROM changer.
-t or –trayclose Option
We can use this option with the eject command to send a tray-closing request to the drive. However, it is important to note that hardware support for this feature may be limited −
eject -t
-T or –traytoggle Option
We can run the eject command with the -T or -traytoggle option to close the tray if it is open or eject it if it is closed −
eject -T
The hardware support for this option is also limited.
-x or --cdspeed Option
This option is used to set the CD-ROM drive’s select speed. For this purpose, you must specify a number to set the desired speed (e.g., 2 for 2X speed), or use 0 for the maximum data rate.
eject -x 0
The device support for this feature may be limited, and you can only select speeds that the drive supports. The setting resets whenever you change the media. We can also combine this option with the -c or -t options.
-X or –listspeed Option
We can use this option to display a list of available speeds from the CD-ROM drive. After this, we can use these listed speeds as arguments for the -x option −
eject -X
This feature requires Linux 2.6.13 or later; on older versions, it only reports the maximum speed. Additionally, some drives may not accurately report available speeds, so this option might not work with all drives.
-n or –noop Option
We can use this option to show information about the selected device without performing any action −
eject -n
-r or –cdrom Option
This option instructs the eject command to treat the device as a CD-ROM. It can be useful for ejecting the CD-ROM tray or handling specific CD-ROM functionalities −
eject -r
-s or –scsi Option
The -s or --scsi option directs the eject command to treat the device as a SCSI device. This can be useful for managing SCSI CD-ROM drives or handling specific functionalities related to SCSI devices −
eject -s
-f or –floppy Option
We can use this option to issue a removable floppy disk eject command for the drive −
eject -f
-q or –tape Option
We can run the eject command with the -q option to issue an offline command for ejecting a tape drive −
eject -q
-p or –proc Option
This option directs the eject command to use /proc/mounts instead of /etc/mtab for determining mounted file systems. Additionally, it passes the -n option to umount −
eject -p
-m or –no-unmount Option
This option prevents the eject command from unmounting the device before ejecting it −
eject -m
That’s all about the use of the eject command in Linux.
Conclusion
The eject command in Linux is used to remove or eject removable devices such as CD-ROMs, floppy disks, and tape drives. It can unmount devices before ejection, manage multi-disc changers, and control tray movements.
In this tutorial, we explained the eject command, its syntax and various options, including -r for CD-ROMs, -t for closing trays, -c for selecting CD slots, and -m to skip unmounting. Practical examples and the manual page were used to demonstrate how to use these options effectively.