I didn't do it!

Jun 09

Reload!

I notice quite often terminal users don’t know how to reload their rc files - bashrc, zshrc, etc. There are two ways:

. ~/.bashrc

or

source ~/.bashrc

Jun 08

Grep basics in 25 seconds

Well, not exactly 25 seconds, but I thought that would make a great title. I find I always have to introduce new junior devs to the world of grep and I think the following will get anyone started very quickly.

If you’re searching for a specific term or keywords within a million files, use this:

grep -ir pattern /path/to/search/

The -i switch stands for case insensitive and r is for recursive.

Another good search, especially within source code is:

grep -rn 'pattern' /path/to/search

This adds the line number to output so you can easily see where a variable or function is used.

If you ever need to search amongst a million files, with thousands of different extensions, but you’re only interested in specific file type, then you can try something like this:

find . -type f -name "*.php" | xargs grep -i pattern

That’ll give you very specific searches. The above, as an example, search only actual files that ends with ‘.php’.

If the tree you’re searching contains whitespace in the paths names you can use find … -print0 and xargs -0:

find . -type f -name "*.php" -print0 | xargs -0 grep -i pattern

To search multiple keywords use this:

find . -xdev -type f -name "*.php" | xargs grep -iHE "keyword1|keyword2"

Have fun!

Jun 07

Finding & bashing stuff

We often use the find command to look for specific files like so:

 find . -type f -name '*.rb'

What if you want to do something with the result? With the find command, you can’t pipe the result because it is not a shell and it can’t spawn one.

Luckily find has a -exec function, so that you can do the following:

find . -type f -name '*.txt' -exec bash -c 'cat {}' \;

Note the \; at the end - that’s important if you’re executing the bash shell as it indicates the end of the line.

Jun 06

ifconfig.me

Awesome little site. Allows you to do this from any servers at command line:

curl ifconfig.me

It returns your external IP address!

Cheap SSH key tricks

Of course, most will know how to generate new SSH keys for passwordless entry like so:

ssh-keygen -t rsa

But I always want to add the key to the remote server first and then ssh in. So, I do this:

cat ~/.ssh/id_rsa.pub | ssh root@server.me "cat >> ~/.ssh/authorized_keys"

Jun 05

Ad-hoc keymapping in Vim

Ever since I saw a screencast by Gary Bernhardt from Destroy All Software, I’ve been using his awesomely handy key mapping trick in vim while writing scripts.

If you’re like me, you’ll spend most of your time writing small shell/bash scripts. I typically would write/edit my script, save my work, switch to another pane in iTerm2, test the script, back to my editing pane, edit, save, switch pane, test… repeat.

How about just create an ad-hoc mapping to do both saving and execution of the script all in one go?

In vim

:map ,s :w\|!./script_name.sh<cr>

That’s it.

Now, every time you’re done editing, hit ‘,s’ and you’ll save and execute the script, all in one go.

Perl Search & Replace One-Liner

Yes, it’s been used for a long long time and I have been using them for quite a while too. But the problem is, I always forget which one to leave out for a dry run.

For search and replace applied to files specified:

me@mycomp:~$ perl -pi -e ‘s/search/replace/g’ *.html

For non-applied search and replace (without the i argument):

me@mycomp:~$ perl -p -e ‘s/search/replace/g’ something.html

The way I try to use now to remember which one to exclude is this: -pi means print into as in print into the file(s) searched and p means print only [to screen].

Have fun!