Showing posts with label tips. Show all posts
Showing posts with label tips. Show all posts

Friday, September 5, 2008

Gnome Terminal Tips

I often use the profiles feature in gnome terminal to organize all my remote shell accounts. Inside of gnome terminal, you can go to Edit->Profiles to manage your profiles. From there, you can either edit an existing profile or create a new one. Either action will take you to the "Editing Profile" window. Once inside, click on "Title and Command" tab and check the box that says "Run a custom command instead of my shell". You can put your ssh login info inside of the "Custom command" box. An example would be:

ssh -i /home/username/.ssh/id_rsa.1 -p 7822 myuser@myhost.com

That would invoke an ssh session using port 7822 and the identity file stored in your .ssh directory. It's particularly useful to setup sessions with shared keys so you can just go File->Open Terminal and point to the desired session. If you have certain ssh session you use a lot, it might be worth adding a launcher to your panel and specifying the desired profile like so:

gnome-terminal --window-with-profile SomeProfile

It's also worth noting that gnome terminal allows you to color code your saved profiles. I find that this helps prevent stupid mistakes caused by thinking your logged into a different session than you actually are.

Since I use gnome terminal to store many remote login sessions, I sometimes need to retrieve this information whilst remotely logged into my workstation. An example would be when I ssh to my work PC from home over a VPN connection. Gnome terminal stores all of it's saved profile information in the following location.

/home/$username/.gconf/apps/gnome-terminal/profiles/$profilename/%gconf.xml

In this case, $username and $profilename would represent your username and desired gnome profile name respectively. Inside of the %gconf.xml file, there's a section for custom command that looks like this.

<entry name="custom_command" mtime="1193759925" type="string">
    <stringvalue>ssh -p 7822 username@somehost.org</stringvalue>
</entry>

From there, you should be able to extract whatever info is desired.

Bonus tip: you can open a new terminal window whilst inside an active gnome terminal session by doing ctrl-shift-N.

Wednesday, June 11, 2008

A Few MySQL Tricks

Here are a few tricks I use quite a lot with MySQL.

1) copy an existing table's schema to a new table

This is handy for making a quick copy of an existing table to test indexes, populate data, etc...

CREATE TABLE NewTableName LIKE OldTableName;

2) copy an existing table to a new table

If you want more than just the structure, you can copy a table's raw data to a new table really easily. The downside is that keys and other constraints aren't preserved.

CREATE TABLE NewTableName SELECT * FROM OldTableName;

3) copy a table's exact structure and data

If you want to preserve everything (constraints, keys, etc...) and copy the data, it requires two steps.

CREATE TABLE NewTableName LIKE OldTableName;
INSERT INTO NewTableName SELECT * FROM OldTableName;

4) quickly load data while specifying columns

After reading the MySQL forums, I've realized a lot of people online think this is impossible. It's actually right there in the documentation; although, somewhat obscurely tucked away. MySQL DOES allow you to specify what columns to operate on when using LOAD DATA INFILE. Just use the following syntax.

LOAD DATA INFILE '/some/file/name.csv' INTO TABLE SomeTableName FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n' (field1, field2, field3);

The field list goes at the end. Adjust the statement according to your needs, and enjoy the 20X increase in data load times.

5) atomic rename

There are a number of situations where you want to replace an entire table worth of data with a new table instantly. This is easily accomplished with an atomic rename and avoids a brief downtime while the tables are being switched.

RENAME TABLE TableName TO TableNamePrevious, TableNameNew TO TableName;

6) monitor a long running insert

If you've ever dealt with long-running inserts, you probably know how annoying it can be to just sit and wonder when the task will complete. For inserts performed in a single transaction, a simple select count(*) won't do because they're all or nothing. Take the following example:

INSERT INTO TableA SELECT * FROM TableB;

Fortunately, there's a way to peak behind the scenes. Do a count on the target table beforehand and make sure it has a populated primary key column. From there, you can do:

SHOW CREATE TABLE TableName;

Look at the last line of the table definition, and you'll see an AUTO_INCREMENT value. That's the last increment of the primary key. If you started with 25,000 rows, and AUTO_INCREMENT is 50,000 then you know 25,000 new rows have been inserted.

7) monitor a LOAD DATA INFILE

If you're using InnoDB, you can do SHOW INNODB and look at the transactions list. From there, "undo entries" displays the number of rows inserted via the current LOAD DATA INFILE operation thus far. This is convenient because I don't believe the AUTO_INCREMENT trick works for LOAD DATA INFILE.

Thursday, March 13, 2008

Using xargs

I'm a big fan of the xargs command. It's widely used in shell scripts but not so much on the command line. It's a really handy command when you want to take an incoming argument and apply a command to it. Here's an example that would kill every process matching a given pattern of "firefox" (similar to killall).

ps aux | grep firefox | awk '{print $2}' | xargs kill -9

remove files matching *lock*:

ls *lock* | xargs rm

for more complex commands, you can set the incoming data to be stored in a token:

ls *lock* | xargs -I $ ls -l $

or

ls *lock | xargs -I {} ls -l {}

Obviously the possibilities go far beyond what's shown here. Play around with it.

Friday, November 9, 2007

Hello World

Hello, my name is Travis Whitton. I'm a programmer by trade, and I've been a user of the Vim text-editor for the last nine years. Although, I still don't consider myself an expert, I do enjoy digging into the vast array of features Vim provides, and the intention of this blog basically to keep track of what I've been learning. I plan on posting new information regularly, and my goal is to try and post at least one tip every workday. I welcome any comments or suggestions and especially appreciate any sharing of information.

The first tip I'm going to share is a very basic one, but I find it to be a nice time-saver. Anytime I am typing, I find it a waste to move my fingers off the home keys. This means that taking my left hand away from the middle-row of the keyboard to press the Esc key is something I want to avoid. I recently learned that ctrl-[ works as a handy substitute for Esc. Try it, you might like it.