tree-like ls

tree” is a very nice command that’s not always available on any systems. As the name implies it lists the contents of directories in a tree-like format.

An example is:

$ tree
.
|-- Boards
|-- Members
|-- Messages
|-- Settings.pl
|-- Sources
|-- Variables
|-- YaBB.cgi
|-- english.lng
|-- template.html
`-- template2.html

Well, a similar result can be achieved in Bash with a nifty little one-liner:

ls -R | grep ":$" | sed -e 's/:$//' \
   -e 's/[^-][^\/]*\//--/g' -e 's/^/   /' -e 's/-/|/'

NOTE: long line, I had to break/fold it, sorry about that. Copy&paste is fine as long as the backslash (“\”) at the end of the first line is preserved.

A short explanation of the cryptic syntax above is as follows:

  • initially a recursive listing of the current directory is done: ls -R
  • pipe
  • any output other that the directory names, identified by “:” at the very end of each line (hence “:$“), is filtered out: grep ":$"
  • pipe
  • finally there’s a little of “sedmagic finding and replacing any hierarchy level (“/“) with one or more dashes (“-“):
    sed -e 's/:$//' -e 's/[^-][^\/]*\//--/g' -e 's/^/ /' -e 's/-/|/'

*NIX chat

Well, no, not chat in the common/modern sense but did you know you can display which users are connected to the same system you’re logged on and send them messages?

First off, displaying users connected to the same system is, for sysadmins, a basic and quite an important monitoring command.

The syntax is extremely simple: “w“!

Example:

$ w
10:02:12 up 2 min,  2 users,  load average: 0.33, 0.18, 0.07
USER      TTY      FROM             LOGIN@   IDLE   JCPU   PCPU  WHAT
<userid1> pts/0    <src IP addr>    10:02    0.00s  4.83s  0.08s bash
<userid2> pts/1    <src IP addr>    10:02    0.00s  4.83s  0.08s w

Now, sending a message to <userid1> from <userid2> is as easy as:

echo "I'm about to disconnect you ;)" | write <userid1> pts/0

On <userid1>‘s console the following will be printed:

Message from <userid2>@<server> on pts/1 at 10:04 ...
I'm about to disconnect you ;)
EOF

Please notice that the TTY being used, pts/0 in the example, may differ from one OS to another.
For instance, on Mac OS X, it should be ttys00x.

The same is possible on Cisco routers by means of “send” command.