Unix How To: Useful Shortcuts for Unix Geeks

By  

In this week's column, we're going to take a look at several commands that might save you some time. Each of these commands is easy to use and easy to remember, but just far enough off the beaten track that you might not have used them before.

nl

The first command is one that I discovered after decades of using sed, awk and various bash commands to force line numbering. There are many ways to do this, of course, but none is easier than using the nl (line number filtering) command. The nl command simply prepends a line number on every line in a file. For example:

$ nl abc
     1  a
     2  b
     3  c
     4  d
     5  e
     6  f
     7  g
     ... (and so on)

There are many options. Even with such a seemingly simple function as line numbering, Unix is going to allow you to select how the particular numbering is applied. For example, by default, nl numbers only lines thatcontain text. You can elect to number only lines that contain or start with particular text as in the third
command below.

$ nl emerg
     1  This is a test of your emergency broadcast system.
     2  Beep boop!  Beep boop boop!

     3  If this was a real emergency...         it wouldn't have worked.

     4  ;-)
$ nl -ba emerg
     1  This is a test of your emergency broadcast system.
     2  Beep boop!  Beep boop boop!
     3
     4  If this was a real emergency...         it wouldn't have worked.
     5
     6  ;-)
$ nl -b p^T file1
       Remember when we were poor?
       No.
       Me neither.
     1  The end.

In fact, nl even provides an option to not number. The result is that the lines are indented as if line numbering were applied.

$ nl -bn emerg
       This is a test of your emergency broadcast system.
       Beep boop!  Beep boop boop!

       If this was a real emergency...          it wouldn't have worked.

       ;-)

And, just in case you're tired of the old 1, 2, 3, you can select to increment your lines numbers differently. In this example, we start with line number 10 and increment each line by 10.

$ nl -v10 -i10 emerg
    10  This is a test of your emergency broadcast system.
    20  Beep boop!  Beep boop boop!

    30  If this was a real emergency...         it wouldn't have worked.

    40  ;-)

pkill

If you manage Unix systems, you've probably made us of the pkill command to kill a process without first having to run a command to determine the process's process ID. Here's a version of the pkill command that will kill all processes belonging to a particular user. If one particular user has trashed performance on a system by starting up a pile of processes and then leaving them running and going home, you can kill them all without even a hint of a struggle with this command:

# pkill -U 1234

where 1234 is his UID.

Join us:
Facebook

Twitter

Pinterest

Tumblr

LinkedIn

Google+

Answers - Powered by ITworld

ITworld Answers helps you solve problems and share expertise. Ask a question or take a crack at answering the new questions below.

Join us:
Facebook

Twitter

Pinterest

Tumblr

LinkedIn

Google+

Ask a Question