How to put line numbers on the output of a command and other useful Linux commands.

Posted: November 14, 2012. At: 6:27 PM. This was 5 years ago. Post ID: 4848

If you want to put line numbers in the output of a command; then feed it through the nl command. This will put the required line numbers there for you.

[john@deusexmachina]:~/Desktop> ls -hula | nl
     1	total 452K
     2	drwxr-xr-x  3 john john 4.0K Nov 14 10:46 .
     3	drwxr-xr-x 46 john john 4.0K Nov 14 11:39 ..
     4	-rw-rw-r--  1 john john  21K Nov 13 20:53 ACME IT Solutions.odt
     5	drwx------  2 john john 4.0K Nov 14 10:46 Ghost
     6	-rw-rw-r--  1 john john  65K Nov  6 21:57 apache.jpg
     7	-rw-rw-r--  1 john john 2.1K Nov 14 10:46 commands.txt
     8	-rw-rw-r--  1 john john  322 Nov  2 19:21 commands.txt~
     9	-rw-rw-r--  1 john john 125K Nov  2 22:18 dash-magazines.jpg
    10	-rw-rw-r--  1 john john  644 Nov 14 10:46 dvrip.txt
    11	-rw-r--r--  1 john john 8.3K Nov 14 10:46 examples.desktop
    12	-rw-rw-r--  1 john john  317 Nov 14 10:46 home.desktop
    13	-rw-rw-r--  1 john john 151K Nov  2 22:34 me-tv.jpg
    14	-rw-rw-r--  1 john john  255 Nov 14 10:46 root.desktop
    15	-rw-rw-r--  1 john john  231 Nov 14 10:46 tmp.desktop
    16	-rw-rw-r--  1 john john  29K Nov 10 15:50 updates.png
    17	lrwxrwxrwx  1 john john   48 Nov 13 20:53 |sdj_1.desktop -> /home/john/.e/e/fileman/favorites/|sdj_1.desktop
    18	lrwxrwxrwx  1 john john   48 Nov 13 20:53 |sdk_0.desktop -> /home/john/.e/e/fileman/favorites/|sdk_0.desktop
    19	lrwxrwxrwx  1 john john   48 Nov 13 20:53 |sdk_1.desktop -> /home/john/.e/e/fileman/favorites/|sdk_1.desktop
    20	lrwxrwxrwx  1 john john   48 Nov 13 20:53 |sr0_0.desktop -> /home/john/.e/e/fileman/favorites/|sr0_0.desktop
[john@deusexmachina]:~/Desktop>

This command is the Linux equivalent of the route PRINT command on Windows. The route -FC command will print out the kernel routing table.

[john@deusexmachina]:~/Desktop> route -FC
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         192.168.1.1     0.0.0.0         UG    0      0        0 eth1
link-local      *               255.255.0.0     U     1000   0        0 eth1
192.168.1.0     *               255.255.255.0   U     1      0        0 eth1
Kernel IP routing cache
Source          Destination     Gateway         Flags Metric Ref    Use Iface
cf-173-245-61-1 192.168.1.2     192.168.1.2     l     0      0      315 lo
syd01s13-in-f5. 192.168.1.2     192.168.1.2     l     0      3      858 lo
192.168.1.2     kwaimuk.canonic 192.168.1.1           0      1        1 eth1
www-slb-ecmp-06 192.168.1.2     192.168.1.2     l     0      1      637 lo
a184-84-221-32. 192.168.1.2     192.168.1.2     l     0      1      283 lo
localhost       deusexmachina   deusexmachina   l     0      0     1166 lo
192.168.1.2     achernar.canoni 192.168.1.1           0      0      552 eth1
192.168.1.2     alkes.canonical 192.168.1.1           0      0      364 eth1
192.168.1.2     cf-199-27-135-5 192.168.1.1           0      0        9 eth1
192.168.1.2     70.92.189.91.lg 192.168.1.1           0      10      36 eth1
hg-in-f125.1e10 192.168.1.2     192.168.1.2     l     0      0      626 lo
syd01s13-in-f8. 192.168.1.2     192.168.1.2     l     0      0       39 lo
syd01s13-in-f22 192.168.1.2     192.168.1.2     l     0      3     1830 lo
syd01s13-in-f0. 192.168.1.2     192.168.1.2     l     0      0       18 lo
192.168.1.2     cf-173-245-61-1 192.168.1.1           0      0       11 eth1
192.168.1.2     cf-173-245-61-1 192.168.1.1           0      1       23 eth1
192.168.1.2     achernar.canoni 192.168.1.1           0      0      371 eth1
192.168.1.2     192.168.1.1     192.168.1.1           0      0     1182 eth1
alkes.canonical 192.168.1.2     192.168.1.2     l     0      0        4 lo
123.104.233.72. 192.168.1.2     192.168.1.2     l     0      0        5 lo
cf-199-27-135-5 192.168.1.2     192.168.1.2     l     0      0      524 lo
kwaimuk.canonic 192.168.1.2     192.168.1.2     l     0      0      437 lo
192.168.1.2     alkes.canonical 192.168.1.1           0      0      275 eth1
192.168.1.1     192.168.1.2     192.168.1.2     il    0      0     1724 lo
192.168.1.2     cf-199-27-135-5 192.168.1.1           0      0       19 eth1
deusexmachina   localhost       localhost       l     0      0     1181 lo
syd01s13-in-f4. 192.168.1.2     192.168.1.2     l     0      3     2595 lo
wordpress.com   192.168.1.2     192.168.1.2     l     0      0       19 lo
122-148-3-200.c 192.168.1.2     192.168.1.2     l     0      1     1177 lo
syd01s13-in-f2. 192.168.1.2     192.168.1.2     l     0      1      100 lo
192.168.1.2     hg-in-f125.1e10 192.168.1.1           0      1        1 eth1
achernar.canoni 192.168.1.2     192.168.1.2     l     0      0      225 lo
192.168.1.2     mulberry.canoni 192.168.1.1           0      1        2 eth1
localhost       deusexmachina   deusexmachina   l     0      0     1184 lo
[john@deusexmachina]:~/Desktop>

This is what the output of the route PRINT command looks like on Windows 7.

C:\Users\flynn>route PRINT
===========================================================================
Interface List
 15...00 13 46 3a 02 83 ......Realtek RTL8139/810x Family Fast Ethernet NIC
 11...6c f0 49 b5 e6 2a ......Realtek PCIe GBE Family Controller
 20...08 00 27 00 6c 5a ......VirtualBox Host-Only Ethernet Adapter
  1...........................Software Loopback Interface 1
 12...00 00 00 00 00 00 00 e0 Microsoft ISATAP Adapter
 13...00 00 00 00 00 00 00 e0 Teredo Tunneling Pseudo-Interface
 14...00 00 00 00 00 00 00 e0 Microsoft ISATAP Adapter #2
 16...00 00 00 00 00 00 00 e0 Microsoft ISATAP Adapter #3
===========================================================================
 
IPv4 Route Table
===========================================================================
Active Routes:
Network Destination        Netmask          Gateway       Interface  Metric
          0.0.0.0          0.0.0.0      192.168.1.1      192.168.1.2     20
        127.0.0.0        255.0.0.0         On-link         127.0.0.1    306
        127.0.0.1  255.255.255.255         On-link         127.0.0.1    306
  127.255.255.255  255.255.255.255         On-link         127.0.0.1    306
      192.168.1.0    255.255.255.0         On-link       192.168.1.2    276
      192.168.1.2  255.255.255.255         On-link       192.168.1.2    276
    192.168.1.255  255.255.255.255         On-link       192.168.1.2    276
     192.168.56.0    255.255.255.0         On-link      192.168.56.1    276
     192.168.56.1  255.255.255.255         On-link      192.168.56.1    276
   192.168.56.255  255.255.255.255         On-link      192.168.56.1    276
        224.0.0.0        240.0.0.0         On-link         127.0.0.1    306
        224.0.0.0        240.0.0.0         On-link      192.168.56.1    276
        224.0.0.0        240.0.0.0         On-link       192.168.1.2    276
  255.255.255.255  255.255.255.255         On-link         127.0.0.1    306
  255.255.255.255  255.255.255.255         On-link      192.168.56.1    276
  255.255.255.255  255.255.255.255         On-link       192.168.1.2    276
===========================================================================
Persistent Routes:
  None
 
IPv6 Route Table
===========================================================================
Active Routes:
 If Metric Network Destination      Gateway
  1    306 ::1/128                  On-link
 20    276 fe80::/64                On-link
 15    276 fe80::/64                On-link
 20    276 fe80::209f:983f:30e1:8283/128
                                    On-link
 15    276 fe80::e15a:52b6:dff:efe3/128
                                    On-link
  1    306 ff00::/8                 On-link
 20    276 ff00::/8                 On-link
 15    276 ff00::/8                 On-link
===========================================================================
Persistent Routes:
  None

As you can see; the output is slightly different but still outputs useful information. There are some interesting commands on this page: http://securitron.securitronlinux.com/ubuntu-karmic.php my Ubuntu Karmic tips page.

And this is my Linux tips and tricks page with a nice set of tips on creating a custom Linux shell prompt: http://www.securitronlinux.com/my-linux-tips-and-tricks-page-part-1/.

No comments have been made. Use this form to start the conversation :)

Leave a Reply