> 시스템 튜토리얼 > 리눅스 > 초보자를위한 예제가 포함 된 NumFMT 명령 자습서

초보자를위한 예제가 포함 된 NumFMT 명령 자습서

Christopher Nolan
풀어 주다: 2025-03-24 09:27:12
원래의
265명이 탐색했습니다.

The Numfmt Command Tutorial With Examples For Beginners

The Numfmt command converts the numbers to/from human readable format. It reads the numbers in various representations and reformats them in human readable format according to the specified options. If no numbers given, it reads the numbers from the standard input. It is part of the GNU coreutils package, so don't bother installing it. In this brief tutorial, let us see the usage of Numfmt command with some practical examples in Linux.

Table of Contents

Numfmt Command Examples

Picture a complex number, for example ‘1003040500’. Some Mathematics experts can easily find the human readable representation of this number in seconds. However, it is bit hard for many people including me. This is where Numfmt command comes in help.

Here are some example commands demonstrating the usage of the numfmt command in Linux.

1. Convert Numbers to Human Readable Format:

Run the following command to convert the given in human readable form.

$ numfmt --to=si 1003040500
1.1G
로그인 후 복사

Let us go for some really long and more complex number than the previous number. How about "10090008000700060005"? Bit hard, right? Yes. But the Numfmt command will display the human readable format of this number instantly.

$ numfmt --to=si 10090008000700060005
11E
로그인 후 복사

Here, si refers the International System of Units (abbreviated SI from systeme internationale , the French version of the name).

So, if you use si, the numfmt command will auto-scale numbers according to the International System of Units (SI) standard.

The Numfmt also uses the following unit options too.

  • iec and iec-i - Auto-scale numbers according to the International Electrotechnical Commission (IEC) standard.
  • auto - With this method, numbers with ‘K’,‘M’,‘G’,‘T’,‘P’,‘E’,‘Z’,‘Y’ suffixes are interpreted as SI values, and numbers with ‘Ki’, ‘Mi’,‘Gi’,‘Ti’,‘Pi’,‘Ei’,‘Zi’,‘Yi’ suffixes are interpreted as IEC values.
  • none - no auto-scaling.

Here is some more examples for the above options.

$ numfmt --to=iec 10090008000700060005
8.8E
로그인 후 복사
$ numfmt --to=iec-i 10090008000700060005
8.8Ei
로그인 후 복사

We have seen how to convert the numbers to human readable format. Now let us do the reverse. Meaning - We are going to convert the numbers from human readable format to their actual numeric representations.

2. Convert Numbers from Human Format to Numeric Value:

To convert numbers from the SI (International System of Units) format to its equivalent numeric value, run:

$ numfmt --from=si 1G
1000000000
로그인 후 복사
$ numfmt --from=si 1M
1000000
로그인 후 복사
$ numfmt --from=si 1P
1000000000000000
로그인 후 복사

We can also do this with iec and iec-i standards.

$ numfmt --from=iec 1G
1073741824
로그인 후 복사
$ numfmt --from=iec-i 1Gi
1073741824
로그인 후 복사
$ numfmt --from=auto 1G
1000000000
로그인 후 복사
$ numfmt --from=auto 1Gi
1073741824
로그인 후 복사

Like I already mentioned, when using "auto", the numbers with ‘K’,‘M’,‘G’,‘T’,‘P’,‘E’,‘Z’,‘Y’ suffixes are interpreted as SI values, and numbers with ‘Ki’, ‘Mi’,‘Gi’,‘Ti’,‘Pi’,‘Ei’,‘Zi’,‘Yi’ suffixes are interpreted as IEC values.

Numfmt command can also be used in conjunction with other commands. Have a look at the following examples.

3. Formatting a Number with Decimal Precision:

$ echo "12345678.12345678" | numfmt --format "%.2f"
로그인 후 복사

This command formats the number "12345678.12345678" with a decimal precision of two, resulting in the output "12,345,678.12".

4. Converting Binary to Decimal:

$ echo "101010" | numfmt --from=auto --to=decimal
로그인 후 복사

This command converts the binary number "101010" to its decimal equivalent, resulting in the output "42".

5. Formatting Large File Sizes in Human-Readable Format:

$ ls -l | awk '{print $5}' | numfmt --to=iec --suffix=B
로그인 후 복사

This command retrieves the file sizes from the output of the 'ls -l' command, then uses numfmt to format them in a human-readable format using IEC units (e.g., kilobytes, megabytes). The "--suffix=B" option appends the 'B' suffix to indicate bytes.

6. Converting Seconds to Minutes:

$ echo "180" | numfmt --from=auto --to=minutes
로그인 후 복사

This command converts the value "180" representing seconds to minutes, resulting in the output "3".

7. Formatting and Summing Numeric Values from a File:

$ cat numbers.txt | numfmt --field=2 --format="%'.2f" --sum
로그인 후 복사

Assuming the 'numbers.txt' file has multiple lines with two columns, this command formats the values in the second column with a comma as the thousands separator and a decimal precision of two. The "--sum" option calculates and displays the sum of those values.

8. Convert values expressed in SI format to their numeric equivalents:

$ echo 1G | numfmt --from=si
1000000000
로그인 후 복사

This command allows you to convert values expressed in SI format, such as "1G" representing 1 gigabyte, to their numeric equivalents using the "numfmt" utility.

9. Convert values expressed in IEC format to their numeric equivalents:

$ echo 1G | numfmt --from=iec
1073741824
로그인 후 복사

This command allows you to convert values expressed in IEC format, such as "1G" representing 1 gibibyte, to their numeric equivalents using the "numfmt" utility.

10. Display disk space information in human-readable SI units:

$ df -B1 | numfmt --header --field 2-4 --to=si
로그인 후 복사

This command allows you to display disk space information using the df command and convert the sizes to human-readable SI units using the numfmt utility.

11. Display the file sizes in a human-readable format using SI units:

$ ls -l | numfmt --header --field 5 --to=si
로그인 후 복사

This command allows you to list the files and directories with their sizes in a human-readable format using the ls command and the numfmt utility.

Heads Up: Please note that the ls and df commands already have "--human-readable" option to display the outputs in human readable form. The above examples are given just for the demonstration purpose only.

You can tweak the output using "--format" or "--padding" options as well.

12. Pad to 5 characters, right aligned using '--format' option:

$ du -s * | numfmt --to=si --format="%5f"
로그인 후 복사

13. Pad to 5 characters, left aligned using '--format' option:

$ du -s * | numfmt --to=si --format="%-5f"
로그인 후 복사

14. Pad to 5 characters, right aligned using '--padding' option:

$ du -s * | numfmt --to=si --padding=5
로그인 후 복사

15. Pad to 5 characters, left aligned using '--padding' option:

$ du -s * | numfmt --to=si --padding=-5
로그인 후 복사

For more options and usage, refer man pages.

$ man numfmt
로그인 후 복사

These examples illustrate various scenarios where the numfmt command can be useful for formatting, converting, and processing numeric values in Linux. Feel free to adapt and modify these commands according to your specific needs.

Remember, the specific usage and options for the numfmt command may vary slightly depending on your Linux distribution and the version of coreutils installed.

Frequently Asked Questions

Here's a FAQ (Frequently Asked Questions) for the numfmt command:

1. What is the numfmt command?

The numfmt command is a utility in Linux used for formatting and converting numeric values in various formats.

2. How can I use numfmt to format numbers?

You can use numfmt to format numbers by specifying options such as decimal precision, thousands separators, and padding.

3. Can numfmt convert numbers between different units?

Yes, numfmt can convert numbers between units like bytes, kilobytes, megabytes, etc., making it useful for handling data sizes.

4. Can numfmt handle numbers in scientific notation?

Yes, numfmt can handle numbers in scientific notation and format them according to specified options.

5. Can numfmt be used for batch processing or scripting?

Yes, numfmt can be used in scripts or for batch processing by providing input through command-line arguments or file redirection.

6. Does numfmt support custom formatting options?

Yes, numfmt provides various options to customize formatting, such as specifying the format, padding, and output field width.

7. Which Linux distributions include the numfmt command?

The numfmt command is typically included in coreutils, which is installed by default on most Linux distributions.

8. Can numfmt perform calculations or arithmetic operations on numbers?

No, numfmt is primarily focused on formatting and converting numeric values rather than performing calculations.

9. How can I use numfmt to format numbers in a CSV file?

You can pipe the output of a command that generates CSV data into numfmt to format specific columns or fields.

10. Where can I find more information about numfmt and its usage?

You can refer to the numfmt manual page by typing "man numfmt" in the terminal, or you can access online resources and tutorials for detailed examples and usage instructions.

Conclusion

In conclusion, the numfmt command in Linux is a versatile utility that simplifies the formatting and conversion of numeric values. It provides a convenient way to manipulate numbers, convert units, and customize the representation of data.

Whether you need to format numbers in a specific locale, convert file sizes, or apply custom formatting options, numfmt is a powerful tool at your disposal.

Resource:

  • numfmt manual

위 내용은 초보자를위한 예제가 포함 된 NumFMT 명령 자습서의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿