Jul 4 2011

Changing Linux Default Timezone

It is easy! Just type ‘dpkg-reconfigure tzdata’ in your shell and follow the instructions.

Command to invoke timezone configuration

Select Region

Select Country

Easy?


Jan 24 2011

Export & Import MySQL results

How to export MySQL selected results (Rows) from database A & import into database B?

The easiest way is to use Select INTO OUTFILE to export and use LOAD DATA INFILE.

SELECT * INTO OUTFILE '/tmp/records.csv'
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\n'
FROM tbl1;

Run this from mysql shell and you will be seeing /tmp/records.csv file (at database server, not at remote client machine). To import it into different database, you required to run this in destination database.

LOAD DATA INFILE 'records.csv'
INTO TABLE tbl2
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\n';

Jan 20 2011

Single Pipe Equal sign (|=) in Ruby

Ever wonder what is |= (single pipe equal) sign in Ruby? I believe some of you might have hard time to hunt that from google like what I did.

The first time I saw this strange symbol is from Ruby Delegator delegate.rb. I learned that &= (single ampersand equal) is also there in Ruby,

delegate.rb

class Delegator
  IgnoreBacktracePat = %r"\A#{Regexp.quote(__FILE__)}:\d+:in `"

  def initialize(obj)
    preserved = ::Kernel.public_instance_methods(false)
    preserved -= ["to_s","to_a","inspect","==","=~","==="]
    for t in self.class.ancestors
      preserved |= t.public_instance_methods(false)
      preserved |= t.private_instance_methods(false)
      preserved |= t.protected_instance_methods(false)
      break if t == Delegator
  end
.
.
.

Let’s do a test with |= in irb

>> arr = ["a", "b", "c", 1, 2, 3]
=> ["a", "b", "c", 1, 2, 3]

>> arr |= ["a", "c", 2, "x", "y", 0]
=> ["a", "b", "c", 1, 2, 3, "x", "y", 0]

>> puts arr.join(",")
a,b,c,1,2,3,x,y,0

As you can see, I set “a”, “b”, “c”, 1, 2, 3 into arr array initially. Next I |= it with ["a", "c", 2, "x", "y", 0] and it only add ["x", "y", 0] to original array where all are unique value. It append the unique value to the arr.

Lets do a new test with &=

>> arr = ["a", "b", "c", 1, 2, 3]
=> ["a", "b", "c", 1, 2, 3]

>> arr &= ["a", "c", 2, "x", "y", 0, 2]
=> ["a", "c", 2]

>> puts arr.join(", ")
a, c, 2

This time only ["a", "c", 2] are returned because only these are exist in both array, well fit to function of AND (&).

Conclusion:
|= is to append unique to the array, whereas
&= is to remain the matched values in the array.


Apr 20 2010

How to Check My Linux Version?

The similar question you may ask:

  • How to know my Linux server is running in 64/32 bits?
  • How to check which kernel version is my Linux?
  • How to show the Linux release name? Is it Ubuntu?Redhat or CentOS or something else?

In work, we have many different linux servers either in physical machine or virtual machine (Using Xen/KVM). Sometimes we easily lost track of which linux version is installed for Server A, B, and other.

There are few simple command to check for which Linux version you were using:

To show the linux release information as per LSB specification

sudo lsb_release -a

No LSB modules are available.
Distributor ID:    Ubuntu
Description:    Ubuntu 8.04
Release:    8.04
Codename:    hardy

To show the Linux kernel build and version

cat /proc/version

Linux version 2.6.24-19-xen (buildd@king) (gcc version 4.2.3 (Ubuntu 4.2.3-2ubuntu7)) #1 SMP Sat Jul 12 00:15:59 UTC 2008

The other commands is to show the kernel version and type:

uname -a

Linux penguin 2.6.24-19-xen #1 SMP Sat Jul 12 00:15:59 UTC 2008 x86_64 GNU/Linux


Apr 9 2010

How to List Crontab for All Users

There was a time I would like to check what are the task/job configured in crontab of all users. After I googled it I found this scripts from StackOverflow

You must run this script as root.

   for user in $(cut -f1 -d: /etc/passwd); do crontab -u $user -l; done

What does it do? This script basically loop thru /etc/passwd to get the user id and call ‘crontab -l’ for each user. A nice script to share and also I would like to keep it here so easy for me to refer next time.