Click here to Skip to main content
15,881,715 members
Articles / Operating Systems / Linux
Tip/Trick

New ways to try old Unix commands

Rate me:
Please Sign up or sign in to vote.
4.00/5 (2 votes)
10 Jun 2010CPOL3 min read 6.6K   2  
Not every Unix command sparks with novelty. After all, the OS has now been around for roughly forty years and the spark wears off. But there are some commands that pick up from where the basics run out of steam. Jump on a Linux box and try these out when you have a little free time. You might surprise yourself with some new tricks for your bag of Unix goodies.

Before we get started with the commands you may not have heard of before, here's a tired and true Unix command that we've all been using for decades, but maybe not this particular way. This grep command represents the simplest way to remove blanks lines from a file that I've ever seen.

grep . oldfile > newfile

Got that? It matches any lines with anything (i.e., not nothing) in them and tosses them into the second file. Hey, this is even easier than my old "grep -v ^$ oldfile > newfile" command that would specifically omit empty lines from the new file.

Now let's look at a command you might not have come across before. The comm command (think of "comm" as in "common" to be contrasted with "diff" as in "difference"). The comm command will attempt to display the lines that two files have in common. Here's an example:

$ cat file1
Remember when we were poor?
No.
Me neither.
The end.
$ cat file2
Let's slip a coin!
Flip what?
The end.
$ comm file1 file2
        Let's slip a coin!
        Flip what?
Remember when we were poor?
No.
Me neither.
                The end.


Pay attention to the way the output is indexed. The lines up against the left side of your screen are those that appear only in the first file. The lines in the middle column appear only in the second file. The lines in the rightmost column are those that appear in both files. In similar manner to diff, the ordering of the lines makes a big difference in how the command operates. If the files aren't sorted, comm will only find the first set of matching lines. Any additional matches will be ignored. Let's look at another example and see this in practice.

Let's say we have a file containing the letters a, b, c, x, a, b, c (one per line) and another containiny a, b, c, x, a, b, c. The comm command will only detect the first match.

$ comm file5 file6
                a
                b
                c
        f
        a
        b
        c
x
a
b
c

Like diff, the comm commands has some other options (like selecting whether to see only some of this output). Read the man page.
Another interesting command is called "tac". Yes, that's "cat" spelled backwards. And what would you expect such a command to do? It displays a file in reverse order -- upside down. So, our a, b, c, x, a, b, c file shows as:
$ tac myfile
c
b
a
x
c
b
a


Another interesting command is called "links", homonym for "lynx" and a command with a similar purpose in life. Like lynx, it's a test-based web browser. It lets you scroll up and down the page you direct it to, supports frames and is a generally easy to use and friendly tool.
Last on our list for today is "locate" with features in common with the which and find commands. The locate command pulls information about the files it finds from a prepared database, so it's quick. Do a locate on perl and you'll find the command, man pages, modules and who knows what else. When you don't have time to wait for a find command to complete, you might find what you're looking for very quickly with locate.

Nice commands from a operating system that hasn't lost its spark.

See other Unix tips here.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --