diff --git a/sources/tech/20210820 Check file status on Linux with the stat command.md b/sources/tech/20210820 Check file status on Linux with the stat command.md deleted file mode 100644 index 552bc004bc..0000000000 --- a/sources/tech/20210820 Check file status on Linux with the stat command.md +++ /dev/null @@ -1,119 +0,0 @@ -[#]: subject: "Check file status on Linux with the stat command" -[#]: via: "https://opensource.com/article/21/8/linux-stat-file-status" -[#]: author: "Seth Kenlon https://opensource.com/users/seth" -[#]: collector: "lujun9972" -[#]: translator: "New-World-2019" -[#]: reviewer: " " -[#]: publisher: " " -[#]: url: " " - -Check file status on Linux with the stat command -====== -All the information you need about any file or file system is just one -Linux command away. -![Hand putting a Linux file folder into a drawer][1] - -The `stat` command, included in the GNU `coreutils` package, provides a variety of metadata, including file size, inode location, access permissions and SELinux context, and creation and modification times, about files and filesystems. It's a convenient way to gather information that you usually need several different commands to acquire. - -### Installing stat on Linux - -On Linux, you probably already have the `stat` command installed because it's part of a core utility package that's generally bundled with Linux distributions by default. - -In the event that you don't have `stat` installed, you can install `coreutils` with your package manager. - -Alternately, you can [compile coreutils from source code][2]. - -### Getting the status of a file - -Running `stat` provides easy to read output about a specific file or directory. - - -``` -$ stat planets.xml -  File: planets.xml -  Size: 325      Blocks: 8     IO Block: 4096   regular file -Device: fd03h/64771d    Inode: 140217      Links: 1 -Access: (0664/-rw-rw-r--)  Uid: (1000/tux)   Gid: (100/users) -Context: unconfined_u:object_r:user_home_t:s0 -Access: 2021-08-17 18:26:57.281330711 +1200 -Modify: 2021-08-17 18:26:58.738332799 +1200 -Change: 2021-08-17 18:26:58.738332799 +1200 - Birth: 2021-08-17 18:26:57.281330711 +1200 -``` - -It may be easy to read, but it's still a lot of information. Here's what `stat` is covering: - - * **File**: the file name - * **Size**: the file size in bytes - * **Blocks**: the number of blocks on the hard drive reserved for this file - * **IO Block**: the size of a block of the filesystem - * **regular file**: the type of file (regular file, directory, filesystem) - * **Device**: the device where the file is located - * **Inode**: the inode number where the file is located - * **Links**: the number of links to the file - * **Access, UID, GID**: file permissions, user, and group owner - * **Context**: SELinux context - * **Access, Modify, Change, Birth**: the timestamp of when the file was accessed, modified, changed status, and created - - - -### Terse output - -For people who know the output well, or want to parse the output with other utilities like [awk][3], there's the `--terse` (`-t` for short) option, which formats the output without headings or line breaks. - - -``` -$ stat --terse planets.xml -planets.xml 325 8 81b4 100977 100 fd03 140217 1 0 0 1629181617 1629181618 1629181618 1629181617 4096 unconfined_u:object_r:user_home_t:s0 -``` - -### Choosing your own format - -You can define your own format for output using the `--printf` option and a syntax similar to [printf][4]. Each attribute reported by `stat` has a format sequence (`%C` for SELinux context, `%n` for file name, and so on), so you can choose what you want to see in a report. - - -``` -$ stat --printf="%n\n%C\n" planets.xml -planets.xml -unconfined_u:object_r:user_home_t:s0 -$ $ stat --printf="Name: %n\nModified: %y\n" planets.xml -Name: planets.xml -Modified: 2021-08-17 18:26:58.738332799 +1200 -``` - -Here are some common format sequences: - - * **%a** access rights - * **%F** file type - * **%n** file name - * **%U** user name - * **%u** user ID - * **%g** group ID - * **%w** time of birth - * **%y** modification time - - - -A full listing of format sequences is available in the `stat` man page and the `coreutils` info pages. - -### File information - -If you've ever tried to parse the output of `ls -l`, then you'll appreciate the flexibility of the `stat` command. You don't always need every bit of the default information that `stat` provides, but the command is invaluable when you do need some or all of it. Whether you read its output in its default format, or you create your own queries, the `stat` command gives you easy access to the data about your data. - --------------------------------------------------------------------------------- - -via: https://opensource.com/article/21/8/linux-stat-file-status - -作者:[Seth Kenlon][a] -选题:[lujun9972][b] -译者:[译者ID](https://github.com/译者ID) -校对:[校对者ID](https://github.com/校对者ID) - -本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出 - -[a]: https://opensource.com/users/seth -[b]: https://github.com/lujun9972 -[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/yearbook-haff-rx-linux-file-lead_0.png?itok=-i0NNfDC (Hand putting a Linux file folder into a drawer) -[2]: https://www.gnu.org/software/coreutils/ -[3]: https://opensource.com/article/20/9/awk-ebook -[4]: https://opensource.com/article/20/8/printf diff --git a/translated/tech/20210820 Check file status on Linux with the stat command.md b/translated/tech/20210820 Check file status on Linux with the stat command.md new file mode 100644 index 0000000000..c89b1c0883 --- /dev/null +++ b/translated/tech/20210820 Check file status on Linux with the stat command.md @@ -0,0 +1,118 @@ +[#]: subject: "Check file status on Linux with the stat command" +[#]: via: "https://opensource.com/article/21/8/linux-stat-file-status" +[#]: author: "Seth Kenlon https://opensource.com/users/seth" +[#]: collector: "lujun9972" +[#]: translator: "New-World-2019" +[#]: reviewer: " " +[#]: publisher: " " +[#]: url: " " + +在 Linux 上使用 stat 命令查看文件状态 +====== +只需要一个 Linux 命令,你就可以获取到任何文件或文件系统的所有信息。 +![Hand putting a Linux file folder into a drawer][1] + +命令 `stat` 被包含在 GNU `coreutils` 软件包里,它提供了关于文件和文件系统的各种元数据,包括文件大小、结点位置、访问权限和 SELinux 上下文以及创建和修改时间。通常情况下,你需要多个不同命令获取的信息,这一个命令就可以实现。 + +### 在 Linux 上安装 stat 命令 + +在 Linux 系统中,可能早已安装了 `state` 命令,因为它是核心功能软件包的一部分,默认情况下,通常包含在 Linux 发行版里。 + +如果系统中没有安装 `stat` 命令,你可以使用包管理器安装 `coreutils` 软件包。 + +另外,你可以 [ 使用源码编译 coreutils 包 ][2]。 + +### 获取文件状态 + +运行 `stat` 命令可以获取指定文件或目录易于理解的输出。 + + +``` +$ stat planets.xml + File: planets.xml + Size: 325 Blocks: 8 IO Block: 4096 regular file +Device: fd03h/64771d Inode: 140217 Links: 1 +Access: (0664/-rw-rw-r--) Uid: (1000/tux) Gid: (100/users) +Context: unconfined_u:object_r:user_home_t:s0 +Access: 2021-08-17 18:26:57.281330711 +1200 +Modify: 2021-08-17 18:26:58.738332799 +1200 +Change: 2021-08-17 18:26:58.738332799 +1200 + Birth: 2021-08-17 18:26:57.281330711 +1200 +``` + +输出的信息很容易理解,但是包含了很多的信息,这里是 `stat` 所包含的项: + + * **File**: 文件名 + * **Size**: 文件大小,以字节表示 + * **Blocks**: 在硬盘驱动器上为文件保留的数据块的数量 + * **IO Block**: 文件系统块大小 + * **regular file**: 文件类型(普通文件,目录,文件系统) + * **Device**: 文件所在的设备 + * **Inode**: 文件所在的 Inode 号 + * **Links**: 文件的链接数 + * **Access, UID, GID**: 文件权限,用户和组的所有者 + * **Context**: SELinux 上下文 + * **Access, Modify, Change, Birth**: 文件被访问、修改、更改状态以及创建时的时间戳 + + + +### 精简输出 + +对于非常了解输出或者想要使用其它工具(例如:[awk][3])解析输出的人,这里可以使用 `--terse`(短参数为 `-t`) 参数,实现没有标题或换行符的格式化输出。 + + +``` +$ stat --terse planets.xml +planets.xml 325 8 81b4 100977 100 fd03 140217 1 0 0 1629181617 1629181618 1629181618 1629181617 4096 unconfined_u:object_r:user_home_t:s0 +``` + +### 选择自己的格式 + +你可以使用 `--printf` 参数以及与 [printf][4] 类似的语法定义自己的输出格式。`stat` 的每一个属性都有一个格式序列(`%C` 表示 SELinux 上下文,`%n` 表示文件名等等),所以,你可以选择你想要的输出格式。 + + +``` +$ stat --printf="%n\n%C\n" planets.xml +planets.xml +unconfined_u:object_r:user_home_t:s0 +$ $ stat --printf="Name: %n\nModified: %y\n" planets.xml +Name: planets.xml +Modified: 2021-08-17 18:26:58.738332799 +1200 +``` + +下面是一些常见的格式序列: + + * **%a** 访问权限 + * **%F** 文件类型 + * **%n** 文件名 + * **%U** 用户名 + * **%u** 用户 ID + * **%g** 组 ID + * **%w** 创建时间 + * **%y** 修改时间 + + + +在 `stat` 手册和 `coreutils` 信息页中都有完整的格式化序列列表。 + +### 文件信息 + +如果你曾经尝试解析过 `ls -l` 的输出,那么,你会很喜欢 `stat` 命令的灵活性。你并不是每次都需要 `stat` 提供的所有信息,但是,当你需要其中一些或全部的时候它是非常有用的。不管你是读取默认输出,还是你自己创建的查询输出,`stat` 命令都可以让你访问想要的数据。 + +-------------------------------------------------------------------------------- + +via: https://opensource.com/article/21/8/linux-stat-file-status + +作者:[Seth Kenlon][a] +选题:[lujun9972][b] +译者:[New-World-2019](https://github.com/New-World-2019) +校对:[校对者ID](https://github.com/校对者ID) + +本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出 + +[a]: https://opensource.com/users/seth +[b]: https://github.com/lujun9972 +[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/yearbook-haff-rx-linux-file-lead_0.png?itok=-i0NNfDC (Hand putting a Linux file folder into a drawer) +[2]: https://www.gnu.org/software/coreutils/ +[3]: https://opensource.com/article/20/9/awk-ebook +[4]: https://opensource.com/article/20/8/printf \ No newline at end of file