list mounted drives linux with code examples

In Linux, there are a few different ways to list mounted drives. One way is to use the df command, which shows the filesystem usage on your system. Another option is to use the lsblk command, which lists information about all available block devices.

Here are some examples of using these commands to list mounted drives in Linux:

Using the df command

The df command shows the filesystem usage on your system. By default, it shows information about all mounted filesystems. To see only the mounted drives, you can use the -h option, which shows the information in "human-readable" format (i.e. with units like GB or MB). Here's an example:

$ df -h
Filesystem      Size  Used Avail Use% Mounted on
udev            7.8G     0  7.8G   0% /dev
tmpfs           1.6G  9.2M  1.6G   1% /run
/dev/sda1       238G   45G  193G  19% /
tmpfs           7.8G     0  7.8G   0% /dev/shm
tmpfs           5.0M     0  5.0M   0% /run/lock
tmpfs           7.8G     0  7.8G   0% /sys/fs/cgroup
/dev/loop0      89M   89M     0 100% /snap/core18/1705
/dev/loop1      90M   90M     0 100% /snap/core18/1754

In this example, we can see that the root filesystem is mounted on the / path and it is using 238G of disk space and 45G of it is used.

Using the lsblk command

The lsblk command lists information about all available block devices. By default, it shows the device name, size, and whether the device is mounted. Here's an example:

$ lsblk
NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda      8:0    0 223.6G  0 disk 
├─sda1   8:1    0   200G  0 part /
├─sda2   8:2    0    16G  0 part [SWAP]
└─sda3   8:3    0     8G  0 part /home

In this example, we can see that there are three partitions on the disk named sda, sda1, sda2 and sda3. The partition sda1 is mounted at '/' path and it is using 200G of disk space.

You can also use the -o option to specify which information you want to see. For example, to see only the device name and mount point, you can use the following command:

$ lsblk -o NAME,MOUNTPOINT
NAME    MOUNTPOINT
sda    
sda1    /
sda2    [SWAP]
sda3    /home

You can use any of the above command as per your requirement and keep the system organized by managing the mounted drives effectively.

Mounting and unmounting drives in Linux:

In Linux, a drive must be "mounted" before it can be accessed. This means that the operating system connects the drive to a specific point in the filesystem, known as the mount point. Once the drive is mounted, you can access the files and directories on the drive as if they were on your local machine.

To mount a drive, you can use the mount command. The basic syntax is as follows:

mount [options] [device] [mount-point]

For example, to mount a drive located at /dev/sdb1 to the /mnt/new_drive directory, you would use the following command:

sudo mount /dev/sdb1 /mnt/new_drive

To unmount a drive, you can use the umount command. The basic syntax is as follows:

umount [options] [device|mount-point]

For example, to unmount the drive located at /dev/sdb1 that was previously mounted to /mnt/new_drive, you would use the following command:

sudo umount /dev/sdb1

You can also use the umount command with the mount point, for example:

sudo umount /mnt/new_drive

It is important to note that you can only unmount a drive if it is not in use. If a file or directory on the drive is open, you will receive an error message.

Automatically mounting drives at startup:

In order to automatically mount a drive at startup, you need to add an entry to the /etc/fstab file. The fstab file is used by the operating system to determine which drives should be mounted at startup.

Each line in the fstab file represents a single drive and contains a set of options that determine how the drive should be mounted. Here is an example of an entry for a drive located at /dev/sdb1 and mounted to /mnt/new_drive:

/dev/sdb1   /mnt/new_drive   ext4   defaults   0   0

The first column is the device, the second column is the mount point, the third column is the filesystem type, the fourth column is the mount options, the fifth column is the dump frequency and the sixth column is the fsck check order.

You can use the blkid command to find the UUID of the partition. Replace the device name with UUID in the fstab file.

UUID=8c5e5d7d-f5d9-4c8d-bca7-0cfd10a8a21f   /mnt/new_drive   ext4   defaults   0   0

It is important to note that changes to the fstab file take effect at the next reboot. And also, be sure to make a backup of fstab file before making any changes.

In this way, you can manage the mounted drives effectively in Linux, and also can automate the process of mounting the drives at startup.

Popular questions

  1. How can I list all the currently mounted drives in Linux?
  • You can use the mount command without any arguments to list all the currently mounted drives in Linux.
  1. How can I list all the available drives on a Linux machine?
  • You can use the lsblk command to list all the available drives on a Linux machine, including both mounted and unmounted drives.
  1. How can I mount a drive in Linux?
  • You can use the mount command to mount a drive in Linux. The basic syntax is mount [options] [device] [mount-point]. For example, to mount a drive located at /dev/sdb1 to the /mnt/new_drive directory, you would use the command sudo mount /dev/sdb1 /mnt/new_drive.
  1. How can I unmount a drive in Linux?
  • You can use the umount command to unmount a drive in Linux. The basic syntax is umount [options] [device|mount-point]. For example, to unmount the drive located at /dev/sdb1 that was previously mounted to /mnt/new_drive, you would use the command sudo umount /dev/sdb1.
  1. How can I automatically mount a drive at startup in Linux?
  • To automatically mount a drive at startup in Linux, you need to add an entry to the /etc/fstab file. Each line in the fstab file represents a single drive and contains a set of options that determine how the drive should be mounted. For example, an entry for a drive located at /dev/sdb1 and mounted to /mnt/new_drive would be /dev/sdb1 /mnt/new_drive ext4 defaults 0 0. Changes to the fstab file take effect at the next reboot.

Tag

Mounting

Posts created 2498

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Posts

Begin typing your search term above and press enter to search. Press ESC to cancel.

Back To Top