Tag Archive "trick"

removing the top N lines from a file

Published October 9th, 2009 by Chad

Just a quick post here. I was messing around with some pretty large text files yesterday. I had a file with 2.9 million insert statements (one per line) that I was running on a database. It was taking forever so I stopped it. I new how many of the insert statements had run so I needed to get rid of the top million and a half so that they were not inserted again. I came across this nifty one-liner that made it really easy and didn’t take too long: sed '1,1500000d' inserts.sql > inserts2.sql Now I will explain what I understand it to be doing. sed is a command that is very powerful (I don’t know the first thing about using it really) for working with text and files. The 1,1500000 indicates that I want to do something to lines 1-1500000 of the file. The d tells sed that I want to delete those lines. Then specify the file you want to do that do and output the remaining contents (>) to a new file insert2.sql.

Pretty simple, but sed can be pretty involved.

ssh login without a password

Published October 8th, 2009 by Chad

There are a few places that I have to ssh to over and over and over again each day. I have a development and a production server at work (many of each actually) as well as my personal web hosting. I also have a very secure and strong password (if I do say so myself) that I use for many things, but it is also pretty long. I can type it pretty fast now, but it is easy to mess up if I am lot looking at my fingers and that slows me down. The solution to all of this…don’t use my password from my personal and work computers. Simple. :)

Following are the steps that you will need to take to be able to ssh to a system without using your password every time.

  1. Generate your public and private ssh keys on your local machine. ssh-keygen -t rsa NOTE: Just keep hitting enter. The ssh-keygen command will ask for a fire name and a passphrase. Just it enter to use the defaults.
  2. Make sure there is a .ssh directory on the remote machine. ssh remoteuser@remotemachine mkdir -p .ssh NOTE: It should ask for your remote users password for the remote machine.
  3. Copy your public key to the remote machine. cat .ssh/idrsa.pub | ssh remoteuser@remotemachine 'cat >> .ssh/authorizedkeys' NOTE: It should ask for your remote users password for the remote machine. This should be the last time you will have to type your password in while using ssh to that machine.

Once you have done this you should be able to ssh into the remote machine without having to type in your password. And this should work every time when sshing from your local machine to the specific remote machine.

If you want to set up a key between the same local machine and a different remote machine, you don’t have to do step 1 again. Just use the same public key from before, id_rsa.pub.