Saturday, August 13, 2011

Linux Commands - uniq,tee and who


This is another sequel to my previous posts regarding the Linux commands. In this post, I want to explain the uniq, tee and who commands.

Uniq

This  command is used to handle the duplicates in the file content. Suppose I have a file a.text.

$ cat a.text 
Condemned
Condemned
Underworld
Underworld
Vantage point
Toy Story
Toy Story 2

Now use uniq command to display the uniq lines only.

$ uniq a.text 
Condemned
Underworld
Vantage point
Toy Story
Toy Story 2

If we want see only the lines that are not duplicated, use -u flag.

$ uniq -u a.text 
Vantage point
Toy Story
Toy Story 2

If we want to avoid the non duplicated lines, that is opposite to the previous one, use -d flag.

$ uniq -d a.text 
Condemned
Underworld

If want to set a counter on each lines that denotes how much times each line is displayed, use the -c flag.

$ uniq -c a.text 
      2 Condemned
      2 Underworld
      1 Vantage point
      1 Toy Story
      1 Toy Story 2

Tee

tee is normally used to split the output of a program so that it can be seen on the display and also be saved in a file. The command can also be used to capture intermediate output before the data is altered by another command or program.

Run the following command to get a directory listing on your terminal, while also redirecting the output to a file named b.text.

$ ls -al | tee b.text

Who

If you want to know which users are currently logged in to your Linux system, which console they're using, and the date and time they logged in, use the who command.

$ who
ajay     tty7         2011-08-12 13:45 (:0)
ajay     pts/0        2011-08-12 13:46 (:0.0)

In the output shown here, the term tty stands for teletype. In the olden days of computing, a terminal was just a keyboard with an attached printer, so you read everything off the teletype.

If you've logged in with multiple virtual consoles and changed your identity on any of them, you may have some trouble figuring out who you are--or at least what user is logged in to the console you're using. If you find yourself in such an identity crisis, try this related command,

$ whoami
ajay

Thanks

AJAY

No comments:

Post a Comment