Showing posts with label terminal. Show all posts
Showing posts with label terminal. Show all posts

Thursday, April 2, 2009

Finding text in files from the Command-Line

This tip is posted everywhere, but for my love of copying and pasting, here's how to find any text in a directory of files quickly from the command line:

find . -name "*.php" -print | xargs grep "text_to_find"

To break this down:

find . -name "*.php" -print finds all files in the current or sub-directories that has the filename *.php and print out the file name

| This is the pipe. It sends the output of the above find command to the next command:

xargs takes the input from the find command and passes those files to the next command:

grep "text_to_find" searches in each of the files for the text "text_to_find"

Friday, March 27, 2009

SafariStand downloads and the Terminal

I love SafariStand. I love SafariStand's ability to organize downloads by date. I hate trying to navigate to the current day's download directory via the terminal. I typically don't delete downloads very often so my Downloads folder tends to get cluttered with a bunch of folders titled '2009-03-11', '2009-03-15', etc.

If you're a heavy terminal user, it can be a pain to get into the current day's folder because tab-completion always stops at '2009-03-' if your folder spans just one month, '2009-' if it spans multiple months, or '200' if it spans multiple years.

I decided to rectify this situation by making an alias to the date command to generate the current date in the same format. It's pretty simple:

In your .profile or .bash_profile file in your home directory simply add the line:

alias today='date "+%Y-%m-%d"'

Then when you're in the terminal, go to your downloads folder and type

cd `today` (note the back quotes around today) (the back quote is shift-tilde)

and it will automatically generate the proper date string and take you right into your folder.

Friday, December 5, 2008

Quickly Adding SSH Keys

Here's a quick way to create and add ssh keys to a server:

  1. Generate your ssh key on your client machine:
    ssh-keygen -t dsa Note: Set a password for your key. (see note)
  2. Secure Copy (scp) the key to your ssh server:
    scp .ssh/id_dsa.pub username@server.com:.ssh/id_dsa.pub
  3. Add the key to your authorized_keys2 file:
    ssh username@server.com 'cat .ssh/id_dsa.pub >> .ssh/authorized_keys2'
  4. Shortcut: Combine steps 2 and 3:
    cat ~/.ssh/id_dsa.pub | ssh username@server.com 'mkdir -p ~/.ssh && cat - >> ~/.ssh/authorized_keys2'

Note on SSH Key Passwords

On step 1, you have the option to add a password to your key. If you don't add a password and someone gets ahold of your private key, they will be able to access any server that you have given that key access to. By setting a password, the user still has to know your key password to use it. Though the purpose of this tutorial is to speed up and secure your login, you will still have to type in that password you set in step 1 every time you log in to an ssh server. This is where SSH Keychain comes into play.

SSH Keychain on the Mac

Leopard has this functionality built into the Keychain, but if you are on 10.4 or older, you can use SSH Keychain on the Mac to manage your SSH passwords. This great little app lets you store your ssh passwords in your computer's Keychain so you're not constantly having to type it in. Download it at http://www.sshkeychain.org/ and add your keys to it.

Why Use SSH Keys Anyways?

When you log into an SSH server , you have to send your password over the network at least once. Any network traffic can be potentially captured and, given a long enough time span, decrypted.

SSH keys work on the principle of Public Key Cryptography, which is beyond the scope of this article. In general it allows the server to verify who you are based on this key trust you just set up. Since your keys are cryptographically strong (usually 1024+ bit), it's much more difficult to hack than a short, memorable password.

Multiple Users

SSH keys also allow you to give multiple users access to a single account on your SSH server by simply adding everyone's keys to the single account.

For more in-depth information on SSH keys, see Authentication by Cryptographic Key by O'REILLY.