Gatlin.io Icon
Image of Austin Gatlin's face
February 28, 2022 (last updated March 17, 2022)

ls

ls is short for list and it lists directory content.

Theoretical points

A file system is not concerned with the actual data contained within the files. It is only concerned with metadata about those files. Unix-style file systems work by storing data in memory (aka, [data blocks][1]) and then referencing the location of that memory via an inode. That inode is then linked to, and that link is given a name, which becomes the filename. The ls utility is predominately focused on the filenames, and the inodes they reference.

[1][https://en.wikipedia.org/wiki/block_(data_storage)]

Common aliases

Some ls flags are so useful that they show up in common aliases.

  • -l prints the "long format" output, which we will discuss further later.
  • -a includes entries that begin with ., aka, hidden files.
  • -G colorizes the output, for example, it will color subdirectories differntly from regular files.

These flags combine to form a good working base for the ls utility. As such, the following are common aliases.

alias l=`ls -G`
alias la=`ls -aG`
alias ll=`ls -alG`

Long format

The long format includes, from right to left, the file name, modified date, file size, group name, owner name, number of links, and file modes.

Owner, Group, and Permissions

Groups are collections of users. Groups exist for dynamic permission allocations by having users belong to multiple groups. While a user can belong to multiple groups, a file only belongs to one group and one user, who is the owner of the file. The group and owner can be changed with chgrp and chown, respectively.

File permissions appear as -rwxrwxrwx at the beginning of the long format. They represent the Read, Write, and eXecutable status of a file. A - means a file does not have the associated permission. These permissions can be modified via chown and chgrp.

This is the number of hard links to file, which always includes itself, and so is at least 1. Directories will have more. Soft links, aka symlinks, will not impact this number. The inode be linked to by the filename can be seen via ls -i, which shows the inode number, or file serial number, in the far left.

File type

The character at the start of the permissions list represents the file type.

  • - means the entry is a regular file.
  • d means the entry is a directory.
  • l means the entry is a symbolic link
  • s means the entry is a socket file
    • Sockets behave like network sockets, but exist as files in Linux to facilitate local client-server communication between processes.
  • c and b are character and block special files.
    • Special files are essentially the hardware device communication medium. The two types of special files correspond to the type of communication that occurs. Character special files transfer data in byte-sized amounts: monitors, terminals, printers, etc. Block special files transfer data in larger block amounts: hard-drives, etc.
  • p means the entry is a pipe file
    • Pipe files are named, persistent versions of the unix pipe, |. I've never used one but in principle you can use it for cross-process piping, among other things. See man mkfifo and the wiki on named pipes for more.

Extended attributes

Sometimes you will see an @ at the end of the permissions list. It means the entry has extended attributes which can be listed via the ls -@ flag. Learn more with man xattr. In brief, xattrs are metadata attributes not contained in the inode data, and so need to live elsewhere in memory while still being associated with the file.