Saturday, February 23, 2008

Clear screen without losing the current command

The command clear clears the screen. An alternative way to clear the terminal screen is through a shortcut: Control-L.

Control-L gives you a bonus: if you are in the middle of typing a long command, hitting Control-L clears the screen without erasing what you already enter on your current command. Whatever you typed in as the current command will appear at the top of the screen.

So, you can finish typing in your command and hit enter to execute it with a pristine screen real estate.

Sunday, February 17, 2008

Go to next/previous word on the command line

I have been looking for a way to move forward and backward on the command line by bigger chunks than just character by character. Something like go back a word, or go to next word.

I finally found it:

  • to go back a word, press Esc, then the letter b
  • to go forward a word, press Esc, then the letter f

I must say that the key sequence is some what awkward to hit right. Never mind about repeating it to move back/forward several words.

With that said, I found what I was looking for.

Thursday, February 14, 2008

Save your command for later execution

Sometimes, you change your mind when you are in the middle of typing in a command at the command prompt. You don't want to execute the command until a later time, but you don't want to retype the line again. What to do?

You can hit Control-C to abandon the command. The problem with Control-C is that what you have entered already is lost: the line won't go to your command history.

A neat trick is to comment the line out. First, hit Control-A to go to the start of the line, and then enter # (the "pound sign"). This essentially makes the entire line a comment line. The line will not be executed, but it does go to the command history.

When you are ready to execute the command, just go back in the command history (via manual scrolling or search), remove the #, and execute the command.

Tuesday, February 12, 2008

Super Fast way to open a file for vi at a given line

Sometimes, you know the line you want to edit in a file, and you just need to open the file and get to that line. For example, you just had done a grep -n, and found the line you want, say line 99 of a file.

Sure, you can vi the file, and then type 99G (capital G, that is), to get there.

A much faster way is enter this in the command prompt:

$ vi +99 some-file

The above opens some-file and go immediately to line 99.

Saturday, February 9, 2008

Create File of a Given Size

Last week, I needed to test the speed of my VPN connection. My plan was to create a file of some given size (say 10M), and test copy it to another server across the VPN tunnel.

My first task was to create a file of size 10M. On Solaris, it can be done simply by this command:
$ mkfile 10m output.dat 

On Linux, you can use the dd command:

$ dd if=/dev/zero of=output.dat  bs=1024  count=10240
10240+0 records in
10240+0 records out
10485760 bytes (10 MB) copied, 0.218581 seconds, 48.0 MB/s
$ ls -hl output.dat
-rw-r--r-- 1 peter peter 10M 2008-02-09 16:21 output.dat

The above dd command creates a zero-filled file named output.dat consisting of a count of 10240 blocks, each of block size 1024.

An anonymous commenter pointed out that you can also create a 10M file like this:
$ dd if=/dev/zero of=output.dat  bs=1M  count=10

Now that the 10m file has been created, I can time the copying of the 10m file like this:

$ time scp path/to/10m.dat  user@

If you can suggest other ways to create an arbitrary sized file, please share with us via comments.

P.S. Articles from this blog on the dd command:
Show progress during dd copy