Simple solution
This seems to be an XY problem. Since you only have 2 disks like that, you can get the name of the drive without even knowing it.
See, you are in the first hard drive, so it's mounted at /
. Your other hard drive is mounted at /media/my_user/something
. Therefore, there is only one directory under /media/my_user
. You can use the wildcard *
to expand to it:
cd /media/my_user/* # Enter that drive
cd /media/my_user/*/home # Enter a subfolder
To get the "quick cd alias" you wanted:
# Put the following in .bashrc:
alias cdthatdrive='cd /media/my_user/*'
# Then after relogging and opening a new terminal:
cdthatdrive # to use that alias
If you still need the random name:
RANDOM_NAME="$(basename /media/my_user/*)"
alias cdthatdrive="cd /media/my_user/$RANDOM_NAME"
Advanced solution
The simple solution above is preferred if it's sufficient for your use case. Only once you encounter the need for supporting 3 or more drives, you can use a more comprehensive solution:
udisksctl
interacts with the daemon that mounts disks on modern Linux. The GUI more or less calls something like udisksctl mount -b /dev/sdb
except using DBus. So run udisksctl dump
and press /
to search for the current random ID the drive is currently mounted at and look for something like /dev/disk/by-id/something-part1
in the "Symlinks:" section above. Then use some DBus stuff to go the other way around:
RANDOM_PATH="/dev/disk/by-id/something-part1" # If your ID is really changing, you can try using wildcard here together with the part (vendor, etc.) that doesn't change
RANDOM_PATH="$(readlink -f "$RANDOM_PATH")"
RANDOM_PATH="$(basename "$RANDOM_PATH")"
RANDOM_PATH="$(dbus-send --system --print-reply=literal --dest=org.freedesktop.UDisks2 /org/freedesktop/UDisks2/block_devices/"$RANDOM_PATH" org.freedesktop.DBus.Properties.Get string:org.freedesktop.UDisks2.Filesystem string:MountPoints)"
RANDOM_PATH="${RANDOM_PATH#*\"}"
RANDOM_PATH="${RANDOM_PATH%\"*}"
# Then you can cd:
cd "$RANDOM_PATH"
lsblk -e 7 -o name,fstype,size,fsused,label,UUID,mountpoint
See https://help.ubuntu.com/community/MaintenanceFreeCustomGrub2Screen#Labeling_the_partitions or: https://askubuntu.com/questions/276911/how-to-rename-partitions – oldfred Jan 11 '24 at 14:52sudo mount -t ext4 UUID=0ac356c1-caa9-4c2e-8229-4408bd998dbd /media/nvme
and do not understand why your UUID seems to be changing. – Doug Smythies Jan 11 '24 at 14:52sudo lsblk -f
please [edit] your question for output. – nobody Jan 11 '24 at 14:533d3f80f2-83ee-41f9-9d6e-7145b5fdb7dd
. – Doug Smythies Jan 11 '24 at 16:49