347

The ls -al command shows the following output;

-rwxrw-r--    10    root   root 2048    Jan 13 07:11 afile.exe

What are all the fields in the preceding display?

Mr. White
  • 3,657

4 Answers4

343

In the order of output;

-rwxrw-r--    1    root   root 2048    Jan 13 07:11 afile.exe
  • file permissions (-rwxrw-r--),
  • number of (hard) links (1),
  • owner name (root),
  • owner group (root),
  • file size in bytes (2048),
  • time of last modification (Jan 13 07:11), and
  • file/directory name (afile.exe)

File permissions is displayed as following;

  • first character is most often -, l or d. A d indicates a directory, a - represents a regular file, l is a symlink (or soft link) and other letters are used for other types of special files
  • three sets of characters, three times, indicating permissions for owner, group and other:
    • r = readable
    • w = writable
    • x = executable (for files) or accessible (for directories)
  • this may be followed by some other character of there are extended permissions, like e.g. Linux ACL that are marked with a +.

In your example -rwxrw-r--, this means the line displayed is:

  • a regular file (displayed as -)
  • readable, writable and executable by owner (rwx)
  • readable, writable, but not executable by group (rw-)
  • readable but not writable or executable by other (r--)

The number of hard links means the number of names the inode has, i.e. links created with ln without the -s option.

Flyk
  • 3,553
  • 20
    There are more than 2 types of file. - is not for non-directories, it's for regular files, there's more than r, w and x permissions. On most systems, the first field is also used to indicate the presence of extra attributes like ACLs, security attributes or other extended attributes. Also note that for symlinks, the target of the symlink is also displayed in the output of ls -l. – Stéphane Chazelas Nov 29 '13 at 21:06
  • 15
    The first character can have different values (eg. b, D, andp`). There's a full explanation on Wikipedia. – ashes999 Oct 14 '14 at 13:53
  • 23
    The numbers of fields is poorly explained. For files it means number of hard links. For directory: number of directories inside directory + this directory itself + 1. – yanpas Mar 19 '16 at 19:20
  • 1
    @ashes999, you could suggest an edit to the answer owner, to make a better answer. have you already done that? – Scantlight Apr 07 '16 at 21:01
  • 21
    What does number of links mean? Thanks. – tommy.carstensen Jan 06 '17 at 11:41
  • 2
    @tommy.carstensen see yanpas comment – akhan Apr 08 '17 at 05:57
  • @giladmayani the command echo "#\!/bin/sh\necho hello world" > afile.exe && chmod 764 afile.exe would create a perfectly valid executable with permissions in question, which you can execute (well, on GNU/Linux at least). – Hi-Angel Oct 13 '17 at 14:37
  • 3
    Why there is no details about these in 'man ls'? If I forgot these, and I am in a CLI system which do not has a GUI or a browser to access StackExchange, where i can get these informations? – Kyung Lee Nov 30 '17 at 01:35
  • 1
    Where is the documentation for this? – vaer-k Feb 02 '18 at 22:29
  • 8
    info ls has more info – ctrl-alt-delor Mar 05 '18 at 09:21
  • 2
    Additional note: the file size for a directory is only the size of the metadata of the directory, not the total size of the files under the directory. – wisbucky May 22 '18 at 21:18
  • Maybe this should be a separate question but how does one find the meaning of such an output without stackexchange? It seems the man page only says that -l gives you a long output format without actually sating what that means. – Kvothe Oct 12 '18 at 14:47
  • info ls has more info – Todd Walton Jan 03 '19 at 16:46
  • The ls coreutils manual (and I've written my own info files…) is BAD. Because this info is NOT IN THERE. – Jürgen A. Erhard Jun 04 '19 at 03:00
  • 2
    I am still not really understand the number of links even it already explain by @yanpas . Can I have example on it. What means if the number of links is 0, and 1. – Panadol Chong Sep 30 '19 at 08:22
  • @PanadolChong: It is really file system, operating system AND port specific. Most 'ls' source code simly lists what the system's stat() call gives, and labels it "number of links". On some older ports of 'ls' to Microsoft OS's it gave me some really odd results for some files. Generally, NTFS and EXT (unix and unix-like) keep a count of how many times the file is referenced somewhere else, so it only truly deletes the fle if the count reaches zero (no more references to the file). Directories are odd, because they (usually) contain two additional 'links': "." and "..". – C. M. Apr 15 '21 at 07:26
  • 1
    Oops, not enough coffee! Correction: Every directory will have ONE additional link for ".". If it has sub-directories, each sub-directory will increase this by +1 to account for the ".." entry. So if you have "/mydir/one" and "/mydir/two", the link count for "mydir" will be 4: 1 for itself, +1 for it's parent, +1 each for "one" and "two" (each having a ".."). – C. M. Apr 15 '21 at 07:36
131

The output of the "ls" command depends on the version of "ls", the options used, the platform used, etc. It appears from your example that you're using it from a typical un*x (such as Linux), and probably using a typical modern "ls" version. In which case:

-rwxrw-r--    10    root   root 2048    Jan 13 07:11 afile.exe
?UUUGGGOOOS   00  UUUUUU GGGGGG ####    MON DD XX:XX FILENAME
^ ^  ^  ^ ^    ^      ^      ^    ^      ^            ^- Filename.
| |  |  | |    |      |      |    |      \-------------- Time of last modification.
| |  |  | |    |      |      |    \--------------------- File Size OR for directory size of the metadata. (Size is *usually* in bytes on modern systems; See below.)
| |  |  | |    |      |      \-------------------------- 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        

I am not sure why your link count is so high for the example file you listed. Some platforms have an odd notion of what constitutes a "link". These usually include hard links and symbolic links, as well as directory entries (which is why directories often have high link counts – its parent has one link, the directory has a link to itself in the . entry, and each of its sub-directories has a link back via ..).

Some versions and/or command line flags will list the number of blocks used instead of the number of bytes; a filesystem with a block size of 1024 bytes will list all sizes up to 1024 bytes as "1", meaning 1 block is used, from 1025 to 2048 as "2", using 2 blocks, and so on. But listing block sizes by default (without explicitly using a command line option) is rare on most modern un*x machines.

The special/alternative access flag is usually a blank space, but on some platforms, it may be used to indicate there are special/alternative access modes (such as ACLs and security descriptors on WIN32, etc), and varies widely – consult your manual, man pages, info tool, or what-not.

The permissions (mode) flags (UUUGGGOOO) are three sets of three chars, where the first set is "User" (i.e., Owner), the second set is "Group" and the third set is "Others" (i.e., everyone else; anyone who is neither Owner nor Group). The three permissions flags in each set are typically r or - meaning the User/Group/Others can read the file (r) or not (-), followed by w or - indicating whether they can write to the file (you can have files which you can write to, but cannot read, as odd as that may sound!), and the third character is a 'catch-all' flag for other modes, typically something like x for execute (for directories, it means you can attempt to access the directory contents), or - for none. Sometimes you may encounter an s or S for setuid and/or setgid programs, or other less common characters; see your ls documentation for the mode characters it will show.

Finally, the very first character is the file type; typically one of: d for directory, l for a symbolic link (hard links show normally without a special character of their own), or - for a normal file. There are many other, but less commonly seen, file types for various filesystems. These first ten characters (file type and permissions) are discussed on Wikipedia. Again, your documentation will tell you exactly what kind of file types your command supports and displays.

BTW, if you cannot find a man/info page for ls itself (man ls/info ls), try looking in the "coreutils" package (info coreutils). Also note that among the more common platforms, Microsoft platforms tend not to translate very well to ls output, so you may see odd behavior, flags, or other unusual info in the output, depending on how your version of ls was compiled, what it was linked against, etc.

One more caveat: The file time stamp is usually the date/time the file was last modified, not the time the file was created. In fact, on a un*x-ish filesystem, there is no record of the file creation time; the ctime field does NOT mean "creation time" as it does on FAT/NTFS filesystems, but rather, it means the "inode [C]hange time" – the time the inode itself was last modified. The "mtime" (last [M]odified) and atime (last [A]ccesed/read) timestamps are the same on both systems – although the precision (FAT has a granularity of two seconds, for example) and time zone may vary.

C. M.
  • 1,884
  • 5
    Which system includes symbolic links in the link count? – celtschk Jul 06 '14 at 07:24
  • 1
    Sorry, I was not clear.. No system I know includes symbolic links in the link count.. I only meant in "what constitutes a 'link'". For example, in Windows', a "shell link" is considered a link, but most console commands will treat it as a normal file. This includes ls, which will not include it in the link count. To the best of my knowledge, unless the sources for 'ls' is otherwise modified, it only reports what the OS reports for the link count. – C. M. Jul 06 '14 at 14:30
  • 2
    Including sym-links and .lnk files would require a walk of the entire directory try. Just to list a file. It would be waaaaaaaaaaaaay slow. It only includes hard link. That is the number of directory entries that the file has. (it is used by the garbage collector. When the reference count gets to zero, then the file is deleted.) – ctrl-alt-delor Mar 05 '18 at 09:26
  • 1
    Normally, my understanding of operating systems (OS) and file systems (FS) would agree, but I've seen some oddities from time to time with certain OS's and FS's. Although, it's not always part of the OS/FS; Sometimes, the oddities are the result of trying to port a tool from one OS to another, and then trying to adjust it for the new platform--the differences in the CTIME field between EXT and FAT/NTFS, for example--and how NTFS implements different kinds of "links" (shell links, reparse points, etc), making it difficult to give an exact and uniform definition. – C. M. Mar 14 '18 at 23:18
  • 2
    Additional note: the file size for a directory is only the size of the metadata of the directory, not the total size of the files under the directory. – wisbucky May 22 '18 at 21:18
  • how the ascii table is made? – Wayou Mar 31 '19 at 08:33
  • @Wayou: Look up the formatting codes for the site (such as the "help" link I see right now editing this comment). You want to use some kind of code that uses a pre-formatted, monospaced font style, so browsers won't collapse multiple spaces/tabs in to single space. – C. M. May 19 '20 at 05:25
  • It's 2020 now, and I wanted to add a small update: Most of the tools I use and/or see now are updated, and better handle the file-system and operating system differences correctly; Particularly things like "link counts". You should still refer to the documentation of the specific tool you are using to better understand it's output. – C. M. May 19 '20 at 05:29
39

On GNU systems, it is described in ls info page in a very detailed way. All you had to do to find it: just open man ls and find in the end link to the full documentation: info coreutils 'ls invocation'.

Here is quote from it:

`-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.
rush
  • 27,403
3

The first column is the file mode, the next column is the numbers of link that the file has, the third and fourth are the name of the owner and the group which the file belongs to. Next column says the number of bytes of the file (some ls implementations have a -h option to see this information in a more user-friendly form). The last two columns indicate the timestamp and the name of the file. You'd read the man page for more info.