How to handle those "smart" device names at the CLI?

Johnny Rosenberg gurus.knugum at gmail.com
Sat May 14 16:07:05 UTC 2011


2011/5/14 Thierry de Coulon <tcoulon at decoulon.ch>:
> On Saturday 14 May 2011 03:04:00 pm Johnny Rosenberg wrote:
>>
>> Why is that? Works for me. In the case above I would type the
>> following to change to that directory, where ”⇥” means the ”↹” key
>> (TAB):
>> cd /me⇥4⇥
>> (/me⇥ will be auto-completed to /media/ and 4⇥ will hopefully be
>> auto-completed to ”4ae56680-7468-493e-aa40-9bb6c79f00f7”. If not, just
>> add one or a few more characters, 4a⇥).
>> But, as some people already suggested, maybe you would rather prefer
>> to use e2label to give it a label that makes sense to you in the first
>> place, like this example:
>> sudo e2label /dev/sde1 Backup
>> To just check the current label, try:
>> sudo e2label /dev/sde1
>
> Thanks to all that made this suggestion. I never figured out that this was
> just a "label".
>
>> By the way, here's a useful trick, not directly related to this subject
>> though: If you run a command that need to be run as root, and you forget to
>> type ”sudo”, you don't need to type the whole command again and you don't
>> even need to use the ↑ key for editing the last command. All you need to do
>> is to type:
>> sudo !!
>> Looks something like this:
>>
>> $ e2label /dev/sdb1
>> e2label: Access denied when trying to open /dev/sdb1
>> Couldn't find valid filesystem superblock.
>> $ sudo !!
>> sudo e2label /dev/sdb1
>> Backup
>> $
>>
>>
>> Regards
>>
>> Johnny Rosenberg
>> ジョニー・ローゼンバーグ
>
> Thanks Johnny. I'm fairly used to first becomming root before using this sort
> of command, but sudo!!  will certainly come handy some day soon :)
>
> Thierry

You need that space between ”sudo” and ”!!” though. ”!!” seems to mean
”last thing you typed” so you can use it in cases like this too:
$ label -i /dev/sdb1 -s ::
label: command was not found
$ m!!
mlabel -i /dev/sdc1 -s ::
Can't open /dev/sdb1: Access denied
Cannot initialize '::'
mlabel: Cannot initialize drive
$ sudo !!
sudo mlabel -i /dev/sdb1 -s ::
 Volume label is Transcend
$

Anyway, since there are different tools for different file systems I
wrote a very simple function.

function label () {
case $# in
1 | 2)
	Device=$1
	Label=$2
	FileSystem=$(cat /proc/mounts | grep $Device | awk '{print $3}')
	case ${FileSystem} in
	"ext2" | "ext3" | "ext4")
		sudo e2label $Device $Label;;
	"vfat")
		sudo dosfslabel $Device $Label;;
	"ntfs")
		sudo ntfslabel $Device $Label;;
	*)
		echo "Sorry, no support currently for ${FileSystem}.";;
	esac;;
*)
	echo "Syntax: label device [name]";;
esac
}

I added the function to ”~/.bash-functions”, which is a file that will
run whenever I login. On my system that is, because I have the
following lines added to my ”~/.bashrc” file:

if [ -f ~/.bash-functions ]; then
    . ~/.bash-functions
fi

So now it works like an alias, kind of. Right now I have two devices
connected: sdb1 and sdc1. The first one is a vfat device and the other
one a ext3 device. The label function works for both of them:
$ label /dev/sdb1
[sudo] password for guraknugen:
Backup
$ label /dev/sdc1
Transcend
$

Of course it's easy to add support for other file systems as well, I
just made it simple for my own use…
And of course I take no responsibility what so ever for any damages
caused by that function.

Kind regards

Johnny Rosenberg
ジョニー・ローゼンバーグ




More information about the ubuntu-users mailing list