CentOS

How to mount USB drive in CentOS

A USB drive, also known as a USB flash drive or a pen drive, is a widely used external storage device that can be utilized to back up or transfer data from one system to another. As the file system is supported in a GUI Operating System such as Windows and GNOME Desktop in CentOS, which helps you to immediately mount a USB and access its data. However, most CentOS users prefer to mount the USB drive using the command-line method as they are used to working around the terminal. If you are one of them, you have to manually mount the USB drive in your CentOS terminal to access the USB data.

This post will demonstrate to you how to mount a USB drive in CentOS. So, let’s start!

How to mount USB drive in CentOS

First of all, plug in the USB drive into any of the available ports of your system:

How to detect USB drive in CentOS

CentOS automatically creates a new block device when an external USB storage device is connected to a system in the “/dev/” directory. Still, you cannot access the data present in the USB before mounting it. To mount the USB drive, you have to know the name of the block device created on your system. For this, we will utilize the “fdisk” command.

For Linux-based systems such as CentOS, “fdisk” is the widely used command-line-based disk manipulation utility. “fdisk” is an acronym for “format disk” or “fixed disk”. You can utilize the fdisk command for creating, copying, resizing, altering, resizing, and transferring partitions on a hard drive. In our case, we will execute the fdisk command to get the details about the device block created for the plugged in USB drive:

$ sudo fdisk -l

In the above-given fdisk command, the “-l” parameter is added to list all system partitions. The device names are used to display the partitions. For instance, /dev/sda, /dev/sdb, and /dev/sdc:

From the output, you can see that CentOS created the “/dev/sdc” for our USB drive having 3.8 GB storage:

How to create a mount point in CentOS

Now, we know about the name of the device block of our USB drive. Before moving towards the mounting procedure, we will create a mount point for the plugged USB drive. In our root filesystem, we will create a “usb” Directory as a mount point.

For this purpose, we will utilize the “mkdir” command in the CentOS terminal. The “mkdir” command permits a user to create single or multiple directories in their filesystem. To create a “usb” directory, we will execute the below-given command in our terminal:

$ sudo mkdir /usb

How to mount USB drive in CentOS

After successfully creating the mount point, we can now easily mount the USB drive. According to the “fdisk” command, the name of the device block for our USB drive is “/dev/sdc1”. The “mount” command is utilized in the CentOS terminal for mounting the filesystems and removable devices such as external hard drives or USB drives.

Here is the syntax of the mount command:

$ sudo mount [Device_Block_Name] [Mount_Point_Path]

For our USB drive, the name of the device block is “/dev/sdc1” and its mount point is “/usb”:

$ sudo mount /dev/sdc1 /usb

How to verify mounted USB drive in CentOS

The “grep” command is used to search for a specific string or pattern. The “grep” command will take the output of the “mount” command as input, by utilizing the pipe operator “|”. In between the whole filesystem related to the external plugged-in devices, the grep will look for the “sdc1” device block:

$ sudo mount | grep sdc1

The execution of the above-given command will verify if the USB drive is mounted or not:

Another method to verify the mounted USB on your CentOS system is to use the “df” command. The “df” is an acronym for “disk filesystem”. It is utilized for getting the summary of used and available disk space of the filesystem.

You can add the combination of the “-HT” options in the “df” command to display the size and type of the filesystem:

$ sudo df -HT

How to access data of USB drive in CentOS

After successfully mounting the USB drive in the mount point, you can now access the USB drive. In this section, we will demonstrate to you the procedure of accessing data from the mounted drive. Firstly, execute the “cd” command to navigate into the “mount point” or “mount directory,” which is “/usb” in our case:

$ cd /usb

For viewing the content of the mounted USB drive, execute the “ls” command in your CentOS terminal:

$ ls

The output will list the names of all files and directories present inside the USB drive:

How to delete a directory from the mounted USB drive in CentOS

As the mounted directory “/usb” is our current working directory. So now, we will utilize the “rmdir” command for deleting a directory from our USB drive:

$ sudo rmdir casper

Again, execute the “ls” command to check if the directory is deleted or not:

$ ls

How to unmount USB drive in CentOS

The “umount” command is used for unmounting any filesystem. For instance, to unmount a USB drive, we will add the device block name of the mounted drive in the “umount” command:

$ sudo umount /dev/sdc1

Conclusion

When you plug in a USB drive in your CentOS system, it normally mounts automatically. After that, you can utilize the file manager on your system to access its data. Unfortunately, in some cases, you may need to mount the USB through the CentOS terminal manually. This post demonstrated how to mount a USB drive, access USB data, and delete any directory from the mounted USB drive. Moreover, the procedure for unmounting the USB drive is also provided to you.

About the author

Sharqa Hameed

I am a Linux enthusiast, I love to read Every Linux blog on the internet. I hold masters degree in computer science and am passionate about learning and teaching.