Read in 5 minutes

last updated 

How to List Files in Linux using the ls Command

The ls command is one of the basic commands that any Linux user should know. It is used to list information about files and directories within the file system. The ls utility is a part of the GNU core utilities which are installed on all Linux distributions.

In this tutorial, we will show you how to use the ls command through practical examples and detailed explanations of the most common ls options.

How to Use the ls Command

The syntax for the ls command is as follows:

ls [OPTIONS] [FILES]

When used with no arguments, the ls command will list the names of all files in the current working directory:

ls

The files are listed in alphabetical order:

cache  db  empty  games  lib  local  lock  log  mail  opt  run  spool  tmp

To list files in a specific directory, pass the path to the directory to the ls command. For example, to list the contents of the /etc directory, type:

ls /etc

You can also pass multiple directories and files to the ls command separated by space:

ls /etc /var /etc/passwd

If the user you are logged in doesn’t have read permissions to the directory you will get a message saying that the ls command can’t open the directory:

ls /root
ls: cannot open directory '/root': Permission denied

The ls command has a number of options. In the sections below, we will explore the most commonly used options.

Long Listing Format

The default output of the ls command shows only the names of the files, which is not very informative. When the long listing format is used the ls command will display the following file information:

  • The file type
  • The file permissions
  • Number of hard links to the file
  • File owner
  • File group
  • File size
  • Date and Time
  • File name

The -l ( lowercase L) option causes ls to print files in long listing format.

ls -l /etc/hosts
-rw-r--r-- 1 root root 337 Oct  4 11:31 /etc/hosts

Let’s explain the most important columns of the output.

The first character shows the file type. In our example, the first character is - which means regular file. Values for other file types are as follows:

  • - - Regular file
  • b - Block special file
  • c - Character special file
  • d - Directory
  • l - Symbolic link
  • n - Network file
  • p - FIFO
  • s - Socket

Next nine characters are showing the file permissions. The first three characters are for the user, the next three are for the group, and the last three are for others. You can change the file permissions with the chmod command. The permission character can take the following value:

  • r - Permission to read file
  • w - Permission to write to file
  • x - Permission to execute file
  • s - setgid bit
  • t - sticky bit

In our 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 size of the file (337), shown in bytes. Use the -h option if you want to print sizes in a human-readable format. You can change the file owner with the chmod command.

Oct 4 11:31 is the last file modification date and time. The last column is the name of the file.

Show Hidden Files

By default, the ls command will not show hidden files. In Linux, a hidden file is any file that begins with a dot (.).

To display all files including the hidden files use the -a option:

ls -la ~/
drwxr-x--- 10 linuxize  linuxize  4096 Feb 12 16:28 .
drwxr-xr-x 18 linuxize  linuxize  4096 Dec 26 09:21 ..
-rw-------  1 linuxize  linuxize  1630 Nov 18  2017 .bash_history
drwxr-xr-x  2 linuxize  linuxize  4096 Jul 20  2018  bin
drwxr-xr-x  2 linuxize  linuxize  4096 Jul 20  2018  Desktop
drwxr-xr-x  4 linuxize  linuxize  4096 Dec 12  2017 .npm
drwx------  2 linuxize  linuxize  4096 Mar  4  2018 .ssh

We are also using the -l option to show the details of all of the files.

How to Sort the ls Output

As we already mentioned, by default the ls command is listing the files in alphabetical order.

  • --sort=extension (or -X ) - sort alphabetically by entry extension
  • --sort=size (or -S) - sort by file size
  • --sort=time ( or -t) - sort by modification time
  • --sort=version (or -v) - natural sort of (version) numbers within text

If you want to get the results in the reverse sort order use the -r option.

For example, to sort the files in the /var directory by modification time in the reverse sort order you would use:

ls -ltr /var

It’s worth mentioning that the ls command is not showing the total space occupied by the contents of the directory.

List Subdirectories Recursively

Use the -R argument to tell the ls command to display the contents of the subdirectories:

ls -R

Conclusion

By now you should have a good understanding of how to use the Linux ls command. For more information about the other ls options visit the GNU Coreutils page

If you have any question or feedback, feel free to leave a comment.