linux获取文件大小_如何在Linux中获取文件或目录的大小

linux获取文件大小

linux获取文件大小

A Linux terminal window on an Ubuntu-style laptop.
Fatmawati Achmad Zaenuri/Shutterstock Fatmawati Achmad Zaenuri / Shutterstock

When you use the Linux du command, you obtain both the actual disk usage and the true size of a file or directory. We’ll explain why these values aren’t the same.

当您使用Linux du命令时,您将获得实际的磁盘使用情况以及文件或目录的真实大小。 我们将解释为什么这些值不相同。

实际磁盘使用量和实际大小 (Actual Disk Usage and True Size)

The size of a file and the space it occupies on your hard drive are rarely the same. Disk space is allocated in blocks. If a file is smaller than a block, an entire block is still allocated to it because the file system doesn’t have a smaller unit of real estate to use.

文件的大小及其在硬盘驱动器上占用的空间很少相同。 磁盘空间按块分配。 如果文件小于一个块,则仍将为整个块分配文件,因为文件系统没有较小的可用空间单位。

Unless a file’s size is an exact multiple of blocks, the space it uses on the hard drive must always be rounded up to the next whole block. For example, if a file is larger than two blocks but smaller than three, it still takes three blocks of space to store it.

除非文件大小恰好是块的整数倍,否则文件在硬盘驱动器上使用的空间必须始终四舍五入到下一个整个块。 例如,如果一个文件大于两个块但小于三个,则它仍需要三个空间来存储它。

Two measurements are used in relation to file size. The first is the actual size of the file, which is the number of bytes of content that make up the file. The second is the effective size of the file on the hard disk. This is the number of file system blocks necessary to store that file.

关于文件大小,使用了两个度量。 第一个是文件的实际大小,它是组成文件的内容的字节数。 第二个是硬盘上文件的有效大小。 这是存储该文件所需的文件系统块数。

一个例子 (An Example)

Let’s look at a simple example. We’ll redirect a single character into a file to create a small file:

让我们看一个简单的例子。 我们将单个字符重定向到文件中以创建一个小文件:

echo "1" > geek.txt
The "echo "1" > geek.txt" command in a terminal window.

Now, we’ll use the long format listing, ls, to look at the file length:

现在,我们将使用长格式清单ls来查看文件长度:

ls -l geek.txt
The "ls -l geek.txt" command in a terminal window.

The length is the numeric value that follows the dave dave  entries, which is two bytes. Why is it two bytes when we only sent one character to the file? Let’s take a look at what’s happening inside the file.

长度是dave dave条目后面的数字值,为两个字节。 当我们仅向文件发送一个字符时,为什么要两个字节? 让我们看看文件内部发生了什么。

We’ll use the hexdump command, which will give us an exact byte count and allow us to “see” non-printing characters as hexadecimal values. We’ll also use the -C (canonical) option to force the output to show hexadecimal values in the body of the output, as well as their alphanumeric character equivalents:

我们将使用hexdump命令,该命令将为我们提供确切的字节数,并允许我们“看到” 非打印字符作为十六进制值 。 我们还将使用-C (规范)选项来强制输出在输出的主体中显示十六进制值及其等效的字母数字字符:

hexdump -C geek.txt
The "hexdump -C geek.txt" command in a terminal window.

The output shows us that, beginning at offset 00000000 in the file, there’s a byte that contains a hexadecimal value of 31, and a one that contains a hexadecimal value of 0A. The right-hand portion of the output depicts these values as alphanumeric characters, wherever possible.

输出显示,从文件的偏移量00000000开始,有一个字节包含一个十六进制值31,一个字节包含一个十六进制值0A。 输出的右侧部分尽可能将这些值描述为字母数字字符。

The hexadecimal value of 31 is used to represent the digit one. The hexadecimal value of 0A is used to represent the Line Feed character, which cannot be shown as an alphanumeric character, so it’s shown as a period (.) instead. The Line Feed character is added by echo . By default, echostarts a new line after it displays the text it needs to write to the terminal window.

十六进制值31用于表示数字1。 十六进制值0A用于表示换行字符,该字符不能显示为字母数字字符,因此将其显示为句点(。)。 换行符由echo添加。 默认情况下, echo在显示需要写入终端窗口的文本后会开始换行。

That tallies with the output from ls and agrees with the file length of two bytes.

这与ls的输出相符,并且与两个字节的文件长度一致。

Now, we’ll use the du command to look at the file size:

现在,我们将使用du命令查看文件大小:

du geek.txt
The "du geek.txt" command in a terminal window.

It says the size is four, but four of what?

它说大小是四个,但是四个呢?

有块,然后有块 (There Are Blocks, and Then There Are Blocks)

When du reports file sizes in blocks, the size it uses depends on several factors. You can specify which block size it should use on the command line. If you don’t force du to use a particular block size, it follows a set of rules to decide which one to use.

du以块为单位报告文件大小时,它使用的大小取决于几个因素。 您可以在命令行上指定应使用的块大小。 如果您不强迫du使用特定的块大小,则它将遵循一组规则来决定使用哪个块大小。

First, it checks the following environment variables:

首先,它检查以下环境变量:

  • DU_BLOCK_SIZE

    DU_BLOCK_SIZE
  • BLOCK_SIZE

    BLOCK_SIZE
  • BLOCKSIZE

    区块大小

If any of these exist, the block size is set, and du stops checking. If none are set, du defaults to a block size of 1,024 bytes. Unless, that is, an environment variable called POSIXLY_CORRECT is set. If that’s the case, du defaults to a block size of 512 bytes.

如果存在任何这些,则设置块大小,并且du停止检查。 如果未设置,则du缺省为1,024字节的块大小。 除非是,否则将设置一个名为POSIXLY_CORRECT的环境变量。 在这种情况下, du默认块大小为512字节。

So, how do we find out which one is in use? You can check each environment variable to work it out, but there’s a quicker way. Let’s compare the results to the block size the file system uses instead.

那么,我们如何找出正在使用的是哪一个呢? 您可以检查每个环境变量来解决它,但是有一种更快的方法。 让我们将结果与文件系统使用的块大小进行比较。

To discover the block size the file system uses, we’ll use the tune2fs program. We’ll then use the -l (list superblock) option, pipe the output through grep, and then print lines that contain the word “Block.”

为了发现文件系统使用的块大小,我们将使用tune2fs程序。 然后,我们将使用-l ( list superblock )选项,通过grep输出输出,然后打印包含单词“ Block”的

In this example, we’ll look at the file system on the first partition of the first hard drive, sda1, and we’ll need to use sudo:

在此示例中,我们将查看第一个硬盘驱动器sda1的第一个分区上的文件系统,并且需要使用sudo

sudo tune2fs -l /dev/sda1 | grep Block
The "sudo tune2fs -l /dev/sda1 | grep Block" command in a terminal window.

The file system block size is 4,096 bytes. If we divide that by the result we got from du (four), it shows the du default block size is 1,024 bytes. We now know several important things.

文件系统块大小为4,096字节。 如果将其除以du (四个)得到的结果,则表明du默认块大小为1,024字节。 我们现在知道一些重要的事情。

First, we know the smallest amount of file system real estate that can be devoted to storing a file is 4,096 bytes. This means even our tiny, two-byte file is taking up 4 KB of hard drive space.

首先,我们知道可用于存储文件的最小文件系统空间为4,096字节。 这意味着即使我们很小的两字节文件也要占用4 KB的硬盘空间。

The second thing to keep in mind is applications dedicated to reporting on hard drive and file system statistics, such as du, ls, and tune2fs, can have different notions of what “block” means. The tune2fs application reports true file system block sizes, while ls and du can be configured or forced to use other block sizes. Those block sizes are not intended to relate to the file system block size; they’re just “chunks” those commands use in their output.

要记住的第二件事是专用于报告硬盘驱动器和文件系统统计信息的应用程序,例如dulstune2fs ,对于“块”的含义可能有不同的概念。 tune2fs应用程序报告真实的文件系统块大小,而lsdu可以配置或强制使用其他块大小。 这些块大小不旨在与文件系统块大小相关; 它们只是这些命令在其输出中使用的“块”。

Finally, other than using different block sizes, the answers from du and tune2fs convey the same meaning. The tune2fs result was one block of 4,096 bytes, and the du result was four blocks of 1,024 bytes.

最后,除了使用不同的块大小外, dutune2fs的答案传达的含义相同。 tune2fs结果是一个4,096字节的块, du结果是1,024字节的四个块。

使用du (Using du)

With no command line parameters or options, du lists the total disk space the current directory and all subdirectories are using.

在没有命令行参数或选项的情况下, du列出了当前目录和所有子目录正在使用的总磁盘空间。

Let’s take a look at an example:

让我们看一个例子:

du
The "du" command in a terminal window.

The size is reported in the default block size of 1,024 bytes per block. The entire subdirectory tree is traversed.

该大小以默认的块大小(每个块1,024字节)报告。 遍历整个子目录树。

在其他目录上使用du (Using du on a Different Directory)

If you want du to report on a different directory than the current one, you can pass the path to the directory on the command line:

如果要让du报告与当前目录不同的目录,可以在命令行中将路径传递到该目录:

du ~/.cach/evolution/
The "du ~/.cach/evolution/" command in a terminal window.

在特定文件上使用du (Using du on a Specific File)

If you want du to report on a specific file, pass the path to that file on the command line. You can also pass a shell pattern to a select a group of files, such as *.txt:

如果要让du报告特定文件,请在命令行中将路径传递到该文件。 您还可以将外壳模式传递给一组文件,例如*.txt

du ~/.bash_aliases
The "du ~/.bash_aliases" command in a terminal window.

报告目录中的文件 (Reporting on Files in Directories)

To have du report on the files in the current directory and subdirectories, use the -a (all files) option:

要使du报告当前目录和子目录中的文件,请使用-a (所有文件)选项:

du -a
The "du -a" command in a terminal window.

For each directory, the size of each file is reported, as well as a total for each directory.

对于每个目录,将报告每个文件的大小以及每个目录的总计。

Output from a "du -a" command in a terminal window.

限制目录树深度 (Limiting Directory Tree Depth)

You can tell du to list the directory tree to a certain depth. To do so, use the -d (max depth) option and provide a depth value as a parameter. Note that all subdirectories are scanned and used to calculate the reported totals, but they’re not all listed. To set a maximum directory depth of one level, use this command:

您可以告诉du将目录树列出到一定深度。 为此,请使用-d (最大深度)选项并提供深度值作为参数。 请注意,所有子目录均已扫描并用于计算报告的总数,但并未全部列出。 要将最大目录深度设置为一个级别,请使用以下命令:

du -d 1
The "du -d 1" command in a terminal window.

The output lists the total size of that subdirectory in the current directory and also provides a total for each one.

输出列出了当前目录中该子目录的总大小,并且还提供了每个子目录的总大小。

To list directories one level deeper, use this command:

要列出更深一层的目录,请使用以下命令:

du -d 2
The "du -d 2" command in a terminal window.

设置块大小 (Setting the Block Size)

You can use the block option to set a block size for du for the current operation. To use a block size of one byte, use the following command to get the exact sizes of the directories and files:

您可以使用block选项为当前操作的du设置块大小。 要使用一个字节的块大小,请使用以下命令来获取目录和文件的确切大小:

du --block=1
The "du --block=1" command in a terminal window.

If you want to use a block size of one megabyte, you can use the -m (megabyte) option, which is the same as --block=1M:

如果要使用1兆字节的块大小,则可以使用-m (兆字节)选项,该选项与--block=1M相同:

du -m
The "du -m" command in a terminal window.

If you want the sizes reported in the most appropriate block size according to the disk space used by the directories and files, use the -h (human-readable) option:

如果要根据目录和文件使用的磁盘空间以最合适的块大小报告大小,请使用-h (人类可读)选项:

du -h
The "du -h" command in a terminal window.

To see the apparent size of the file rather than the amount of hard drive space used to store the file, use the --apparent-size option:

要查看文件的外观大小,而不是用于存储文件的硬盘空间,请使用--apparent-size选项:

du --apparent-size
The "du --apparent-size" command in a terminal window.

You can combine this with the -a (all) option to see the apparent size of each file:

您可以将其与-a (所有)选项结合使用以查看每个文件的表观大小:

du --apparent-size -a
The "du --apparent-size -a" command in a terminal window.

Each file is listed, along with its apparent size.

列出每个文件及其外观大小。

Output from the "du --apparent-size -a" command in a terminal window.

仅显示总计 (Displaying Only Totals)

If you want du to report only the total for the directory, use the -s (summarize) option. You can also combine this with other options, such as the -h (human-readable) option:

如果要让du仅报告该目录的总数,请使用-s (摘要)选项。 您还可以将其与其他选项结合使用,例如-h (人类可读)选项:

du -h -s
The "du -h -s" command in a terminal window.

Here, we’ll use it with the --apparent-size option:

在这里,我们将它与--apparent-size选项一起使用:

du --apparent-size -s
The "du --apparent-size -s" command in a terminal window.

显示修改时间 (Displaying Modification Times)

To see the creation or last modification time and date, use the --time option:

要查看创建或上次修改的时间和日期,请使用--time选项:

du --time -d 2
The "du --time -d 2" command in a terminal window.

结果奇怪吗? (Strange Results?)

If you see strange results from du , especially when you cross-reference sizes to the output from other commands, it’s usually due to the different block sizes to which different commands can be set or those to which they default. It could also be due to the differences between real file sizes and the disk space required to store them.

如果您从du看到奇怪的结果,尤其是当您将大小与其他命令的输出进行交叉引用时,通常是由于可以设置不同命令的块大小或默认大小所致。 也可能是由于实际文件大小和存储它们所需的磁盘空间之间的差异。

If you need to match the output of other commands, experiment with the --block option in du.

如果需要匹配其他命令的输出,请尝试使用du--block选项。

翻译自: https://www.howtogeek.com/450366/how-to-get-the-size-of-a-file-or-directory-in-linux/

linux获取文件大小

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值