How to List Files in Linux using the Ls Command

Ls is one of the most basic and useful commands for Unix-like operating systems. Many people who are aware of how to use ls to list information about files and directories on their system will be aware of its basic functionality, but not some of the extra useful features it packs-in.

 

Today we will be having a look at how to use the ls command in Linux to list files and directories. We will also go through some of the available option flags to customise the output of ls.

 

Ls Syntax

ls [-options] [file ...]

 

As you can see the syntax for ls is very basic. There are a set of options and a file or directory path from which to list information.

 

Basic Usage

The most basic way to use use the ls command is to type in the command on its own and nothing else.

 

ls

 

This will alphabetically list the names of directories and files from the current working directory.

 

Archive.zip app composer.json gulpfile.js package.json routes webpack.mix.js

 

 

List Files From a Directory

To list files and directories from a specific directory you can pass the path to the directory as the second argument in ls. This can be a relative or absolute path.

 

ls /var/www/skillsugar.com/app

 

List Files From Multiple Directories

You can pass as many directory paths as you wish into one ls command and their contents will be returned.

 

ls app config
app/:
Blog.php Item.php Page.php

config/:
app.php breadcrumbs.php cache.php

 

 

Long Listing Format

A useful feature of ls is deployed by implementing the -l flag. It will display a list of information about the files including their read-write-execute permissions, owner, size and last modification date.

 

ls -l

 

Pro tip - pass in the -h flag and the size of the files will be presented in a human-readable format.

 

ls -lh
-rw-r--r--   1 johnhoward staff  15M 17 Jun 02:13 Archive.zip
drwxr-xr-x  40 johnhoward staff  1.3K 17 Jun 20:35 app
-rwxr-xr-x   1 johnhoward staff  1.6K 14 Aug 2017 artisan
drwxr-xr-x   5 johnhoward staff  160B 21 Nov 2017 bootstrap
-rwxr-xr-x   1 johnhoward staff  1.7K 10 Jun 16:46 composer.json
-rwxr-xr-x   1 johnhoward staff  161K 10 Jun 16:46 composer.lock

 

let's breakdown what the individual elements of this output mean from left to right.

 

The very first element indicates the type of file, which in most cases will be a - meaning a normal file or d meaning a directory. Here is a full list of file abbreviations and what type of file they indicate:

 

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

 

Immediately after are the file permissions, followed by the owner and group, the size, date last modified and finally the name of the file.

 

Show Hidden Files

Files that begin with a . (dot) are hidden and are not shown by ls in its default behaviour. To show hidden files pass the -a flag into the ls command.

 

ls -a /var/www/skillsugar.com

 

List Files Recursively

To list files in a directory recursively use the -R option. This will output a list of all files in the given directory along with which folder they are located in:

 

ls -R resources/views

 

Sorting Files

ls provides functionality for sorting the output by size, version, date and extension. To do this add one of the following flags:

 

  • -S - order by size
  • -v - order by version number
  • -d - order by date last modified
  • -X - order by extension name

 

Conclusion

You now know how to use the ls command and some of its most useful options for customising the output and combing different directories to retrieve the contents of.

terminal command line unix file ubuntu