How to Create Human Readable Output with Linux Commands

How to Create Human Readable Output with Linux Commands

The command line interface is a lot more “information dense” compared to the equivalent GUIs on Windows. With a single instruction, you can get a screen full of data, with columns, calculations, and colors. Most commands have additional options that allow you to modify their output so that you get the exact information you’re looking for.

Unfortunately, this power comes with a loss of usability. Many useful commands like netstat for example, generate their output in a series of columns with no fixed width, making it hard to parse by human readability standards. Even simple commands like listing the contents of a directory can contain additional information to confuse you.

In this article, I’ll show you how to do three things:

  1. Choose to display only certain columns of output
  2. Retain the color options for certain commands
  3. Make the columns neat and tidy when the output is all scrambled

1. Choosing to Display on Certain Columns

A simple command like “ls” has options to display a lot more data than just the filename and directory. For example, this command:

ls -l

The “-l” option lists a bunch of columns for each entry like the permissions, who owns it, time of creation etc as shown here:

 Create Human Readable Output with Linux Commands

Useful as this data is, it might be a bit too much. Luckily, Linux has a system of “piping” the output of commands to others and using this, we can pick and choose which columns to display using the “awk” command.

Let’s say that in the screenshot here, we want to only display the last column – the one with the file or folder name. We do it like this:

ls -l | awk '{ print $9 }'

This generates the following output:

Human Readable Output with Linux Commands

The key here is the part of the command that says:

awk '{ print $9 }'

Which displays only column number 9. We take the output from “ls -l” and “pipe” it to awk. If we want, we can display columns 1 and 9 like this:

ls -l | awk '{ print $1, $9 }'

You might be thinking that it’s too much effort to type out all the above each and every time you want to print a couple of columns. You might not even remember it! And you’d be right. Which is why you can just create a custom command of your own to run it automatically with a keyword of your choice!

2. Preserving Color Information for ls

Since ls is such a common command, you’ll probably be using it with awk a lot. You might have noticed in the previous examples, that piping the output to awk destroys the color information of ls. This is unfortunate, because a lot of us rely on color coding to quickly differentiate between folders, directories, and zipped files.

With ls, we can preserve this information by using the ” –color=always” option. So our awk command above becomes:

ls -l --color=always | awk '{ print $9 }'

And this produces the following:

Createing Human Readable Output with Linux Commands

Neat right?

3. Showing the Variable Width Columns in a Table

The “ls” command is very obliging. Its output is neat, and each column has a specific width. But what if you have a file where the columns don’t line up nicely? For example, take this file:

Need a fast and easy fix?
✔ Unlimited Managed Support
✔ Supports Your Software
✔ 2 CPU Cores
✔ 2 GB RAM
✔ 50 GB PCIe4 NVMe Disk
✔ 1854 GeekBench Score
✔ Unmetered Data Transfer

Now just $43 .99

cat animal-count

How to Create Human Readable Output with Linux
Since each animal name has a different width, the next column starts at a different place each time. In a long list, the output can become unreadable. Commands like netstat are impossible to decipher.

As before, we have a command called “column” into which we can pipe the output to make it display the data in a table. Here it is:

cat animal-count | column -t

The “-t” parameter specifies a table.

We get the following output:

 Readable Output with Linux Commands That’s much easier to read. We use “column” frequently while parsing the output of commands with a lot of data and variable width columns. It’s particularly useful for log files – sometimes containing thousands of lines of code.

We hope these tips and tricks make your Linux admin tasks a little easier with human readable output! If you use one of our managed Linux hosting plans, you can always as our system administrator to help you any aspect of managing your Linux server. They are available 24/7 and will take care of your request immediately.

If you liked this post please share it with your friends via social media networks, or if you have any question regarding this blog post, please feel free to leave a comment below and one of our system administrators will reply as soon as possible. Thanks!

2 thoughts on “How to Create Human Readable Output with Linux Commands”

Leave a Comment