Saturday, July 21, 2012

Changing Storage Label on Linux

On Windows, you can simply right click on a device and rename it. On Linux in the other hands, you can use the following method:

During formatting, use let say if you want FAT as the filesystem:


Let say the device is located in /dev/sdc and I want it to be named as "najmi-archsight". Issue this command:

sudo mkfs.vfat  -I /dev/sdc -n "najmi-archsight"

While let say in the other hand, you already got a data inside and want to change the label, use dosfslabel:

1- If the device is mounted, unmount it first
2- Issue this command:

Let say here I want to rename the device to "najmi-csm". The device is firstly mounted at /media/B807-ED09.

najmi@vostro:~$ df -kh
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda5        47G   42G  3.1G  94% /
udev            1.9G  4.0K  1.9G   1% /dev
tmpfs           752M  1.4M  750M   1% /run
none            5.0M     0  5.0M   0% /run/lock
none            1.9G   88K  1.9G   1% /run/shm
/dev/sda6       173G  164G   12K 100% /home
/dev/sdb        2.0G  4.0K  2.0G   1% /media/najmi-archs
/dev/sdc        3.8G  2.5M  3.8G   1% /media/B807-ED09
 

Unmount first:
najmi@vostro:~$ sudo umount /media/B807-ED09/
 

Label it:
najmi@vostro:~$ sudo dosfslabel /dev/sdc "najmi-csm"
 

Check and plug back/mount the drive:
najmi@vostro:~$ df -kh
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda5        47G   42G  3.1G  94% /
udev            1.9G  4.0K  1.9G   1% /dev
tmpfs           752M  1.4M  750M   1% /run
none            5.0M     0  5.0M   0% /run/lock
none            1.9G   88K  1.9G   1% /run/shm
/dev/sda6       173G  164G  4.0K 100% /home
/dev/sdb        2.0G  4.0K  2.0G   1% /media/najmi-archs
/dev/sdc        3.8G  2.5M  3.8G   1% /media/najmi-csm

As you can see, device /media/B807-ED09 was changed to /media/najmi-csm

Wednesday, July 18, 2012

Regular expression in bash "for" loop for file exclusion

najmi@aku-PC ~/cuba
$ touch ayam.txt ayam.csv ayam.egg

najmi@aku-PC ~/cuba
$  for i in a*[!.csv,.txt];do echo $i;done
ayam.egg


Here we use the popular ! mark to exclude the file extension within the brackets. Got the solution after few trials.

Friday, July 13, 2012

Using regexp in Linux commands

What if you have several files with same name but different extension and you want to choose only few files and leave the rests?

See the example here:

Create several files:
najmi@vostro:~/test$ touch aku.png aku.jpg aku.txt

Check:
najmi@vostro:~/test$ ls
aku.jpg  aku.png  aku.txt

New directory to separate the files
najmi@vostro:~/test$ mkdir newdir


Use {} braces to include only the specific extensions that you want to handle:
najmi@vostro:~/test$ mv aku.{jpg,txt} newdir/ -v
`aku.jpg' -> `newdir/aku.jpg'
`aku.txt' -> `newdir/aku.txt'


Check current directory
najmi@vostro:~/test$ ls
aku.png  newdir

Selected files already affected(moved)
najmi@vostro:~/test$ ls newdir/
aku.jpg  aku.txt