Updated on Nov 2017!

Here we are again! I hope that you will appreciate this new set of linux commands

Linux Utility 1.21: Group members

In the previous batch of linux commands we have seen how to create users and add them to groups. In order to see the members of a group you can use:

userk@serotonin:/var/www/html$ grep 'tenzo' /etc/group
tenzo:x:2016:user1,user2,user3

Utility 1.22 : Mysql dump database

One of the most common ways of backing up a MySQL database is to use a command called mysqldump

Let us suppose that you have a database called my_db and a privileged user called userk, the syntax is:

userk@serotonin:/var/www/html$ mysqldump -u userk -p my_db > backup_name.sql

Utility 1.23 : Mysql restore database

To restore a dumped database you first have to create a new database to house the imported data:

userk@serotonin:/var/www/html$ mysql -u username -p
mysql > CREATE DATABASE database_name;
mysql > exit

Then you can copy the content of the sql file in the new database:

userk@serotonin:/var/www/html$ mysql -u userk -p database_name < backup_name.sql


Utility 1.24 : Mysql basic commands

Basic table operations: To create a new table with a couple of fields

CREATE TABLE table_name (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
firstname VARCHAR(30) NOT NULL,
lastname VARCHAR(30) NOT NULL,
email VARCHAR(50),
created_at DATETIME
updated_at DATETIME
)

Drop a table:

DROP TABLE table_name;

Modify tables:

To drop a column

ALTER TABLE table_name DROP COLUMN column_name;

To add a column

ALTER TABLE table_name ADD column_name datatype;

To modify the datatype of a column:

ALTER TABLE table_name ALTER COLUMN column_name datatype;

Linux Utility 1.25: Who is using the speaker?


List the exact name of the applications that are using your sound system with:

userk@dopamine:~$ pacmd list-sink-inputs | grep lient

Not bad.

Linux Utility 1.26: Terminal not fully functional?

Check if you have created a new user with:

$ useradd -d /home/users/usera -m usera

you might have noticed that usera’s terminal lacks of several features like tab completion and hostname highlighting

usera’s shell is not /bin/bash, but /bin/sh. This shell has no fancy interactive features such as command line edition.

Check last column of usera’s row in/etc/passwd. Or simply

$  getent passwd usera
usera:x:5022:5022::/home/usera:

Just change user’s shell with:

$chsh -s /bin/bash usera

Let’s check the shell associated to usera

$ getent passwd usera
usera:x:5004:5004::/home/usera/:/bin/bash

Ok that’s correct!

Linux Utility 1.27: Clone and restore sdcard using dd

Sometimes a copy of your system can be very useful, especially if you want to restore an Operative System. You achieve this goal in two steps: Clone the sdcard and create an image file. Transfer the image into the target sdcard. Let’s see how to do it.
Extract the sd card and using a reliable external card reader plug it into your computer.

userk@dopamine:~$ sudo fdisk -l
[sudo] password for userk: 

Disk /dev/sda: 232,9 GiB, 250059350016 bytes, 488397168 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0xebf65755

Device     Boot     Start       End   Sectors  Size Id Type
/dev/sda1  *         2048 471801855 471799808  225G 83 Linux
/dev/sda2       471803902 488396799  16592898  7,9G  5 Extended
/dev/sda5       471803904 488396799  16592896  7,9G 82 Linux swap / Solaris


Disk /dev/sdd: 7,5 GiB, 8068792320 bytes, 15759360 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0xb10a5dec

Device     Boot Start      End  Sectors  Size Id Type
/dev/sdd1        8192    93813    85622 41,8M  c W95 FAT32 (LBA)
/dev/sdd2       94208 15759359 15665152  7,5G 83 Linux

Identify your sd card, eventually by comparing the output of the previous command with and without the sd card. In my case it is called /dev/sdd

Now create the image using the dd command

userk@dopamine:~$ dd if=/dev/sdc of=sdimage.img bs=4M status=progress

Now, remove the sd card from the reader. Plug the targer, virgin sdcard. Run fdisk command to identify it.

userk@dopamine:~$ sudo fdisk -l
[sudo] password for userk: 
Disk /dev/sda: 232,9 GiB, 250059350016 bytes, 488397168 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0xebf65755

Device     Boot     Start       End   Sectors  Size Id Type
/dev/sda1  *         2048 471801855 471799808  225G 83 Linux
/dev/sda2       471803902 488396799  16592898  7,9G  5 Extended
/dev/sda5       471803904 488396799  16592896  7,9G 82 Linux swap / Sol


Disk /dev/sdd: 14,9 GiB, 15931539456 bytes, 31116288 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x00000000

Device     Boot Start      End  Sectors  Size Id Type
/dev/sdd1        8192 31116287 31108096 14,9G  c W95 FAT32 (LBA)

The device is again /dev/sdd. Please note that this identifier can change. So before continuing, double check the device name. If you don’t know or if you are not sure, feel free to comment the post and we will see it together.

Unmount the device and copy the image.

userk@dopamine:~$ sudo umount /dev/sdd1
userk@dopamine:~$ sudo dd if=epinephrine.img of=/dev/sdd bs=4M status=progress
8057257984 bytes (8,1 GB, 7,5 GiB) copied, 488,095 s, 16,5 MB/s
1923+1 records in
1923+1 records out
8068792320 bytes (8,1 GB, 7,5 GiB) copied, 571,993 s, 14,1 MB/s

Done! Now unplug the target micro sdcard from the reader and you should be good to go.

Hope this helps.