Americas

  • United States
sandra_henrystocker
Unix Dweeb

Lesser known but still handy Linux commands

How-To
Mar 27, 20175 mins
Data CenterLinux

nww linux predictions slide 1
Credit: Thinkstock

Some Linux commands that might not be sitting in your top favorites list can still come in very handy in a number of ways. In today’s post, we’re going to examine some interesting though somewhat unusual command options.

Looking at dates — with a twist

The date command is certainly one that is used quite often, but not necessarily with options that reformat the command’s output. In the date commands shown below, we display the normal output and then reformat the date in a format that makes it more convenient to create files or directories that can be easily sorted by date.

$ date
Mon Mar 27 10:51:11 EDT 2017
$ date +%Y-%m-%d
2017-03-27
$ ls -l | grep 2017-03-2
drwxrwxr-x  5 graycat  graycat     4096 Mar 20 17:00 2017-03-20
drwxrwxr-x  5 graycat  graycat     4096 Mar 21 17:00 2017-03-21
drwxrwxr-x  5 graycat  graycat     4096 Mar 22 17:00 2017-03-22
drwxrwxr-x  5 graycat  graycat     4096 Mar 23 17:00 2017-03-23
drwxrwxr-x  5 graycat  graycat     4096 Mar 24 17:00 2017-03-24
drwxrwxr-x  5 graycat  graycat     4096 Mar 25 17:00 2017-03-25
drwxrwxr-x  5 graycat  graycat     4096 Mar 26 17:00 2017-03-26
drwxrwxr-x  5 graycat  graycat     4096 Mar 27 17:00 2017-03-27

A cron job that runs every night might create directories used to store various system or application metrics.

#/bin/bash

dt=`date +%Y-%m-%d`
mkdir /apps/stats/$dt
mkdir /apps/stats/$dt/data
mkdir /apps/stats/$dt/logs
mkdir /apps/stats/$dt/notes

Numbering your lines of output

The line-numbering command, nl command can be helpful in many occassions — such as when you know you have an error on line 142 of a complex script. But that’s not all. You can use the nl command to add line numbers to just about anything — file contents or command output included.

Number the lines in a file:

$ nl Horton
     1  "Believe me," said Horton. "I tell you sincerely, my ears are
     2  quite keen and I heard him quite clearly. I know there's a
     3  person down there. And, what's more, quite likely there's two.
     4  Even three. Even four. Quite likely a family, for all that we
     5  know! A family with children just starting to grow."

     6  "So, please," Horton said, "as a favor to me, try not to disturb
     7  them. Just please let them be."

With an added option, you can number the lines in a file — even those that are blank

$ nl --body-numbering=a Horton
     1  "Believe me," said Horton. "I tell you sincerely, my ears are
     2  quite keen and I heard him quite clearly. I know there's a
     3  person down there. And, what's more, quite likely there's two.
     4  Even three. Even four. Quite likely a family, for all that we
     5  know! A family with children just starting to grow."
     6
     7  "So, please," Horton said, "as a favor to me, try not to disturb
     8  them. Just please let them be."

Of course, if you’re anything like me, typing such a lengthy option would just not work. I’d have to create an alias.

alias nla='nl --body-numbering=a'

Shuffling your output

And, while this isn’t likely to provide greater insight into Dr. Seuss’s “Horton Hears a Who”, you can choose to rearrange (i.e. shuffle) the lines in a text file using the shuf command.

$ shuf Horton
quite keen and I heard him quite clearly. I know there's a
person down there. And, what's more, quite likely there's two.
"So, please," Horton said, "as a favor to me, try not to disturb
"Believe me," said Horton. "I tell you sincerely, my ears are
them. Just please let them be."
know! A family with children just starting to grow."

Even three. Even four. Quite likely a family, for all that we

And, yes, the shuf command also works on input that is piped to it.

$ man shuf | shuf
              output at most COUNT lines
AUTHOR
       --help display this help and exit
NAME
       GNU coreutils home page: 
       should give you access to the complete manual.
       -o, --output=FILE

Maybe a useful application of shuf would involve putting your team’s names in a file and using the top line in the command output to determine who buys the pizza on Friday.

Scanning sockets

The ss command (if it’s installed) can provide a quick view of the sockets on your systems.

$ ss
Netid  State      Recv-Q Send-Q   Local Address:Port       Peer Address:Port
u_str  ESTAB      0      0                    * 8859                  * 8860
u_str  ESTAB      0      0      /var/run/dbus/system_bus_socket 8860                  * 8859
u_str  ESTAB      0      0                    * 8855                  * 8854
u_str  ESTAB      0      0                    * 10337                 * 10336
u_str  ESTAB      0      0                    * 8854                  * 8855
u_str  ESTAB      0      0                    * 10336                 * 10337
tcp    ESTAB      0      1152       172.30.0.28:ssh       192.161.76.51:64341

Or sockets with memory usage:

$ ss -m
Netid  State      Recv-Q Send-Q   Local Address:Port       Peer Address:Port
u_str  ESTAB      0      0                    * 34909                 * 34908
         skmem:(r0,rb212992,t0,tb212992,f0,w0,o0,bl0)
u_str  ESTAB      0      0                    * 8859                  * 8860
         skmem:(r0,rb212992,t0,tb212992,f0,w0,o0,bl0)
u_str  ESTAB      0      0      /var/run/dbus/system_bus_socket 8860  * 8859
         skmem:(r0,rb212992,t0,tb212992,f0,w0,o0,bl0)
u_str  ESTAB      0      0                    * 8855                  * 8854
         skmem:(r0,rb212992,t0,tb212992,f0,w0,o0,bl0)
u_str  ESTAB      0      0                    * 8854                  * 8855
         skmem:(r0,rb212992,t0,tb212992,f0,w0,o0,bl0)
u_str  ESTAB      0      0                    * 34908                 * 34909
         skmem:(r0,rb212992,t0,tb212992,f0,w0,o0,bl0)
tcp    ESTAB      0      1760       172.30.0.28:ssh       192.161.76.51:64035
         skmem:(r0,rb233880,t0,tb46080,f3328,w25344,o0,bl0)

Adding dimension to your file listings

The tree command provides a more “dimensional” view of your files and, for those of us who forget to go and clean out our file holdings from time to time, the hierarchical view can be quite surprising. Do I really have 114 directories and 355236 files?

114 directories, 355236 files

The structure of the data leading up to the final stats make it clear where the bulk of those files are stored in lines that look like these:

├── archive
│   ├── bfox
│   │   ├── backups
│   │   │   ├── production
│   │   │   └── test
│   │   ├── code
│   │   │   ├── production
│   │   │   └── test
│   │   └── images
│   │       ├── production
│   │       └── test

Of course, if you have tens of thousands of files, you’ll probably pipe the output to a file and take your time examing where all those files are stored.

sandra_henrystocker
Unix Dweeb

Sandra Henry-Stocker has been administering Unix systems for more than 30 years. She describes herself as "USL" (Unix as a second language) but remembers enough English to write books and buy groceries. She lives in the mountains in Virginia where, when not working with or writing about Unix, she's chasing the bears away from her bird feeders.

The opinions expressed in this blog are those of Sandra Henry-Stocker and do not necessarily represent those of IDG Communications, Inc., its parent, subsidiary or affiliated companies.

More from this author