六个例子带你入门 size 命令
size
![](/data/attachment/album/201804/01/191748w444lcxl4glvqlqa.jpg)


正如你所知道的那样，Linux 中的目标文件或着说可执行文件由多个段组成（比如文本段和数据段）。若你想知道每个段的大小，那么确实存在这么一个命令行工具 —— 那就是 `size`。在本教程中，我们将会用几个简单易懂的案例来讲解该工具的基本用法。


在我们开始前，有必要先声明一下，本文的所有案例都在 Ubuntu 16.04 LTS 中测试过了。


### Linux size 命令


`size` 命令基本上就是输出指定输入文件各段及其总和的大小。下面是该命令的语法：



```
size [-A|-B|--format=compatibility]
            [--help]
            [-d|-o|-x|--radix=number]
            [--common]
            [-t|--totals]
            [--target=bfdname] [-V|--version]
            [objfile...]

```

man 页是这样描述它的：



> 
> GNU 的 `size` 程序列出参数列表中各目标文件或存档库文件的段大小 — 以及总大小。默认情况下，对每个目标文件或存档库中的每个模块都会产生一行输出。
> 
> 
> `objfile...` 是待检查的目标文件。如果没有指定，则默认为文件 `a.out`。
> 
> 
> 


下面是一些问答方式的案例，希望能让你对 `size` 命令有所了解。


### Q1、如何使用 size 命令？


`size` 的基本用法很简单。你只需要将目标文件/可执行文件名称作为输入就行了。下面是一个例子：



```
size apl

```

该命令在我的系统中的输出如下：


[![How to use size command](/data/attachment/album/201804/01/191758d4jmjrewamvg49v0.png)](https://www.howtoforge.com/images/command-tutorial/big/size-basic-usage.png)


前三部分的内容是文本段、数据段和 bss 段及其相应的大小。然后是十进制格式和十六进制格式的总大小。最后是文件名。


### Q2、如何切换不同的输出格式？


根据 man 页的说法，`size` 的默认输出格式类似于 Berkeley 的格式。然而，如果你想的话，你也可以使用 System V 规范。要做到这一点，你可以使用 `--format` 选项加上 `SysV` 值。



```
size apl --format=SysV

```

下面是它的输出：


[![How to switch between different output formats](/data/attachment/album/201804/01/191808pvv4lh444jbgc55f.png)](https://www.howtoforge.com/images/command-tutorial/big/size-format-option.png)


### Q3、如何切换使用其他的单位？


默认情况下，段的大小是以十进制的方式来展示。然而，如果你想的话，也可以使用八进制或十六进制来表示。对应的命令行参数分别为 `o` 和 `-x`。


[![How to switch between different size units](/data/attachment/album/201804/01/191810fn86ppnn8prc66dr.png)](https://www.howtoforge.com/images/command-tutorial/big/size-o-x-options.png)


关于这些参数，man 页是这么说的：



> 
> -d
> 
> 
> -o
> 
> 
> -x
> 
> 
> --radix=number
> 
> 
> 使用这几个选项，你可以让各个段的大小以十进制（`-d` 或 `--radix 10`）、八进制（`-o` 或 `--radix 8`）；或十六进制（`-x` 或 `--radix 16`）数字的格式显示。`--radix number` 只支持三个数值参数（8、 10、 16）。总共大小以两种进制给出； `-d` 或 `-x` 的十进制和十六进制输出，或 `-o` 的八进制和十六进制输出。
> 
> 
> 


### Q4、如何让 size 命令显示所有对象文件的总大小？


如果你用 `size` 一次性查找多个文件的段大小，则通过使用 `-t` 选项还可以让它显示各列值的总和。



```
size -t [file1] [file2] ...

```

下面是该命令的执行的截屏：


[![How to make size command show totals of all object files](/data/attachment/album/201804/01/191811zp5czlxglktia3tc.png)](https://www.howtoforge.com/images/command-tutorial/big/size-t-option.png)


`-t` 选项让它多加了最后那一行。


### Q5、如何让 size 输出每个文件中公共符号的总大小？


若你为 `size` 提供多个输入文件作为参数，而且想让它显示每个文件中公共符号（指 common segment 中的 symbol）的大小，则你可以带上 `--common` 选项。



```
size --common [file1] [file2] ...

```

另外需要指出的是，当使用 Berkeley 格式时，这些公共符号的大小被纳入了 bss 大小中。


### Q6、还有什么其他的选项？


除了刚才提到的那些选项外，`size` 还有一些一般性的命令行选项，比如 `v` （显示版本信息）和 `-h` （可选参数和选项的汇总）。


[![What are the other available command line options](/data/attachment/album/201804/01/191819oo4elerljslg7q24.png)](https://www.howtoforge.com/images/command-tutorial/big/size-v-x1.png)


除此之外，你也可以使用 `@file` 选项来让 `size` 从文件中读取命令行选项。下面是详细的相关说明：



> 
> 读出来的选项会插入并替代原来的 `@file` 选项。若文件不存在或着无法读取，则该选项不会被替换，而是会以字面意义来解释该选项。文件中的选项以空格分隔。当选项中要包含空格时需要用单引号或双引号将整个选项包起来。通过在字符前面添加一个反斜杠可以将任何字符（包括反斜杠本身）纳入到选项中。文件本身也能包含其他的 `@file` 选项；任何这样的选项都会被递归处理。
> 
> 
> 


### 结论


很明显，`size` 命令并不适用于所有人。它的目标群体是那些需要处理 Linux 中目标文件/可执行文件结构的人。因此，如果你刚好是目标受众，那么多试试我们这里提到的那些选项，你应该做好每天都使用这个工具的准备。想了解关于 `size` 的更多信息，请阅读它的 [man 页](https://linux.die.net/man/1/size) 。




---


via: <https://www.howtoforge.com/linux-size-command/>


作者：[Himanshu Arora](https://www.howtoforge.com) 译者：[lujun9972](https://github.com/lujun9972) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
# Linux size Command Tutorial for Beginners (6 Examples)

### On this page

[Linux size command](#linux-size-command)[Q1. How to use size command?](#q-how-to-use-size-command)[Q2. How to switch between different output formats?](#q-how-to-switch-between-different-output-formats)[Q3. How to switch between different size units?](#q-how-to-switch-between-different-size-units)[Q4. How to make size command show totals of all object files?](#q-how-to-make-size-command-show-totals-of-all-object-files)[Q5. How to make size print total size of common symbols in each file?](#q-how-to-make-size-print-total-size-of-common-symbols-in-each-file)[Q6. What are the other available command line options?](#q-what-are-the-other-available-command-line-options)[Conclusion](#conclusion)

As some of you might already know, an object or executable file in Linux consists of several sections (like txt and data). In case you want to know the size of each section, there exists a command line utility - dubbed **size** - that provides you this information. In this tutorial, we will discuss the basics of this tool using some easy to understand examples.

*But before we do that, it's worth mentioning that all examples mentioned in this article have been tested on Ubuntu 16.04LTS.*

## Linux size command

The size command basically lists section sizes as well as total size for the input object file(s). Here's the syntax for the command:

size [-A|-B|--format=compatibility]

[--help]

[-d|-o|-x|--radix=number]

[--common]

[-t|--totals]

[--target=bfdname] [-V|--version]

[objfile...]

And here's how the man page describes this utility:

The GNU size utility lists the section sizes---and the total size---for each of the object or

archive files objfile in its argument list. By default, one line of output is generated for each

object file or each module in an archive.

objfile... are the object files to be examined. If none are specified, the file "a.out" will be

used.

Following are some Q&A-styled examples that'll give you a better idea about how the size command works.

## Q1. How to use size command?

Basic usage of size is very simple. All you have to do is to pass the object/executable file name as input to the tool. Following is an example:

size apl

Following is the output the above command produced on our system:

The first three entries are for text, data, and bss sections, with their corresponding sizes. Then comes the total in decimal and hexadecimal formats. And finally, the last entry is for the filename.

## Q2. How to switch between different output formats?

The default output format, the man page for size says, is similar to the Berkeley's format. However, if you want, you can go for System V convention as well. For this, you'll have to use the **--format** option with *SysV* as value.

size apl --format=SysV

Here's the output in this case:

## Q3. How to switch between different size units?

By default, the size of sections is displayed in decimal. However, if you want, you can have this information on octal as well as hexadecimal. For this, use the** -o** and **-x** command line options.

Here's what the man page says about these options:

-d

-o

-x

--radix=number

Using one of these options, you can control whether the size of each section is given in decimal

(-d, or --radix=10); octal (-o, or --radix=8); or hexadecimal (-x, or --radix=16). In

--radix=number, only the three values (8, 10, 16) are supported. The total size is always given in

two radices; decimal and hexadecimal for -d or -x output, or octal and hexadecimal if you're using

-o.

## Q4. How to make size command show totals of all object files?

If you are using size to find out section sizes for multiple files in one go, then if you want, you can also have the tool provide totals of all column values. You can enable this feature using the **-t** command line option.

size -t [file1] [file2] ...

The following screenshot shows this command line option in action:

The last row in the output has been added by the **-t** command line option.

## Q5. How to make size print total size of common symbols in each file?

If you are running the size command with multiple input files, and want the command to display common symbols in each file, then you can do this with the **--common** command line option.

size --common [file1] [file2] ...

It's also worth mentioning that when using Berkeley format these are included in the bss size.

## Q6. What are the other available command line options?

Aside from the ones discussed until now, size also offers some generic command line options like **-v** (for version info) and **-h** (for summary of eligible arguments and options)

In addition, you can also make *size* read command-line options from a file. This you can do using the **@file** option. Following are some details related to this option:

The options read are inserted in place of the original @file option. If file does not exist, or

cannot be read, then the option will be treated literally, and not removed. Options in file are

separated by whitespace. A whitespace character may be included in an option by surrounding the

entire option in either single or double quotes. Any character (including a backslash) may be

included by prefixing the character to be included with a backslash. The file may itself contain

additional @file options; any such options will be processed recursively.

## Conclusion

One thing is clear, the size command isn't for everybody. It's aimed at only those who deal with the structure of object/executable files in Linux. So if you are among the target audience, practice the options we've discussed here, and you should be ready to use the tool on daily basis. For more information on size, head to its [man page](https://linux.die.net/man/1/size).