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.
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
$ 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 ;-)
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. Easy and clean.
The last little trick is just a way to list all the files in a directory in a single line. No tricks with piping the output from ls and removing linefeeds required. If you just type:
$ echo * echo * file1 file10 file100 file101 file102 file103 file104 file105 file106 file107 file108 file109 file11 file110 file111 file12 file13 file14 file15 file16 file17 file18 file19 file2 file20 file21 file22 file23 file24 file25 file26 file27 file28 file29 file3 file30 file31 file32 file33 file34 file35 file36 file37 file38 file39 file4 file40 file41 file42 file43 file44 file45 file46 file47 file48 file49 file5 file50 file51 file52 file53 file54 file55 file56 file57 file58 file59 file6 file60 file61 file62 file63 file64 file65 file66 file67 file68 file69 file7 file70 file71 file72 file73 file74 file75 file76 file77 file78 file79 file8 file80 file81 file82 file83 file84 file85 file86 file87 file88 file89 file9 file90 file91 file92 file93 file94 file95 file96 file97 file98 file99
You will get a single line containing all of your file names. This works since the shell expands the asterisk into a list of file names which the echo command is as happy to echo as anything else.