The Greater Fool

Posts for Tag: CLI

Shells, Terminals, and tmux, oh my!

I've been working in a lot of non-microsoft development lately, namely the xAMP stack. Trying to find a suitable development environment has been a challenge. I've tried Eclipse, Netbeans, MAMP, MacVim and found all of them wanting in some fashion or another. It's either too bloated or slow or just feels wrong. Finally this weekend, I was determined to get back to the root and use Terminal (see what I did there?)

Learning to go CLI after a career in IDE is somewhat intimidating. Luckily, I started learning this stuff in the *nix heyday of the early nineties (I remember reading on USENET that some Finnish guy name Linus was going to port unix to the x86 platform). So I was at least comfortable in vi and bash, if not proficient. Started with plain ole Terminal and quickly moved to iTerm2 and quickly built tmux and I was off. I could do most things on the CLI, from creating directories to vim to executing the occasional sudo make install to get stuff running. 

Ironically, the most taxing thing to learn how to do was to copy a directory and all its contents to a new directory. I tried copydir and cpdir without any success. With a little help from my favorite search engine, I found a nice unix command line primer on ibm's site of all places. If you ever need to copy the contents of a directory from one to another, here it is:

tar cf - /path/to/original |  \
  (mkdir -p /path/to/copy; cd /path/to/copy; tar xvf -)

I'm sure there will be more. Now tomorrow I have to switch back to Windows and MS development. I hope I remember how to start Visual Studio.

SymKat» SymKat | Five Text Processing Tools You Should Know

awk

With grep we found out how we could pull just the matching lines from a file. All we care about is the IP addresses used to log into symkat, though. We have a lot of information we don’t need and it’s making our eyes glaze over.

One trick we can do with awk is to show only the columns of information we want to know. In this case it’s the 8th column.

01 symkat@symkat:~$ awk '{print $8}' auth.log
02 10.0.0.234
03 172.16.32.56
04 192.168.1.100
05 10.0.0.234
06 10.0.0.234
07 172.16.32.56
08 10.0.0.234
09 172.16.32.56
10 172.16.32.56
11 10.0.0.234
12 10.0.0.234
13 10.0.0.234
14 192.168.1.100
15 10.0.0.234
16 symkat@symkat:~$

Awk expects to be given a pattern to match and an action and that’s exactly what we gave it. To understand how this works we have to understand a few key points about awk:

  1. Awk expects to be given a pattern to match and an action to run on input
  2. Awk assigns $1, $2, $3, and so on to correlate to the values of the input, split by white space. $1 = symkat.com in this example. $2 = sshd[]:

We did not include a pattern.  As such, awk will match on all lines.  The action we took was to print the eighth column.

It is worth noting that if I wanted additional information, such as both the username and the IP address a concatenation operator is not needed, the assumption is quoted text is literal (for instance, this quoting of a space between $6th value (username) and $8th value (IP Address):

01 symkat@symkat:~$ awk '{print $6 " " $8 }' auth.log
02 symkat 10.0.0.234
03 symkat 172.16.32.56
04 symkat 192.168.1.100
05 symkat 10.0.0.234
06 symkat 10.0.0.234
07 symkat 172.16.32.56
08 symkat 10.0.0.234
09 symkat 172.16.32.56
10 symkat 172.16.32.56
11 symkat 10.0.0.234
12 symkat 10.0.0.234
13 symkat 10.0.0.234
14 symkat 192.168.1.100
15 symkat 10.0.0.234
16 symkat@symkat:~$

One of my favorite unix commands along with grep, awk, gets a good writeup by SymKat. There's a reasonable Windows port at http://gnuwin32.sourceforge.net/packages/gawk.htm as part of the Gnu Win32 OSS project.