Tag Archives: unix

Add Line Numbers to “cat” Output

cat can be used to print the contents of a file in a Terminal, and it’s often useful to number each line of the output (including empty lines).

Adding line numbers is as simple as adding the -n switch to the command:

cat -n filename.txt

Example Output, with -n switch:

$ cat -n filename.txt

1 Line 1
2 Line 2
3 Line 3
4 Line 4
5 Line 5

Example Output, without -n:

$ cat filename.txt

Line 1
Line 2
Line 3
Line 4
Line 5

 

Vim Text Editor: Use Spaces Instead of Tabs

By default, Vim uses tabs instead of spaces when you press the tab button.  There is an ongoing argument around the internet (which we won’t go into now, but may in the future!) on what you should use, but, either way, you may want/need to change your tabs to spaces.  These are often called “soft tabs” – every time you press the tab key, it inserts a certain amount of spaces.

You can do this quite easily in Vim, by editing your .vimrc file.  You simply add these two lines:

set expandtab
set tabstop=2

If you don’t want to edit your .vimrc file, or just want to make it a temporary change whilst you work on a certain file, you can also change the setting from within vim by prefixing the above commands with a colon:

:set expandtab
:set tabstop=2

This will set the space count to 2, on every tab, which is a common requirement (some guides recommend two spaces so lots of people use it).  Of course, it could easily be 4, 6, 8 etc.. so choose whichever you require.

Monitoring the Status of Multiple Linux/BSD/OSX/Unix Hosts

daz@scampi:~$ ruptime

You may run multiple Linux servers, or BSD servers, or even AIX servers, and need to monitor uptime, load averages, and users logged in from one central point.  @davetaylor on Twitter asked for the output of the ruptime command, which got me looking into the command in more detail – hence this post!

For this example, I’ll be using three Ubuntu Servers, hence the apt-get commands – for other systems, use it’s own inbuilt package management system.

Installing the “monitoring” node is simple:

apt-get install rwho

Once this is complete, it’ll install the rwhod daemon and also the ruptime command.

If you run ruptime now, you’ll get the output from only localhost:

daz@scampi:~$ ruptime
scampi        up   56+03:39,     1 user,  load 0.00, 0.00, 0.00

So, that’s one host done.  Now to install it on the remote machines.  Please note though, as rwhod works on network broadcast traffic, this will only work for hosts on your local network in the same broadcast domain.  To install a remote machine, we just need the rwhod daemon:

apt-get install rwhod

This will install just the daemon.  Now, when you run ruptime on the central host, it’ll use broadcast traffic to pull the stats from every machine, running rwhod, to the local machine.  In this example, I have three hosts:

daz@scampi:~$ ruptime
morph         up    2+13:21,     0 users,  load 0.00, 0.00, 0.00
noddy         up   38+00:02,     1 user,   load 0.01, 0.03, 0.00
scampi        up   56+03:42,     1 user,   load 0.00, 0.00, 0.00

You can do some sorting of results, but that’s pretty much it.  I guess if you wanted to see just one host, you’d be using grep or similar to just show the one line.

This is the manpage for ruptime on Ubuntu to show the options:  http://manpages.ubuntu.com/manpages/jaunty/man1/ruptime.1.html

If you notice you have 10 people logged into a host you know nothing about, then the rwho command will show you all users logged in, to any of the hosts:

daz@scampi:~$ rwho
daz      noddy:pts/2  Jul  7 13:31
daz      scampi:pts/2 Jul  7 13:12

It might be a small tool, but you may find it useful to monitor hosts quickly – especially if you just want to see vital information such as load averages and users logged in.