0

I prefer to work as much as possible at the command line. There's this neat trick that Nautilus does that ought to be do-able from the command line, but I can't figure out how.

I plug in an external disk into my USB port. It has an exFAT partition with the name "200503exfat". I gave the partition that name, if I remember correctly, using the macOS Disk Utility app. I want to mount the partition in Linux, and I want to use the partition name as the mount point name. I can do that like so:

$ lsblk | grep sd
sda           8:0    0 931,5G  0 disk 
├─sda1        8:1    0   200M  0 part 
└─sda2        8:2    0 931,3G  0 part  # Must be this partition. What's its name?
$ sudo mkdir /media/myusername/200503exfat  # I happen to know the name but I...
$ sudo mount /dev/sda2 !$
sudo mount /dev/sda2 /media/myusername/200503exfat #...don't want to hard code it.
FUSE exfat 1.2.8
$

I want to put this into a bash script, but I want the script to DISCOVER the partition name (here 200503exfat). And there's the rub. If I fire up Nautilus, bingo, I see the name "200503exfat" in the left pane. So if Nautilus can do it, it ought to be do-able at the command line, right?

This seems like a question asked a zillion times before, but I can't find the answer. I've looked at lsblk, fdisk, sfdisk, parted ... and of course google.

TIA.

P.S. For optional extra credit ... how does Nautilus know that /dev/sda2 is mountable but /dev/sda1 is not?

2 Answers 2

3

Typing blkid in the terminal will show the labels along with other good info. Some of mine are not labeled.

$ blkid
/dev/sda1: LABEL="Windows RE tools" UUID="8222C2A122C29995" TYPE="ntfs" PARTLABEL="Basic data partition" PARTUUID="9a07eb60-62a1-4531-a6ea-332b76c5f25c"
/dev/sda2: LABEL="SYSTEM" UUID="04C5-0D79" TYPE="vfat" PARTLABEL="EFI system partition" PARTUUID="120686dc-3078-4c71-b9e9-602f70ae69b1"
/dev/sda4: LABEL="Windows" UUID="B60AEC350AEBEFF3" TYPE="ntfs" PARTLABEL="Basic data partition" PARTUUID="3d050941-d614-4139-a9d8-9be3afec194b"
/dev/sda5: UUID="261A4F451A4F10EB" TYPE="ntfs" PARTUUID="9527964a-811b-4595-9d90-e105c0e7bfc6"
/dev/sda6: LABEL="Old_D" UUID="1e257c3b-a275-4b87-b6dc-bbfb945b8e5c" TYPE="ext4" PARTLABEL="Basic data partition" PARTUUID="55d4f90c-6da8-4650-aa37-744c0a338f2d"
/dev/sda7: LABEL="Recovery image" UUID="72D8CB52D8CB1371" TYPE="ntfs" PARTLABEL="Basic data partition" PARTUUID="81d7a341-55f3-4c4a-8409-fad88cbebaa5"
/dev/sdb1: UUID="0ca22edb-e6b0-412b-ad6e-00b2126b37c7" TYPE="ext4" PARTUUID="84e30647-bc14-4ca7-ba96-441ce8cd19de"
/dev/sdb2: UUID="adc97911-6d93-4094-8ec4-15cab05377e7" TYPE="swap" PARTUUID="4df44d3e-e24c-4130-aa21-5f2a63cff165"
/dev/sdb3: LABEL="home" UUID="e98fac97-277b-4e8a-907a-0f1bf4f428e6" TYPE="ext4" PARTUUID="de2793a9-1046-4e88-ad4a-56b389519cf3"
/dev/sdc1: LABEL="Old_E" UUID="1f0d243f-9dcd-41e8-b9da-2f75b0e3c356" TYPE="ext4" PARTUUID="2203843a-8ddf-8c44-600c-fc025d375992"
/dev/sdc2: LABEL="Old_F" UUID="27b594c2-4392-45ba-9d0f-fd586189d886" TYPE="ext4" PARTUUID="846cb5b3-1c37-f49f-0b74-5ab5300bc914"

lsblk with options works too.

$ lsblk -o model,size,name,fstype,label,mountpoint 
MODEL              SIZE NAME   FSTYPE LABEL            MOUNTPOINT
TOSHIBA DT01ACA1 931.5G sda                            
                   800M ├─sda1 ntfs   Windows RE tools 
                   260M ├─sda2 vfat   SYSTEM           /boot/efi
                   128M ├─sda3                         
                 149.7G ├─sda4 ntfs   Windows          
                   350M ├─sda5 ntfs                    
                 759.3G ├─sda6 ext4   Old_D            /media/Win-D
                    21G └─sda7 ntfs   Recovery image   
WDC WD30EFRX-68E   2.7T sdb                            
                  73.2G ├─sdb1 ext4                    /
                  48.8G ├─sdb2 swap                    [SWAP]
                   2.6T └─sdb3 ext4   home             /home
Hitachi HDS5C404   3.7T sdc                            
                   1.7T ├─sdc1 ext4   Old_E            /media/Win-E
                     2T └─sdc2 ext4   Old_F            /media/Win-F
RAM GH95N         1024M sr0                            
4
  • 1
    Problem solved! Thank you! (For benefit of other readers: I had to say "/sbin/blkid" because /sbin is not in my search path. Maybe I'll add it.) Anyway thanks again. Perfect answer.
    – Ken
    May 10, 2020 at 21:45
  • Thanks @Ken. As you are a new user, I'll point out that the proper procedure for accepting an answer is to click the gray checkmark beside it. Feel free to upvote it too :) May 10, 2020 at 21:46
  • LABEL is file-system-name, while PARTLABEL is partition-name. PARTLABEL is only available for partitions on drives with GPT.
    – mook765
    May 10, 2020 at 22:35
  • @Organic Marble - Thanks. I knew about clicking the grey checkmark. I just hesitated a few minutes. But I don't see how to "upvote" your answer.
    – Ken
    May 11, 2020 at 9:32
0

I said I wanted to write a bash script. Here it is, with special thanks to @Organic Marble who solved my roadblock ...

#!/bin/bash

# Discover and mount any and all un-mounted and labelled /dev/sdxx partitions.
# Special case: Ignore partitions named "EFI". These are system partitions.
# Mount point will be /media/$USERNAME/<partition-name>/ .
# If any given partition is already mounted, skip it.
# If there are no partitions to mount, do nothing silently and successfully.

# Caveat: I've written this for personal use only.  I am posting this code
# one hour after I wrote it, with minimal testing, so beware. That said, it
# does seem to work for me as intended.

# I welcome any comments or suggestions.

process_blkid_lines() {
    echo $blkid_line | grep -q LABEL= || continue  # no label? => skip
    label=$( echo $blkid_line | sed 's/^.* LABEL="//' | sed 's/".*//' )
    [[ -z "$label"      ]]     && continue  # no label?         => skip
    [[ "$label" = "EFI" ]]     && continue  # system partition? => skip
    dev_path=$( echo $blkid_line | awk -F: '{print $1}' )  # eg /dev/sda2
    mount | grep -q ^$dev_path && continue  # already mounted?  => skip
    mount_point=/media/$USERNAME/$label
    sudo mkdir -p $mount_point
    sudo mount $dev_path $mount_point
}

# On my system, blkid lives in /sbin/.
# I added /sbin to my $PATH to avoid "command not found".
# If you prefer, you could just change "blkid" to "/sbin/blkid".
sudo blkid | grep ^/dev/sd | process_blkid_lines

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .