Posts Tagged ‘shell’

Convert audiofiles on Ubuntu by the command line with soundconverter

If you need to convert audio files in another format you can use the tool called “soundconverter”, that you can simply install:

~$ sudo apt-get install soundconverter

Then simply run a command like:

~$ soundconverter -b -m "audio/mpeg" -s ".mp3" /home/laura/cd-wav/*.wav

“-b” stands for batch type

“-m” to specify the mime format.

“-s” to specify the suffix

And move them into another folder eventually:

~$ mv /home/laura/cd-irene/*.mp3 /home/laura/cd-wav.

For more info check the official wiki pages:
http://wiki.ubuntuusers.de/soundconverter
http://manpages.ubuntu.com/manpages/karmic/man1/soundconverter.1.html

GitHub: working with the git command line on Windows

This tutorial will allow you to upload your project on the github repository.

I am a linux fan but I have to work with Microsoft Windows. Git is integrated in the Eclipse IDE, but I want to learn to use it by command line, to learn the commands in a better way.

Reality is a bit tougher on the shell. I know about setting SSH keys on Linux. I have already blogged about it on this site, but I have decided to use the Github software provided for Windows.

The steps are the following:

1) install git to run the commands by shell. You can get the installer for Windows at: https://code.google.com/p/msysgit/downloads/list?q=full+installer+official+git

2) download and install github client, that you can find at https://help.github.com/articles/set-up-git

3) run the Github client and login with your github account credentials. The software will create a .ssh folder with your rsa git keys automatically, so you won´t have to worry about passwords and keys. Just use the github software and the provided shell.

4) create a github repository from the github website or the installed client: for example “Hibernate_Demo” (Github client, figure 1).

Figure 1

5) clone the project locally (Github client,figure 2).

Figure 2

6) click on the project name in github and openshell by clicking on “tools and options>open shell here” (Github client, figure 3).

Figure 3

7) Navigate on the shell to your project on your desktop. For example:

C:\Users\liparulol\workspace\hb_project>

You can now work on the github shell directly.

8) Run the command “git init” to inizialize a local repository for the hb_project and check the status with “git status”.

9) Run the command “git add *” or “git add .” to tell git to commit all the files and directories in the project folder

10) Commit all the added files, adding a message running: 

git commit -m "Add all content"

You can check your last activity with the “git log” command

11) Add a new remote repository of your project:

git remote add origin [email protected]:lauraliparulo/https://github.com/laurliparulo/Hibernate_Demo.git

12) If the created repository contains file that are not on the local master run:

git merge origin/master

13) The you can finally push your project to the repository with the following command:
git push origin master

You should see the project on the github site. It takes some time to see the changes on your github client locally…

You can also manage your git project by running “gitk", which will open a Python revision browser.

Backing up the Master Boot Record in Ubuntu 11.10

The Master Boot Record (MBR) is a space of 512 bytes that holds the partition table as well as the stage 1 of the boot loader.

Making a backup of the MBR might be useful in case of damage, as the operative system wouldn’t boot.

Therefore, you can make a copy in your home directory typing:

sudo dd if=/dev/sda of=~/mbr.txt count=1 bs=512

If you type:

sudo cat /dev/sda

you can see that /dev/sda is a binary file. However you can see that the backup is a file called “mbr.txt“.

If you need to restore the MBR you can use the following command:

sudo dd if=~/mbr.txt of=/dev/sda count=1 bs=512

 

Sqlite database backup: the .dump command

Let’s go on mastering our sqlite3 knowledge.
SQLite database is really just a file: a backup it’s as simple as copying one file.

The .dump command shows information about all the changes performed onto the database. Less pieces of information to the hidden file can be found in your home/user typing: $ ~/.sqlite_history.

$ sqlite3 test.db “.dump”

The result is:
PRAGMA foreign_keys=OFF;
BEGIN TRANSACTION;
CREATE TABLE n(id INTEGER PRIMARY KEY, f TEXT, l TEXT);
INSERT INTO “n” VALUES(1,’linus’,’torvalds’);
INSERT INTO “n” VALUES(2,’richard’,’stallman’);
COMMIT;

If you want to backup the database in a new file, you can specify a name (ex. “dbbackup”):
$ $ sqlite3 test.db ‘.dump’ > dbbackup

The contents of the backup can be modified.
For example you can filter and pipe it to another database. Below, table “n” is changed to “people” with the sed command, and it is piped into the “computer_pioneers” database.

$ sqlite3 test.db “.dump”|sed -e s/n/people/|sqlite3 computer_pioneers.db
The contect is the same:
$sqlite3 computer_pioneers.db “select * from people”;

First steps in Sqlite and Perl

Sqlite is useful to create a database (one file, .db extension) used to store configuration data, used Miicrosoft, Skype, Banshee… Smart Phone applications….

The file extension .db stands for a whole database: it’s made by the software C library called Sqlite

This is a great opportunity to learn about SQLITE and the Perl scripting languages… great, don’t you think?
Let’s kill two birds with a stone!
Install Sqlite3 and follow me:
On UBUNTU, DEBIAN, etc:
$ sudo apt-get install sqlite3
On OPEN-SUSE:
$ sudo zypper install sqlite3
On REDHAT, CentOS, or FEDORA:
$ yum install SQLite3

BASH EXAMPLE
Let’s create an example: a database called “test.db” by the (unix) shell by the following command:

:~$ sqlite3 test.db “create table if not exists user(id INTEGER PRIMARY KEY, name TEXT,  surname TEXT);”

Let’s fill it:
:~$ sqlite3 test.db “insert into user (name, surname) values (‘linus’,’torvalds’);”

:~$ sqlite3 test.db “insert into user(name,surname) values(‘richard’, ‘stallman’)”;

To check it out:
:~$sqlite3 test.db “select * from n”;

The result is:
1|linus|torvalds
2|richard|stallman

Alternatively you can create a database entering the sqlite3 enviroment
$ sqlite3 test.db
SQLite version 3.0.8
Enter “.help” for instructions
Enter SQL statements terminated with a “;”
sqlite>

In the sqlite3 enviroment you can use pure SQL statements to work with your database (in this case the test.db).
You can even change a few default settings to make the ouput of the commands look better. For example the column .mode and the .headers commands.They will last you exit the SQLite shell or change them to something else.
sqlite> .mode col
sqlite> .headers on

To see all the tables and views type:
sqlite> .tables

To see the databases that are currently open use the .databases command. It will show the main and temp databases and where they are on the system:
sqlite> .databases

To exit type .quit or .exit:
sqlite> .quit

Anyway it’s better to work in the shell prompt directly, that allows you to run bash scripts.
In this example the prompt is in your home/user directory. Check it out by the pwd command if you’re not sure…

PERL EXAMPLE

Make a file called test.pl :
$ touch test1.pl

Use an editor(ex. gedit under ubuntu and opensuse) or the cat test1.pl command to fill it with the following script:


#!/usr/bin/perl -w

use DBI;
use strict;

my $db = DBI->connect(“dbi:SQLite:test.db”, “”, “”) or die “couldn’t connect to db”.DBI->errstr;

$db->do(“CREATE TABLE IF NOT EXISTS user (id INTEGER PRIMARY KEY, name TEXT, surname TEXT)”);
$db->do(“INSERT INTO user\(name, surname) VALUES ( ‘linus’, ‘torvalds’)”);
$db->do(“INSERT INTO user\(name, surname) VALUES ( ‘richard’, ‘stallman’)”);

my $all = $db->selectall_arrayref(“SELECT * FROM USER”);

foreach my $row (@$all) {
my ($id, $name, $surname) = @$row;
print “$id|$name|$surname \n”;

}

$db->disconnect;

Alternatively you can make the file directly by the shell:
$ cat > test.pl
.. perl script content…

Type Ctlr+C to close the file and exit.
Now check the file content:
$> cat test.pl

Make the perl script executable:
$ sudo chmod +x test.pl
Then run the perl script simply:
$ ./test.pl
The result is:
1|linus|torvalds
2|richard|stallman

In the same folder where the script lies, you can find a file called “test.db”. That’s the Sqlite database. Just one file.

Yeah! So we’ve learned some Perl and SQLite, right?

Categories
Links: