Tag Archives: command

How to text search in terminal

There’re times when I need to quickly search through occurrences of certain phrases in my PHP source files. Here is when I have to search for ‘giraffes’.

This is an example finding the word ‘giraffes’ in all PHP source files from the current directory recursively.

find . -name "*.php" -exec grep -i -H -n "giraffes" {} \;

This works for Ubuntu, CentOS and Mac OS X.

How to check Ubuntu version number

This is a personal note. This lets you check Ubuntu’s version number. Run the command ‘cat /etc/lsb-release’.

tkw@deathnode:~$ cat /etc/lsb-release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=9.04
DISTRIB_CODENAME=jaunty
DISTRIB_DESCRIPTION="Ubuntu 9.04"
tkw@deathnode:~$

My blog has pretty much become a scrap book.

To find Red Hat or CentOS linux version number use:

tkw@redhat:~$ cat /etc/redhat-release
CentOS release 4.8 (Final)

jer added that this is a better alternative:

lsb_release -a

Thanks!

How to import and export MySQL database into an SQL file

Or Gzip for the matter. Here’s the command to run in your UNIX-based server to import or export via an SQL file, this is useful for performing backup and restoring of a MySQL database. (I wrote a similar import and export guide for PostgreSQL.) The mysqldump utility performs just that:

Exporting using mysqldump:

mysqldump -u[Username] -p[Password] [Database] > output.sql

For example, my username is ‘kahwee’, my password being ‘secret’ and database being ‘justrealized_db’, I would run the following to export my database to a SQL file:

mysqldump -ukahwee -psecret justrealized_db > output.sql

And to Gzip:

mysqldump -u[Username] -p[Password] [Database] | gzip > output.sql.gz

Importing using mysql:

To import back, we can use the mysql utility in a similar fashion, note that the > (greater than) has change to a < (lesser than).

mysql -u[Username] -p[Password] [Database] < output.sql

For example, my username is 'kahwee', my password being 'secret' and database being 'justrealized_db', I would run the following to import my database:

mysql -ukahwee -psecret justrealized_db < output.sql

And to Ungzip:

gunzip < output.sql.gz | mysql -u[Username] -p[Password] [Database]

Backing up and restoring MySQL databases in Windows

Unfortunately, you can't use gzip here. So all those commands above with gzip can't work. The rest, however, still works. However, mysqldump and mysql may not be set in your system environment variables. These are instructions on how to add them for Windows Vista:

Editing system environment variables in Windows Vista.

Click on 'Edit the system environment variables', a dialog box will pop up. Click on 'Environment Variables...', you should be greeted with the following dialog box:

Editing the path for environment variables

My path looks like this before I add anything:

%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem

Append your MySQL bin directory at the back of what is already there. I use XAMPP (XAMPP lite to be specific) which has its MySQL bin folder located here ';C:\xampplite\mysql\bin\', so I would be appending this:

;C:\xampplite\mysql\bin\

That's all I guess, hope it is helpful for you.

How to optimize MySQL database with a command

When you delete large portions for data from your MySQL table, the database becomes more fragmented. It is always a good idea to re-optimize your MySQL database especially if you have a lot of varying characters (VARCHAR). Here’s the command to optimize just one table:

OPTIMIZE TABLE foo

But I am looking for a more command line solution and here it is:

mysqlcheck -op database_name

The ‘o’ refers to optimize. And the ‘p’ is the option for password requirement. You’ll be prompted for the password.

If you’re logged in as user ‘john’ in your SSH, the MySQL username will be ‘john’. If that’s not what you want, use option ‘u’ like this:

mysqlcheck -op -u user_name database_name

And to optimize all databases:

mysqlcheck -op -u user_name --all-databases

You can also use option ‘r’ to repair.

Note that the optimization only works for MyISAM, InnoDB, and ARCHIVE tables.