Mastering the Linux column Command: A Comprehensive Guide with Examples

Jan 15, 2025 · 4 min read
Illustration of a laptop running the Linux column command with colorful aligned tables on screen, next to the Tux penguin mascot and floating charts
blog

The column command in Linux is a powerful yet often overlooked tool for formatting text output into columns. It is particularly useful for making messy or unstructured data more readable by aligning it into neat tables. Whether you’re a system administrator, developer, or just a Linux enthusiast, mastering the column command can save you time and make your work more efficient.

In this article, we’ll explore the column command in detail, covering all its use cases with practical examples. By the end, you’ll be able to use column like a pro!

1. Introduction to the column Command

The column command is part of the util-linux package and is available on most Linux distributions. It is designed to format text into columns, making it easier to read and analyze. It works with both piped input and files, and it supports various options for customizing the output.

2. Basic Usage

The simplest way to use column is to pipe text into it. By default, it formats the input into columns:

$ echo -e "Name Age\nJohn 25\nJane 30\nDoe 22" | column -t
Name  Age
John  25
Jane  30
Doe   22
  • The echo command generates a list of names and ages.
  • The column command formats the output into two columns.

3. Filling Rows Before Columns (-x)

By default, column fills columns before rows. However, you can use the -x option to fill rows first:

$ echo -e "Name Age\nJohn 25\nJane 30\nDoe 22" | column -x
Name Age John 25  Jane 30  Doe 22

The -x option ensures that rows are filled before columns.

4. Using column with Files

Instead of piping input, you can directly pass a file to column:

$ column file.txt
  • This formats the contents of file.txt into columns.

5. Formatting find Command Output

The column command is particularly useful for cleaning up the output of commands like find:

$ find /usr/share/fonts -name "*.otf" | column
  • This lists all .otf files in /usr/share/fonts and formats the output into columns.

6. Table Mode (-t)

The -t option enables table mode, which is ideal for formatting structured data:

$ cat /etc/passwd | column -t -s ":"
  • This formats the /etc/passwd file into a table, using : as the delimiter.

7. Adding Column Headers (-N)

The -N option allows you to add column headers to the output:

$ cat /etc/passwd | column -t -s ":" -N "USERNAME,PW,UID,GID,COMMENT,HOME,INTERPRETER"
  • This formats /etc/passwd into a table with custom column headers.

8. Hiding Columns (-H)

The -H option allows you to hide specific columns:

$ cat /etc/passwd | column -t -s ":" -N "USERNAME,PW,UID,GID,COMMENT,HOME,INTERPRETER" -H "PW"
  • This hides the PW (password) column from the output.

9. Reordering Columns (-O)

The -O option allows you to reorder columns:

$ cat /etc/passwd | column -t -s ":" -N "USERNAME,PW,UID,GID,COMMENT,HOME,INTERPRETER" -O "USERNAME,UID,GID,COMMENT,HOME,INTERPRETER"
  • This reorders the columns, removing the PW column.

10. JSON Output (-J)

The -J option outputs the data in JSON format:

$ cat /etc/passwd | column -t -s ":" -N "USERNAME,PW,UID,GID,COMMENT,HOME,INTERPRETER" -J -n "users"
  • This converts the /etc/passwd data into a JSON object named users.

11. Combining Options for Advanced Formatting

You can combine multiple options for more advanced formatting:

$ cat /etc/passwd | column -t -s ":" -N "USERNAME,PW,UID,GID,COMMENT,HOME,INTERPRETER" -H "PW" -O "USERNAME,UID,GID,COMMENT,HOME,INTERPRETER" -J -n "users"

This command:

  • Formats /etc/passwd into a table.
  • Adds column headers.
  • Hides the PW column.
  • Reorders the columns.
  • Outputs the result as JSON.

12. Conclusion

The column command is a versatile tool for formatting text output into columns, making it easier to read and analyze. Whether you’re working with command output or files, column can help you present data in a clean, tabular format.

Key Takeaways:

  • Use -t for table mode.
  • Use -s to specify a delimiter.
  • Use -N to add column headers.
  • Use -H to hide columns.
  • Use -O to reorder columns.
  • Use -J for JSON output.

For more advanced data manipulation, consider using tools like awk. However, for quick and easy formatting, column is an indispensable tool in your Linux toolkit.

Final Thoughts

The column command is a simple yet powerful way to organize and present data. By mastering its options, you can save time and make your work more efficient. Whether you’re a system administrator, developer, or just a Linux enthusiast, column is a tool you’ll want to keep handy.

Happy formatting! 🚀

Ahmed K Emara
Authors
Chief Technology Officer · FinTech & Platform Engineering
I’m a chief technology officer with 18+ years building and scaling technology organizations across regulated FinTech, payments, e-commerce, and enterprise platforms. Today I’m CTO at Tarmeez Capital, a Saudi FinTech financing and investing through Islamic Sukuk, where I took the platform from a four-person pre-launch team to 40+ people and a CMA/SAMA-licensed launch. I write about backend engineering, databases, security, microservices, and FinTech infrastructure — the problems I actually hit in production.