Friday, January 24, 2014

Using the 'diff' command in Unix

diff - compare files line by line.

Syntax
       diff [OPTION]... FILES

I can't imagine a world without the 'diff' utility.  In fact, this is where the commandline is superior to GUIs in my opnion.  Applications like "git" take full advantage of the power of 'diff'.

In many cases these days, most developers utilize version control from the beginning.  But, imagine you are wanting to see the difference between two versions of the same code.  This is quite common when working with legacy software.

In our example, say I have two versions of a simple file.  We'll call them a.c and b.c.  Here's a.c:


Now, in version "b" of the same application, there are a few more lines of code added:


True, you could go down the file line by line.  But, on a file that has several thousand lines, having a utility would be great.

So, we can do the following:

[johnny@starr ~/test/diff]$ diff -c a.c b.c
*** a.c Fri Jan 24 16:14:15 2014
--- b.c Fri Jan 24 16:16:35 2014
***************
*** 1,6 ****
--- 1,10 ----
  #include <stdio.h>
+ #include <stdlib.h>

  int main(void) {
      printf("Testing 123.\n");
+     while (1) {
+         printf("endless loop.\n");
+     }
      return 0;
  }

[johnny@starr ~/test/diff]$

At this point, we can see that the '+' symbol is used to define that b.c has added these lines.  If we would have removed lines, you would see the '-' symbol.

Notice that I used the '-c' flag, this is helpful in displaying diff in "context" format, giving us the symbols.

You can customize diff with tons of flags.  Check out 'man diff' to find out more.  There's also an application floating around called 'colordiff' which is a wrapper for diff that outputs in color.  Not required of course, but can be helpful in general.

How to use the 'apropos' command in Unix

apropos - A utility searches through man pages in Unix and returns results that match keywords.
 
Syntax
      apropos [-d] keyword ...


I've found at times, I'm not exactly sure which man-page to read up on when dealing with something new.  Man pages are often updated as well.  Being able to search for keywords will help you to locate topics to learn.

For example, what if I discover I need to generate a private key, but I'm not sure how to do this?  I could try the following:

]$ man private
No manual entry for private

But, let's try this instead:

]$ apropos private
gendsa(1)                - generate a DSA private key from a set of parameters
genpkey(1)               - generate a private key
genrsa(1)                - generate an RSA private key
libpng(3)                - Portable Network Graphics (PNG) Reference Library 1.4.8 

pkcs8(1)                 - PKCS#8 format private key conversion tool
pkey(1)                  - public or private key processing tool


Now we have a lead.  Let's take a look at 'genpkey'

]$ man genpkey

NAME
       genpkey - generate a private key

SYNOPSIS
       openssl genpkey [-out filename] [-outform PEM|DER] [-pass arg]
       [-cipher] [-engine id] [-paramfile file] [-algorithm alg] [-pkeyopt
       opt:value] [-genparam] [-text]

DESCRIPTION
       The genpkey command generates a private key.
...

'apropos' can also be emulated by man by using the -k flag.  Typing 'man -k private' generates the same results as 'apropos'.  In a sense, apropos is a wrapper for 'man -k' as well.

Thursday, January 23, 2014

How to use the 'alias' command in Unix

Alias - used to create a pseudonym for another command with, or without options.

Syntax
      alias [-p] [name[=value] ...]

      unalias [-a] [name ... ]

Key
   -p   Print the current values

   -a   Remove All aliases
 
The 'alias' command is a great way to add some customization to your command line.  I've found over the years, that the more you can reduce redundant typing, the more enjoyable your shell will be.  Alias allows you to trim the fat in some of your commands, especially if you like getting fancy with flags.  We will consider some of the ways you can use 'alias' to make life easier.

However, I have also found that the more you rely on aliasing, the more problems you can have with ambiguity.  Also, by customizing your shell too much, you can run the risk of de-standardization to the point where you are no longer proficient with foreign terminals.  Like all things, when used in moderation, alias can be a great resource.

Examples:

]$ alias ls='ls -a'  - update the 'ls' command to include the '-a' or 'all' flag.
]$ alias ..='cd ../' - '..' instead of having to type out the 'cd' command.
]$ alias cgrep='grep -n --color' - display line numbers / color with grep.

These are just a few choices you can make.  The only downside of typing them manually is that the aliases will be wiped out when you close your session.  If you would like to permanently use an alias, there are a few ways to go about it.

Method 1:
Create a file in your ~/ directory called .bash_aliases.  Simply add your aliases to this file and it will run when you initialize your next session.

Method 2:
The second option is to add alias commands line by line to your .bashrc file.  Depending on your OS, you may already have an rc file to edit.  If not, search the web for some decent examples.

Overall, 'alias' is a powerful tool and should be used judiciously.