Skip to content
Eduardo Lira

LINUX

ls Command in Linux - Listing Directory Contents

Learn how to use the "ls" command in Linux to list directory contents, including options like -l, -a, -i, and more. Explore file permissions and concepts like setuid, setgid, and sticky bit.

Lists directory contents.

Syntax:

ls

Options:

-l : long mode. Lists details about objects, such as owner, group, permissions, last modification date, and size (default unit is byte).

-a (or —all) : lists all objects contained in the directory, including hidden ones.

-i (or inode -> index nodes) : lists the node number of all objects (index that the file system uses to identify the file or directory).

-c : lists in alphabetical order if used with -l, but can also be used with -lt to sort by last modification. If used with neither, it will display the most recent ones first.

-h (or —human-readable): lists content in “more human” mode. The size unit of each object is as understandable as possible.

-C : lists content in columns.

-t : sorts content by last modification date (mtime).

-S : sorts files by size.

-R : lists content recursively, subdirectories and files.

-r : sorts content in reverse alphabetical order.

Examples:

Listing all content of the /root/LPI directory, including hidden files, in long and “more human” mode:

ls -lha /root/LPI

Listing in reverse alphabetical order and recursively, all content of /etc:

ls -lrR /etc

File details:

When listing a specific directory with ls -l, some interesting items were verified. Remembering that in this example we are dealing with the lowercase l.

Our output represents the file type and the permissions applied to it. From this first column, let’s highlight for now the type (first field).

Files are divided into a few types:

- : is a regular file.

d : is a directory.

l : is a symbolic link.

b: is a block file (a disk or a partition).

c: is a character file (a terminal, for example).

p: is a pipe or also known as FIFO (First In First Out)

n: network file

s: is a socket.

The other main columns are:

r - Permission for the user to read the file.

w - Permission for the user to write to the file.

x - Permission for the user to execute the file.

➜  ~ ls -lS /etc/hosts
-rw-r--r-- 1 root root 160 Sep  4 18:32 /etc/hosts

In an example, -rw-r--r-- means that the user can read and write the file, and the group and others can only read the file. The number 1 after the permission characters is the number of hard links to this file.

The next two fields root root are showing the file owner and the group, followed by the file size (160), shown in bytes. Use the -h option if you want to print sizes in a readable format.

Sep 4 18:32 is the date and time of the last file modification. At the end, we have the file name.

Before proceeding, let’s get an idea of what setuid, setgid, and sticky are

setuid and setgid bit

(abbreviations for “set user ID upon execution” and “set group ID upon execution”) are Unix access right flags that allow users to run an executable with the permissions of the executable’s owner or group, respectively, and change behavior in directories.

They are often used to allow users on a computer system to run programs with temporarily elevated privileges in order to perform a specific task. While the assumed user ID or group ID privileges provided are not always elevated, at a minimum they are specific.

They are necessary for tasks that require higher privileges than those of common users, such as changing their login password. Some of the tasks that require elevated privileges may not be immediately obvious, however - such as the ping command, which has to send and listen for control packets on a network interface.

sticky bit

This permission is usually applied to directories. When this occurs, files created inside can be renamed or deleted only by the file owner, the directory owner, or the superuser.

➜  ~ ls -ld /tmp
drwxrwxrwt 43 root root 1000 Oct 16 00:02 /tmp

source: https://en.wikipedia.org/wiki/Setuid

Ok. It was just a small introduction to setuid, setgid, and sticky. Soon we will see more details and utility of these permissions. Better stop here about ls. Unfortunately, a command with several possibilities like this won’t fit in a single post. …But calm down, there’s more coming soon.

See you next time!

Revisited in November 2026

Rereading this 2017 post, I corrected a few things directly in the text and record them here:

  • In the examples, the hyphen of the options had turned into a dash (–lha). Anyone who copied and pasted got an error, because ls looked for a file named –lha. It now has the right hyphen.
  • -t sorts by the content modification date, which is mtime, not ctime as I had written.
  • ctime is the date of the file’s last status change: permissions, owner, link count or content. That’s what -c uses. With -lt, -c sorts by and shows that date; with -l alone, it shows the date and sorts by name; on its own, it sorts by it, newest first.
  • The long forms of the options take two hyphens: --all, --human-readable, --inode.
  • The n type, “network file”, doesn’t show up in Linux’s ls. It comes from other Unix-like systems, such as HP-UX.
  • ping was the classic example of a setuid program. On most current distributions it no longer needs it: it uses a specific capability (cap_net_raw) or the unprivileged ICMP sockets the kernel provides. passwd, which changes the password, is still a good example of setuid.

The post promised more details about ls and these permissions “soon”. That didn’t come at the time; the commands guide only came back in 2024, in part 1 and part 2.

Who writes this

Eduardo Lira

Software developer in Rio de Janeiro. I started this blog in 2012, in technical support at a hosting company, writing down the fixes for the problems that came up every day. Today I work at Sofis Tecnologia, on critical healthcare systems and SQL Server database migrations.

Found a mistake in this post?

A command that no longer works, a broken link, wrong information: tell me what you saw. The message goes only to the author and is not shown on the site.