The uniq command

Imagine your manager wants you to remove all shells from the system that are not being used on the system. The logical place to start looking is the password file, since it will list all the shells that the users on your system needs. Currently on my system I'm using bash. On your system you might be using the same shell, or ksh, or csh or sh.

To determine what shells are used on your system:[8]

cut -d':' -f1,7 /etc/passwd
            

Running this command, we're returned a list of usernames and shells. Let's assume that we're only interested in the unique shells, so we're only going to cut field seven out of the password file.

Using the uniq command, we can remove duplicates, leaving only the unique things in the file.

There's one pre-requisite, and that is that uniq expects a sorted file to do the comparison of duplicated lines. So we must first pipe the output to sort.

cut -d':' -f7 /etc/passwd | sort | uniq
            

This command returns only the unique shells that are currently being used on your system.

Now how did this command work?

  1. First we cut out parts of the /etc/passwd file that we were interested in.

  2. We then grouped all similar shells together using sort.

  3. Finally we grabbed all the unique elements from this output.



[8] Just as a matter of interest, cut also takes a filename on the command line, so it doesn't have to be used as a pipe.