ls -al 输出中的字段是什么意思?

Mr.*_*ite 296 ls

ls -al命令显示以下输出;

-rwxrw-r--    10    root   root 2048    Jan 13 07:11 afile.exe
Run Code Online (Sandbox Code Playgroud)

前面显示的所有字段是什么?

小智 301

按输出顺序;

-rwxrw-r--    1    root   root 2048    Jan 13 07:11 afile.exe
Run Code Online (Sandbox Code Playgroud)
  • 文件权限 ( -rwxrw-r--),
  • (硬)链接数 ( 1),
  • 所有者姓名 ( root),
  • 所有者组 ( root),
  • 以字节为单位的文件大小 ( 2048),
  • 上次修改时间 ( Jan 13 07:11),以及
  • 文件/目录名称 ( afile.exe)

文件权限显示如下;

  • 第一个字符通常是-,ld。Ad表示目录,a-表示普通文件,l是符号链接(或软链接),其他字母用于其他类型的特殊文件
  • 三组字符,三遍,分别表示owner、group等权限:
    • r = 可读
    • w = 可写
    • x = 可执行(对于文件)或可访问(对于目录)
  • 这后面可能跟有一些其他字符,有扩展权限,例如标有+.

在您的示例中-rwxrw-r--,这意味着显示的行是:

  • 常规文件(显示为-
  • 所有者可读、可写和可执行 ( rwx)
  • 可读、可写,但不能被组 ( rw-) 执行
  • 可读但不可写或可执行的其他 ( r--)

硬链接数是指名称的数量的一个inode,即与创建的链接ln 没有-s选项。

  • **字段数** 解释不善。对于文件,它意味着硬链接的数量。对于目录:目录内的目录数 + 此目录本身 + 1。 (21认同)
  • “链接数”是什么意思?谢谢。 (19认同)
  • 有超过 2 种类型的文件。`-` 不是用于 _non-directories_,而是用于 _regular files_,还有不止是 `r`、`w` 和 `x` 权限。在大多数系统上,第一个字段还用于指示额外属性的存在,如 ACL、安全属性或其他扩展属性。另请注意,对于符号链接,符号链接的目标也显示在 `ls -l` 的输出中。 (16认同)
  • 第一个字符可以有不同的值(例如`b`、D` 和`p`)。[维基百科](http://en.wikipedia.org/wiki/Unix_file_types) 有完整的解释。 (14认同)
  • `info ls` 有更多信息 (6认同)
  • 为什么“man ls”中没有关于这些的详细信息?如果我忘记了这些,并且我在没有 GUI 或浏览器来访问 StackExchange 的 CLI 系统中,我可以从哪里获得这些信息? (3认同)
  • @tommy.carstensen 见 yanpas 评论 (2认同)
  • 附加说明:目录的文件大小只是该目录的元数据的大小,而不是该目录下文件的总大小。 (2认同)
  • 即使@yanpas 已经解释过,我仍然不太了解“链接数”。我可以举个例子吗?如果链接数为 0 和 1,这意味着什么。 (2认同)

C. *_* M. 119

“ls”命令的输出取决于“ls”的版本、使用的选项、使用的平台等。从您的示例中可以看出,您正在使用典型的 un*x(例如 Linux),并且可能使用典型的现代“ls”版本。在这种情况下:

-rwxrw-r--    10    root   root 2048    Jan 13 07:11 afile.exe
?UUUGGGOOOS   00  UUUUUU GGGGGG ####    ^-- date stamp and file name are obvious ;-)
^ ^  ^  ^ ^    ^      ^      ^    ^
| |  |  | |    |      |      |    \--- File Size
| |  |  | |    |      |      \-------- Group Name (for example, Users, Administrators, etc)
| |  |  | |    |      \--------------- Owner Acct
| |  |  | |    \---------------------- Link count (what constitutes a "link" here varies)
| |  |  | \--------------------------- Alternative Access (blank means none defined, anything else varies)
| \--\--\----------------------------- Read, Write and Special access modes for [U]ser, [G]roup, and [O]thers (everyone else)
\------------------------------------- File type flag
Run Code Online (Sandbox Code Playgroud)

我不确定为什么您列出的示例文件的链接数如此之多。一些平台对什么是“链接”有一个奇怪的概念。这些通常包括硬链接和符号链接,以及目录条目(这就是目录通常具有高链接数的原因——它的父目录有一个链接,目录在.条目中有一个到它自己的链接,它的每个子目录都有通过..)返回的链接。

某些版本和/或命令行标志会列出使用的块数而不是字节数;块大小为 1024 字节的文件系统会将最大为 1024 字节的所有大小列为“1”,表示使用 1 个块,从 1025 到 2048 为“2”,使用 2 个块,依此类推。但是默认情况下列出块大小(没有明确使用命令行选项)在大多数现代 un*x 机器上很少见。

特殊/替代访问标志通常是一个空格,但在某些平台上,它可能用于表示存在特殊/替代访问模式(例如 WIN32 上的 ACL 和安全描述符等),并且差异很大 – 请参阅您的手册、手册页、信息工具等。

权限(模式)标志(UUUGGGOOO)是三组三个字符,其中第一组是“用户”(即所有者),第二组是“组”,第三组是“其他人”(即其他所有人) ; 既不是所有者也不是组的任何人)。每组中的三个权限标志通常是r-表示用户/组/其他人可以读取文件 ( r) 或不可以 ( -),后跟w-指示他们是否可以写入文件(您可以拥有可以写入的文件,但无法读取,听起来很奇怪!),并且第三个字符是其他模式的“全能”标志,通常类似于x执行(对于目录,这意味着您可以尝试访问目录内容),或者-没有。sSsetuid 和/或 setgid 程序,或其他不太常见的字符;有关它将显示的模式字符,请参阅您的“ls”文档。

最后,第一个字符是文件类型;通常是以下之一:d用于目录、l用于符号链接(硬链接通常显示时没有自己的特殊字符)或-用于普通文件。对于各种文件系统,还有许多其他但不太常见的文件类型。前十个字符(文件类型和权限)在 Wikipedia 上进行了讨论。同样,您的文档将准确地告诉您您的命令支持和显示的文件类型。

顺便说一句,如果您找不到“ls”本身的手册/信息页面(“man ls”/“info ls”),请尝试查看“coreutils”包(“info coreutils”)。另请注意,在更常见的平台中,Microsoft 平台往往无法很好地转换为“ls”输出,因此您可能会在输出中看到奇怪的行为、标志或其他异常信息,具体取决于您的“ls”版本编译,它与什么相关联,等等。

还有一个警告:文件时间戳通常是文件上次修改的日期/时间,而不是文件的创建时间。实际上,在 un*x-ish 文件系统上,没有文件创建时间的记录;ctime 字段并不像在 FAT/NTFS 文件系统上那样表示“创建时间”,而是表示“inode [C]hange 时间”——inode 本身上次修改的时间。"mtime" (last [M]odified) 和 atime (last [A]ccessed/read) 时间戳在两个系统上是相同的——尽管精度(例如 FAT 的粒度为两秒)和时区可能会有所不同.

  • 哪个系统在链接计数中包含符号链接? (4认同)
  • 包括符号链接和 .lnk 文件需要遍历整个目录。只是为了列出一个文件。这将是waaaaaaaaaaaay慢。它只包括硬链接。那是文件具有的目录条目数。(它被垃圾收集器使用。当引用计数变为零时,文件被删除。) (2认同)
  • 附加说明:目录的文件大小只是该目录的元数据的大小,而不是该目录下文件的总大小。 (2认同)

rus*_*ush 34

在 GNU 系统上,它在ls信息页面中以非常详细的方式进行了描述。找到它所需要做的一切:只需打开man ls并在最后找到完整文档的链接:info coreutils 'ls invocation'.

这是它的引述:

`-l'
`--format=long'
`--format=verbose'
     In addition to the name of each file, print the file type, file
     mode bits, number of hard links, owner name, group name, size, and
     timestamp (*note Formatting file timestamps::), normally the
     modification time.  Print question marks for information that
     cannot be determined.

     Normally the size is printed as a byte count without punctuation,
     but this can be overridden (*note Block size::).  For example, `-h'
     prints an abbreviated, human-readable count, and
     `--block-size="'1"' prints a byte count with the thousands
     separator of the current locale.

     For each directory that is listed, preface the files with a line
     `total BLOCKS', where BLOCKS is the total disk allocation for all
     files in that directory.  The block size currently defaults to 1024
     bytes, but this can be overridden (*note Block size::).  The
     BLOCKS computed counts each hard link separately; this is arguably
     a deficiency.

     The file type is one of the following characters:

    `-'
          regular file

    `b'
          block special file

    `c'
          character special file

    `C'
          high performance ("contiguous data") file

    `d'
          directory

    `D'
          door (Solaris 2.5 and up)

    `l'
          symbolic link

    `M'
          off-line ("migrated") file (Cray DMF)

    `n'
          network special file (HP-UX)

    `p'
          FIFO (named pipe)

    `P'
          port (Solaris 10 and up)

    `s'
          socket

    `?'
          some other file type

     The file mode bits listed are similar to symbolic mode
     specifications (*note Symbolic Modes::).  But `ls' combines
     multiple bits into the third character of each set of permissions
     as follows:

    `s'
          If the set-user-ID or set-group-ID bit and the corresponding
          executable bit are both set.

    `S'
          If the set-user-ID or set-group-ID bit is set but the
          corresponding executable bit is not set.

    `t'
          If the restricted deletion flag or sticky bit, and the
          other-executable bit, are both set.  The restricted deletion
          flag is another name for the sticky bit.  *Note Mode
          Structure::.

    `T'
          If the restricted deletion flag or sticky bit is set but the
          other-executable bit is not set.

    `x'
          If the executable bit is set and none of the above apply.

    `-'
          Otherwise.

     Following the file mode bits is a single character that specifies
     whether an alternate access method such as an access control list
     applies to the file.  When the character following the file mode
     bits is a space, there is no alternate access method.  When it is
     a printing character, then there is such a method.

     GNU `ls' uses a `.' character to indicate a file with an SELinux
     security context, but no other alternate access method.

     A file with any other combination of alternate access methods is
     marked with a `+' character.
Run Code Online (Sandbox Code Playgroud)