How to use the command 'numfmt' (with examples)

How to use the command 'numfmt' (with examples)

The numfmt command is a versatile tool that is part of the GNU Core Utilities, aimed at formatting numbers for human readability, especially those with large values that can be cumbersome to read and interpret at a glance. Useful in various fields such as data analysis, shell scripting, and system administration, numfmt enables conversion between different scales and formatting options including SI units, IEC units, and custom padding.

Use case 1: Convert 1.5K (SI Units) to 1500

Code:

numfmt --from=si 1.5K

Motivation:

When dealing with data sets or files that use SI units like K, M, G, etc., it’s often necessary to convert these human-readable figures into exact numbers for computation. This conversion is vital in scripts where precise numerical values are required for calculations or comparisons.

Explanation:

  • --from=si: This option specifies that the input number (1.5K) is in SI units. SI units use powers of ten (1K = 1000), which are commonly used in contexts such as disk usage, memory size, etc.
  • 1.5K: The input number, where K denotes thousands in SI units.

Example output:

1500

Use case 2: Convert 5th field (1-indexed) to IEC Units without converting header

Code:

ls -l | numfmt --header=1 --field=5 --to=iec

Motivation:

When listing files using ls -l, the file sizes are given in bytes by default. In situations where human-readability is crucial, such as generating reports or making assessments based on file sizes, converting these sizes to more interpretable units without affecting the headers is essential.

Explanation:

  • ls -l: Lists files in the current directory with detailed information, where the 5th column typically represents the file size in bytes.
  • |: A pipe operator that passes the output of ls -l as input to the numfmt command.
  • --header=1: Indicates that the first row should be treated as a header and not converted.
  • --field=5: Specifies that only the fifth column (the file size) should be converted, preserving other output columns.
  • --to=iec: Converts the specified field into IEC units, using powers of 1024 (e.g., KiB, MiB), which are especially common in computing for representing file and memory sizes.

Example output:

-rw-r--r-- 1 user group 1.5K Oct 1 10:00 example.txt

Use case 3: Convert to IEC units, pad with 5 characters, left aligned

Code:

du -s * | numfmt --to=iec --format="%-5f"

Motivation:

When displaying file sizes in a shell script or command output, alignment and formatting significantly enhance readability, especially when inspecting directory sizes. Consistent column widths make it easier to scan through multiple entries at once.

Explanation:

  • du -s *: Computes the total disk usage for each item in the current directory, printing size alongside the directory name.
  • |: Pipes the output into numfmt.
  • --to=iec: Converts printed sizes to IEC units for better human readability.
  • --format="%-5f": Adds a format specifier, where % signals a formatting operation. The - indicates left alignment, 5 is the width (padding the output to at least 5 characters if necessary), and f denotes a generic floating-point number.

Example output:

1.9M  directory_one
820K  directory_two

Conclusion:

The numfmt command serves as a straightforward yet powerful tool for transforming numerical data into more interpretable formats, suitable for human consumption or further computational processing. Each use case showcases different aspects of numfmt, from converting between unit systems to customizing alignment and formatting, thereby equipping users with the ability to handle numbers more intuitively within command-line environments.

Related Posts

How to Use the `perldoc` Command (with Examples)

How to Use the `perldoc` Command (with Examples)

perldoc is a command-line tool that provides access to Perl documentation, allowing users to read about Perl’s functions, variables, modules, and other components directly from the terminal.

Read More
How to Use the Command 'pbpaste' (with Examples)

How to Use the Command 'pbpaste' (with Examples)

The pbpaste command is a useful utility on macOS systems that allows you to perform various operations with the contents of your clipboard.

Read More
How to Use the Command 'git bug' (with Examples)

How to Use the Command 'git bug' (with Examples)

The git bug command is a powerful tool that integrates bug tracking directly into the Git infrastructure, offering a seamless way to track and manage issues alongside your code.

Read More