Skip to main content

Getting started with ls

Need some help understanding your file listings on the command line? Read on.

With the help of the ls command, users and superusers list information about files and directories. This article helps the administrator and user identify file permissions, which is useful for access and basic troubleshooting.

Using ls

At its most basic, you can use the ls command to see what files are in a directory: Note: The listing may or may not be color-coded.

[root@centos7 ~]# ls /etc
abrt          firewalld     makedumpfile.conf.sample  rdma
adjtime       flatpak       man_db.conf               redhat-release
aliases       fonts         maven                     request-key.conf
aliases.db    fprintd.conf  mime.types                request-key.d
alsa          fstab         mke2fs.conf               resolv.conf
alternatives  fuse.conf     modprobe.d                rhsm
anacrontab    gconf         modules-load.d            rpc

There are many flags available to give you more information with your file listing. The most common one to use is -a (or --all), since it shows you all of the files in the directory, including the ones that start with a dot (e.g., .bash_logout).

Another common flag is -l, which gives you more information about the files in a directory, including the file’s permissions, ownership, and size. If you want to be able to tell more easily what units a file size is in (e.g., 1K, 234M, 2G), you can add the -h (short for --human-readable) flag.

Let’s walk through an example. A simple file listing in the root user’s directory might be this:

[root@name ~]# ls /root                      

If you want the long format version of this list (we will cover the additional information shown here in the next section):

[root@name ~]# ls -l /root
total 4
-rw-------. 1 root root 896 Feb 22 01:40 anaconda-ks.cfg

However, if you add the -a flag, the ls command returns a short-form list including the hidden items:

[root@name ~]# ls -a /root
. anaconda-ks.cfg  .bash_logout .bashrc  .cshrc  .tcshrc
.. .bash_history  .bash_profile .cache  .local                                                    

Consider the single period and double period in both directory lists. The single dot (.) refers to the directory itself. This dot is convenient if you want to run a command and reference your current directory (for example, when you want to copy a file there).

The double dots (..) refer to the parent directory. If you type cd .. you move up one step in the filesystem hierarchy. For example, if your current directory is /root, typing cd .. would take you to /, the very top of the hierarchy.

If you combine the -a option with the -l option into -la, you get more details about the hidden and not hidden files:

[root@name ~]# ls -la /root
total 44
dr-xr-x---.  4 root root 4096 Feb 11 05:47 .
dr-xr-xr-x. 19 root root 4096 Jul 11 13:15 ..
-rw-------.  1 root root  896 Feb 22 01:40 anaconda-ks.cfg
-rw-------.  1 root root  163 Jul 11 01:18 .bash_history
-rw-r--r--.  1 root root   18 Feb  2 10:37 .bash_logout
-rw-r--r--.  1 root root  176 Feb  2 10:37 .bash_profile
-rw-r--r--.  1 root root  176 Feb  2 10:37 .bashrc
drwx------.  3 root root 4096 Jul 11 01:37 .cache
-rw-r--r--.  1 root root  100 Feb  2 10:37 .cshrc
drwxr-xr-x.  3 root root 4096 Feb 22 01:57 .local
-rw-r--r--.  1 root root  129 Feb  2 10:37 .tcshrc

Decoding the long listing format

When you start using the -l flag, you will notice a lot more information. Here is an example line of output:

drwxrwxr-x 2 root test 4096 Dec  3  2009 test

The first character to the left tells you what you’re looking at. If it’s a d, as you see here, that tells that this file is a directory, which is basically a special kind of file (and it will be referred to as a file throughout). However, if it was a dash (-), that would indicate that the file is a regular file.

If the first character was a lowercase l, then this item would be a special file type called a symlink, or soft link, which is a pointer (shortcut) to another location in the filesystem. Such a listing might look like this:

lrwxrwxrwx 1 root root      4 Jun 30 03:29 sh -> bash

After the first character are the permission abbreviations, which are grouped in threes. Permissions for files are represented by the following letters:

  • r for the read permission
  • w for the write permission
  • x for the execute permission

Back to this example:

drwxrwxr-x 2 root test 4096 Dec  3  2009 test
  • The first triplet (rwx) shows the permissions for the user or file owner.
  • The next triplet (rwx) shows the permissions for the group category.
  • The last triplet (r-x) shows the permissions for the final category, other.

In this example, users who are neither the file owner nor in the group have read and execute permissions but not write, as indicated by the dash (-) in the middle position.

The number listed after the permissions indicates either the link count (for a file) or the number of contained directory entries (for a directory). This number is not relevant for permissions.

After this number, the first name indicates the file’s owner. The user permissions (first triplet) apply to this owner, so in this example, the user root has read, write, and execute permissions for this directory.

The second name is the file’s group. The group permissions apply to any user in the same group, so in this case, anyone in the mail group has read, write, and execute permissions.

Wrapping up

Now you should have a much better feeling for getting information about your files from the command line. The ls command has many more options. Type man ls to learn more, and figure out your favorite flag combinations for getting things done. You might even want to make some aliases.

Topics:   Linux  
Author’s photo

Suraj Patil

Suraj has a passion to learn and explore open source technology and is enthusiastic to contribute to open source. He is also part of Fedora Magazine and Fedora kernel testing as well a member of the Chef community. More about me

On Demand: Red Hat Summit 2021 Virtual Experience

Relive our April event with demos, keynotes, and technical sessions from
experts, and sign up to attend breakout sessions June 15–16.

Related Content