0
0
Linux CLIscripting~5 mins

mount and umount in Linux CLI - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes you need to access files on a storage device like a USB drive or a disk partition. The mount command connects that device to your system so you can use its files. When done, umount safely disconnects it to avoid data loss.
When you plug in a USB drive and want to access its files from your computer.
When you want to access a specific partition on your hard drive.
When you need to temporarily connect a network storage device to your system.
When you want to safely disconnect a storage device after use to prevent data corruption.
When you want to check what devices are currently connected and accessible on your system.
Commands
This command connects the device /dev/sdb1 (like a USB drive partition) to the folder /mnt so you can access its files there.
Terminal
sudo mount /dev/sdb1 /mnt
Expected OutputExpected
No output (command runs silently)
This command checks if the device is successfully mounted by listing all mounted devices and filtering for /mnt.
Terminal
mount | grep /mnt
Expected OutputExpected
/dev/sdb1 on /mnt type ext4 (rw,relatime,data=ordered)
This command safely disconnects the device mounted at /mnt, making sure all data is saved and the device can be removed.
Terminal
sudo umount /mnt
Expected OutputExpected
No output (command runs silently)
This command verifies that the device is no longer mounted by checking the list of mounted devices again.
Terminal
mount | grep /mnt
Expected OutputExpected
No output (command runs silently)
Key Concept

If you remember nothing else from this pattern, remember: mount connects a device to a folder to access files, and umount safely disconnects it when done.

Common Mistakes
Trying to mount a device without using sudo or root permissions.
Mounting devices requires special permissions; without them, the command will fail.
Always use sudo before mount to have the necessary permissions.
Unmounting a device while files are still open or in use.
This can cause data loss or corruption because the system may still be writing to the device.
Close all files and programs using the device before running umount.
Trying to mount a device to a folder that does not exist.
Mount needs a valid folder (mount point) to connect the device; if the folder is missing, the command fails.
Create the mount point folder first using mkdir, for example: sudo mkdir /mnt.
Summary
Use mount with sudo to connect a storage device to a folder and access its files.
Check mounted devices with mount and grep to confirm the device is connected.
Use umount to safely disconnect the device when finished to avoid data loss.