linux stat 命令_如何在Linux上使用stat命令
linux stat 命令
The Linux stat
command shows you much more detail than ls
does. Take a peek behind the curtain with this informative and configurable utility. We’ll show you how to use it.
Linux stat
命令向您显示的详细信息比ls
更多。 使用此信息丰富且可配置的实用工具,可以窥视幕后。 我们将向您展示如何使用它。
stat将您带入幕后 (stat Takes You Behind the Scenes)
The ls
command is great at what it does—and it does a lot—but with Linux, it seems that there’s always a way to go deeper and see what lies beneath the surface. And often, it isn’t just a case of lifting the edge of the carpet. You can rip up the floorboards and then dig a hole. You can peel Linux like an onion.
ls
命令擅长于完成它的工作,并且可以完成很多工作,但是对于Linux来说,似乎总有一种方法可以使您更深入地了解底层内容。 通常,这不只是举起地毯边缘的情况。 您可以撕开地板,然后挖一个洞。 您可以像剥洋葱一样剥开Linux。
ls
will show you a good deal of information about a file, such as which permissions are set on it, and how big it is, and whether it is a file or a symbolic link. To display this information ls
reads it from a file system structure called an inode.
ls
将为您显示有关文件的大量信息,例如,在文件上设置了哪些权限, 文件的大小以及它是文件还是符号链接 。 为了显示此信息, ls
从称为inode的文件系统结构中读取了该信息。
Every file and directory has an inode. The inode holds metadata about the file, such as which filesystem blocks it occupies, and the date stamps associated with the file. The inode is like a library card for the file. But ls
will only show you some of the information. To see everything, we need to use the stat
command.
每个文件和目录都有一个索引节点。 索引节点保存有关文件的元数据 ,例如它占用哪个文件系统,以及与该文件关联的日期戳。 索引节点就像文件的存储卡。 但是ls
仅会向您显示一些信息。 要查看所有内容,我们需要使用stat
命令。
Like ls
, the stat
command has a lot of options. This makes it a great candidate for the use of aliases. Once you have discovered a particular set of options that make stat
give you the output that you want, wrap it in an alias or shell function. This makes it much more convenient to use, and you don’t have to remember an arcane set of command-line options.
像ls
一样, stat
命令具有很多选项。 这使其成为使用别名的理想选择。 一旦发现使stat
提供给您所需输出的特定选项集,请将其包装在别名或shell函数中 。 这使它使用起来更加方便,并且您不必记住一组神秘的命令行选项。
快速比较 (A Quick Comparison)
Let’s use ls
to give us a long listing ( -l
option) with human-readable file sizes ( -h
option):
让我们使用ls
为我们提供一个长长的清单( -l
选项)以及易于理解的文件大小( -h
选项):
ls -lh ana.h
From left to right, the information that ls provides is:
从左到右,ls提供的信息是:
- The very first character is a hyphen “-” and this tells us the file is a regular file and not a socket, symlink, or another type of object. 第一个字符是连字符“-”,这告诉我们文件是常规文件,而不是套接字,符号链接或其他类型的对象。
-
The owner, group, and other permissions are listed in octal format.
所有者,组和其他权限以八进制格式列出 。
- The number of hard links pointing to this file. In this case, and in most cases, it will be one. 指向此文件的硬链接数。 在这种情况下,并且在大多数情况下,它将是其中之一。
- The file owner is dave. 文件所有者是dave。
- The group owner is dave. 群组所有者是dave。
- The file size is 802 bytes. 文件大小为802字节。
- The file was last modified on Friday, 13th December 2015. 该文件的最后修改时间为2015年12月13日,星期五。
-
The file name is
ana.c
.文件名是
ana.c
Let’s take a look with stat
:
让我们看一下stat
:
stat ana.h
The information we get from stat
is:
我们从stat
信息中获得的信息是:
-
File: The name of the file. Usually, it is the same as the name we passed to
stat
on the command line, but It can be different if we’re looking at a symbolic link.File :文件名。 通常,它与我们在命令行
stat
的名称相同,但是如果我们查看符号链接,则可以不同。 -
Size: The size of the file in bytes.
大小 :文件大小,以字节为单位。
-
Blocks: The number of filesystem blocks the file requires, in order to be stored on the hard drive.
块数:文件所需要的文件系统块数,以便存储在硬盘驱动器上。
-
IO Block: The size of a filesystem block.
IO块 :文件系统块的大小。
-
File type: The type of object the metadata describes. The most common types are files and directories, but they can also be links, sockets, or named pipes.
文件类型 :元数据描述的对象的类型。 最常见的类型是文件和目录,但它们也可以是链接,套接字或命名管道。
-
Device: The device number in hexadecimal and decimal. This is the ID of the hard drive the file is stored on.
设备 :设备编号( 十六进制和十进制)。 这是存储文件的硬盘驱动器的ID。
-
Inode: The inode number. That is, the ID number of this inode. Together, the inode number and the device number uniquely identify a file.
索引节点:索引节点号。 也就是说,此索引节点的ID号。 索引节点号和设备号一起唯一地标识一个文件。
-
Links: This number indicates how many hard links point to this file. Each hard link has its own inode. So another way to think about this figure is how many inodes point to this one file. Each time a hard link is created or deleted, this number will be adjusted up or down. When it reaches zero, the file itself has been deleted, and the inode is removed. If you use
stat
on a directory, this number represents the number of files in the directory, including the “.” entry for the current directory and the “..” entry for the parent directory.链接 :此数字表示指向该文件的硬链接数。 每个硬链接都有其自己的索引节点。 因此,考虑该图的另一种方法是有多少个inode指向该文件。 每次创建或删除硬链接时,此数字都会向上或向下调整。 当它达到零时,文件本身已被删除,并且索引节点也被删除。 如果在目录上使用
stat
,则此数字表示目录中的文件数,包括“。” 当前目录的条目,父目录的“ ..”条目。 -
Access: The file permissions are shown in their octal and traditional
rwx
(read, write, execute formats).访问权限 :文件权限以八进制和传统的
rwx
(读,写,执行格式)显示。 -
Uid: User ID and account name of the owner.
Uid :所有者的用户ID和帐户名。
-
Gid: Group ID and account name of the owner.
Gid :所有者的组ID和帐户名。
-
Access: The access timestamp. Not as straightforward as it might seem. Modern Linux distributions use a scheme called
relatime
, which tries to optimize the hard drive writes required to update the access time. Simply put, the access time is updated if it is older than the modified time.访问 :访问时间戳。 不像看起来那样简单。 现代Linux发行版使用一种称为
relatime
的方案,该方案试图优化更新访问时间所需的硬盘驱动器写入 。 简而言之,如果访问时间早于修改时间,则更新时间。 -
Modify: The modification timestamp. This is the time when file’s contents were last modified. (As luck would have it, the contents of this file were last changed four years ago to the day.)
修改 :修改时间戳。 这是上次修改文件内容的时间。 (很幸运,此文件的内容最后一次更改是在四年前的今天。)
-
Change: The change timestamp. This is the time the file’s attributes or contents were last changed. If you modify a file by setting new file permissions, the change timestamp will be updated (because the file attributes have changed), but the modified timestamp will not be updated (because the file contents were not changed).
更改 :更改时间戳。 这是文件属性或内容的最后更改时间。 如果通过设置新文件权限来修改文件,则更改时间戳将被更新(因为文件属性已更改),但是修改后的时间戳将不会被更新(因为文件内容未更改)。
-
Birth: Reserved to show the original creation date of the file, but this is not implemented in Linux.
出生日期:保留该日期以显示文件的原始创建日期,但Linux中未实现。
了解时间戳 (Understanding the Timestamps)
The timestamps are timezone sensitive. The -0500
at the end of each line shows that this file was created on a computer in a Coordinated Universal Time (UTC) timezone that is five hours ahead of the timezone of the current computer. So this computer is five hours behind the computer that created this file. In fact, the file was created on a UK timezone computer, and we’re looking at it here on a computer in the US Eastern Standard time zone.
时间戳是时区敏感的。 每行末尾的-0500
表示此文件是在协调世界时 (UTC)时区的计算机上创建的,该时区比当前计算机的时区早五小时。 因此,此计算机比创建此文件的计算机晚五个小时。 实际上,该文件是在英国时区计算机上创建的,我们正在美国东部标准时区的计算机上查看该文件。
The modify and change timestamps can cause confusion because, to the uninitiated, their names sound as if they mean the same thing.
修改和更改时间戳可能会引起混淆,因为对于未启动的时间戳,它们的名称听起来好像它们含义相同。
Let’s use chmod
to modify the file permissions on a file called ana.c
. We’re going to make it writeable by everyone. This won’t affect the contents of the file, but it will affect the attributes of the file.
让我们使用chmod
修改名为ana.c
的文件的文件权限。 我们将使每个人都可以编写它。 这不会影响文件的内容,但是会影响文件的属性。
chmod +w ana.c
And then we’ll use stat
to look at the timestamps:
然后,我们将使用stat
查看时间戳:
stat ana.c
The change timestamp has been updated, but the modified one has not.
更改时间戳已更新,但修改后的时间戳尚未更新。
The modified timestamp will only be updated if the contents of the file are changed. The change timestamp is updated for both content changes and attribute changes.
仅当文件内容更改时, 修改后的时间戳才会更新。 更改时间戳针对内容更改和属性更改进行了更新。
将Stat与多个文件一起使用 (Using Stat With Multiple Files)
To have stat report on several files at once, pass the filenames to stat
on the command line:
要一次报告多个文件的stat
,请在命令行中将文件名传递给stat
:
stat ana.h ana.o
To use stat
on a set of files, use pattern matching. The question mark “?” represents any single character, and the asterisk “*” represents any string of characters. We can tell stat
to report on any file called “ana” with a single letter extension, with this command:
要对一组文件使用stat
,请使用模式匹配。 问号“?” 代表任何单个字符,星号“ *”代表任何字符串。 我们可以使用以下命令告诉stat
报告带有单个字母扩展名的名为“ ana”的任何文件:
stat ana.?
使用stat来报告文件系统 (Using stat to Report on Filesystems)
stat
can report on the status of filesystems, as well as the status of files. The -f
(filesystem) option tells stat
to report on the filesystem that the file resides on. Note we can also pass a directory such as “/” to stat
instead of a filename.
stat
可以报告文件系统的状态以及文件的状态。 -f
(文件系统)选项告诉stat
报告文件所在的文件系统。 请注意,我们还可以将目录(例如“ /”)传递给stat
而不是文件名。
stat -f ana.c
The information stat
gives us is:
stat
信息提供给我们的是:
-
File: The name of the file.
File :文件名。
-
ID: The filesystem ID in hexadecimal notation.
ID :以十六进制表示的文件系统ID。
-
Namelen: The maximum permissible length for file names.
Namelen :文件名的最大允许长度。
-
Type: The type of filesystem.
Type :文件系统的类型。
-
Block size: The amount of data to request read requests for optimum data transfer rates.
块大小 :请求读取请求以获取最佳数据传输速率的数据量。
-
Fundamental block size: The size of each filesystem block.
基本块大小 :每个文件系统块的大小。
Blocks:
方块:
-
Total: The total count of all blocks n the filesystem.
总数 :文件系统中所有块的总数。
-
Free: The number of free blocks in the filesystem.
Free :文件系统中的空闲块数。
-
Available: The number of free blocks available to regular (non-root) users.
可用 :普通(非root)用户可用的可用块数。
Inodes:
索引节点:
-
Total: The total count of inodes in the filesystem.
Total :文件系统中inode的总数。
-
Free: The number of free inodes in the filesystem.
Free :文件系统中空闲inode的数量。
取消引用符号链接 (Dereferencing Symbolic Links)
If you use stat
on a file that is actually a symbolic link, it will report on the link. If you wanted stat
to report on the file that the link points to, use the -L
(dereference) option. The file code.c
is a symbolic link to ana.c
. Let’s look at it without the -L
option:
如果在实际上是符号链接的文件上使用stat
,它将在该链接上报告。 如果希望stat
报告链接指向的文件,请使用-L
(取消引用)选项。 该文件code.c
是一个符号链接ana.c
。 让我们看看不带-L
选项的情况:
stat code.c
The filename shows code.c
pointing to ( ->
) ana.c
. The file size is only 11 bytes. There are zero blocks devoted to storing this link. The file type is listed as a symbolic link.
文件名显示code.c
指向( ->
) ana.c
。 文件大小只有11个字节。 有零个块专门用于存储此链接。 文件类型被列为符号链接。
Clearly, we’re not looking at the actual file here. Let’s do that again and add the -L
option:
显然,我们不在这里查看实际文件。 让我们再做一次并添加-L
选项:
stat -L code.c
This is now showing the file details for the file pointed to by the symbolic link. But note that the filename is still given as code.c
. This is the name of the link, not the target file. This happens because this is the name we passed to stat
on the command line.
现在,这将显示符号链接所指向的文件的文件详细信息。 但是请注意,文件名仍然以code.c
给出。 这是链接的名称,而不是目标文件。 发生这种情况是因为这是我们在命令行stat
的名称。
简要报告 (The Terse Report)
The -t
(terse) option causes stat
to provide a condensed summary:
-t
(简短)选项使stat
提供简明摘要:
stat -t ana.c
There are no clues given. To make sense of it—until you’ve memorized the field sequence—you need to cross-reference this output to a full stat
output.
没有任何线索。 要理解它(直到您记忆了字段序列),您需要将此输出交叉引用为完整的stat
输出。
自定义输出格式 (Custom Output Formats)
A better way to obtain a different set of data from stat
is to use a custom format. There is a long list of tokens called format sequences. Each of these represents a data element. Select the ones you want to have included in the output and create a format string. When we call stat
and pass the format string to it, the output will only include the data elements we requested.
从stat
获取一组不同的数据的更好方法是使用自定义格式。 有很长的令牌列表,称为格式序列。 这些每个都代表一个数据元素。 选择要包含在输出中的内容,然后创建格式字符串。 当我们调用stat
并将格式字符串传递给它时,输出将仅包含我们请求的数据元素。
There are different sets of format sequences for files and filesystems. The list for files is:
文件和文件系统有不同的格式序列集。 文件列表为:
-
%a: The access rights in octal.
%a :八进制访问权限。
-
%A: The access rights in human-readable form (
rwx
).%A :可读格式(
rwx
)的访问权限。 -
%b: The number of blocks allocated.
%b :分配的块数。
-
%B: The size in bytes of each block.
%B :每个块的大小(以字节为单位)。
-
%d: The device number in decimal.
%d :设备编号(十进制)。
-
%D: The device number in hex.
%D :设备编号(十六进制)。
-
%f: The raw mode in hex.
%f :十六进制的原始模式。
-
%F The file type.
%F文件类型。
-
%g: The group ID of the owner.
%g :所有者的组标识。
-
%G: The group name of the owner.
%G :所有者的组名。
-
%h: The number of hard links.
%h :硬链接数。
-
%i: The inode number.
%i :索引节点号。
-
%m: The mount point.
%m :安装点。
-
%n: The file name.
%n :文件名。
-
%N: The quoted file name, with dereferenced filename if it is a symbolic link.
%N :带引号的文件名,如果是符号链接,则带有解引用的文件名。
-
%o: The optimal I/O transfer size hint.
%o :最佳I / O传输大小提示。
-
%s: The total size, in bytes.
%s :总大小,以字节为单位。
-
%t: The major device type in hex, for character/block device special files.
%t :十六进制的主要设备类型,用于字符/块设备专用文件。
-
%T: The minor device type in hex, for character/block device special files.
%T :十六进制的次要设备类型,用于字符/块设备特殊文件。
-
%u: The user ID of the owner.
%u :所有者的用户标识。
-
%U: The user name of the owner.
%U :所有者的用户名。
-
%w: The time of file birth, human-readable, or a hyphen “-” if unknown.
%w :文件的诞生时间,可读性或连字符“-”(如果未知)。
-
%W: The time of file birth, seconds since the Epoch; 0 if unknown.
%W :文件诞生的时间,自纪元以来的秒数; 如果未知,则为0。
-
%x: The time of last access, human-readable.
%x :上次访问的时间,人类可以理解。
-
%X: The time of last access, seconds since the Epoch.
%X :最后一次访问的时间,距离纪元以来的秒数。
-
%y: The time of last data modification, human-readable.
%y :最后一次数据修改的时间,人类可以理解。
-
%Y: The time of last data modification, seconds since the Epoch.
%Y :上次修改数据的时间,自纪元以来的秒数。
-
%z: The time of last status change, human-readable.
%z :上次状态更改的时间,人类可以理解。
-
%Z: The time of last status change, seconds since the Epoch.
%Z :最后一个状态更改的时间,距离纪元以来的秒数。
The “epoch” is the Unix Epoch, which took place on 1970-01-01 00:00:00 +0000 (UTC).
“ epoch”是Unix Epoch ,它发生在1970-01-01 00:00:00 +0000(UTC)上。
For filesystems the format sequences are:
对于文件系统,格式序列为:
-
%a: The number of free blocks available to regular (non-root) users.
%a :普通(非root)用户可用的可用块数。
-
%b: The total data blocks in the filesystem.
%b :文件系统中的总数据块。
-
%c: The total inodes in the filesystem.
%c :文件系统中的总索引节点。
-
%d: The number of free inodes in the filesystem.
%d :文件系统中空闲索引节点的数量。
-
%f: The number of free blocks in the filesystem.
%f :文件系统中的空闲块数。
-
%i: The file system ID in hexadecimal.
%i :文件系统ID(十六进制)。
-
%l: The maximum length of filenames.
%l :文件名的最大长度。
-
%n: The filename.
%n :文件名。
-
%s: The block size (the optimum writing size).
%s :块大小(最佳写入大小)。
-
%S: The size of filesystem blocks (for block counts).
%S :文件系统块的大小(用于块计数)。
-
%t: The file system type in hexadecimal.
%t :文件系统类型,十六进制。
-
%T: file system type in human-readable form.
%T :易于理解的文件系统类型。
There are two options that accept strings of format sequences. These are --format
and --printf
. The difference between them is --printf
interprets C-style escape sequences such as newline \n
and tab \t
, and it does not automatically add a newline character to its output.
有两个选项可以接受格式序列的字符串。 它们是--format
和--printf
。 它们之间的区别是--printf
解释C样式的转义序列,例如换行\n
和制表符\t
,并且不会自动在其输出中添加换行符。
Let’s create a format string and pass it to stat
. The format sequences were going to use are %n
for filename, %s
for the size of the file and %F
for the file type. We’re going to add the \n
escape sequence to the end fo the string to make sure each file is handled on a new line. Our format string looks like this:
让我们创建一个格式字符串,并将其传递给stat
。 要使用的格式序列为%n
表示文件名, %s
表示文件大小, %F
表示文件类型。 我们将在字符串的末尾添加\n
转义序列,以确保每个文件都在新行中处理。 我们的格式字符串如下所示:
"File %n is %s bytes, and is a %F\n"
We’re going to pass this to stat
using the --printf
option. We’re going to ask stat
to report on a file called code.c
and a set of files that match ana.?
. This is the full command. Note the equals sign “=
” between --printf
and the format string:
我们将使用--printf
选项将其传递给stat
。 我们将要求stat
报告一个名为code.c
的文件以及一组与ana.?
匹配的文件ana.?
。 这是完整的命令。 注意--printf
和格式字符串之间的等号“ =
”:
stat --printf="File %n is %s bytes, and is a %F\n" code.c ana/ana.?
The report for each file is listed on a new line, which is what we requested. The filename, file size, and file type are provided for us.
每个文件的报告都在新行中列出,这是我们要求的。 为我们提供了文件名,文件大小和文件类型。
Custom formats give you access to even more data elements than are included in the standard stat
output.
自定义格式使您可以访问比标准stat
输出中包含的数据元素更多的数据元素。
细粒控制 (Fine Grain Control)
As you can see, there is tremendous scope to extract the particular data elements that are of interest to you. You can probably also see why we recommended using aliases for the longer and more complex incantations.
如您所见,提取您感兴趣的特定数据元素的范围很大。 您可能还可以看到为什么我们建议对更长更复杂的咒语使用别名。
翻译自: https://www.howtogeek.com/451022/how-to-use-the-stat-command-on-linux/
linux stat 命令
上一篇: 获取文件属性 stat() fstat() lstat()
下一篇: 一致性哈希的分析与实现