11 Examples of ls Command to List Files in Linux

Normally, the work of system administrators consists of an almost permanent use of the terminal; this is because servers with GNU/LINUX-based systems, for security reasons, do not have a graphical desktop environment. And this means that we have to use the terminal for basically everything from the simplest or most complex tasks. Today we will learn the ls command utility that allows us to list files and folders from the terminal. Here we will also describe different combinations and options that can be used with ls command to get different output.

LIST FILES IN LINUX: “ls” COMMAND 

As it was said, the command ls allows you to list files and folders of a specific path, is easy to use and is very useful and used in the daily work. The command has several options to improve the listings. In a terminal, we can invoke the help of the command ls in this way.

ls --help 

Above command will display all options available for ls command to list the files and folders. In this way we get the syntax of usage and options as output per terminal.

List all ls command options

As all options will not cover in one single screenshot so i have scrolled the above screen and taken another screenshots to cover all options in screenshots that has been displayed below.

Display all ls command options

Display all ls command options

As we can see the documentation is extensive, but don’t worry we will examine the most useful and used options. The basic syntax of the command is:

ls [options] [file|dir]

Although it is possible to use ls from the most basic form only by typing:

#List files from your given directory.
ls

With this we only list the files that are in the directory where we are located in the terminal. For example, if we wanted to list everything in the root directory of our GNU/LINUX distribution it would be with the command:

#List everything in the root directory
ls /

Run ls / to display all directories

It was easy, wasn’t it?

Most Important Options of the Command “ls”

ls as well as all GNU/LINUX commands have options that modify the basic command execution result. Here is a list of the most outstanding ones for this command.

OptionsDescription
-a Do not ignore entries starting with .
-A Do not list implied . And ..
-dList directories themselves, not their contents
-iprint the index number of each file
-lUse a long listing format.  Displays information such as permissions and owner of the folder or file.
-rreverse order while sorting
-RList subdirectories recursively.  Displays the contents of the directory subfolders.
-sprint the allocated size of each file, in blocks
-Ssort by file size, largest first
-tsort by modification time, newest first
-Udo not sort; list entries in directory order
-Xsort alphabetically by entry extension

 Examples of Command “ls”

Here i have given different examples of command ls that you can use to fulfill different requirements.

List Hidden Files 

We can use option -a if we want to list the hidden files of a specific folder, it would be enough to write:

ls –a path

In my case it would be something like that:

ls –a /home/angelo/

list hidden files in Linux

You can see all hidden files as well in above screenshot. We can identify hidden files by looking at the output as such files or folders start with dot (.).

Display Files with their Index Number

We can list files with their index number by running below command. To show files/folders with their index number we will use the option –i along with command ls. You can see i have used below command and output is showing in below image.

ls -i /home/angelo/
 

List folders with index number

List Files with full Details (Permissions, Index no, Owner, Create Date etc.)

We can use ls command to display information such as permissions and owner of the folder or file. We will be using option -I to show complete information about files and folders as shown in below screenshot.

ls -l /home/angelo/

Display all details of folders and files

This output shows us more specific information about what we have in the folder: First it shows the permissions on the folder, its index number, the owner of the folder, who created the folder, its size in kb, the date it was modified and the folder name.

List Files or Folders in a Reverse Order

ls command also gives an option to sort files and folders in reverse order. We can use -r option to show files and folders in a reverse order as shown in below screenshot after running below command.

ls –r  /home/angelo/

Display files and folders in reverse order

List Subdirectories Recursively

We can List subdirectories recursively by adding the -R option with ls command.  It will display the contents of the directory subfolders. Run below command to display the result.

ls –R /home/angelo/
 Display list of Directories Only

We can use -d and an asterisk option to list and show only the directories. Have a look at below command that has displayed the list of directories under /home/angelo.

ls –d */home/angelo/

Show directories with astrick

Display Files Sort by their File Size

Run below command to display files sort by file size. It is possible with the option –S.

ls -S /home/angelo/

Display files sort by file size

Sort by Date/Time

We can option -t to display files sort by date/time. Run below command to see the output that is also showing in below screenshot.

ls –t /home/angelo/

Display files sort by date time

Display Files as they were Created

Use option -U to show folders and files in no order at all. It will display just as they are created.

ls –U /home/angelo/

Display Files as they were created

Sort by File Extension

We can use option -X with ls command to sort files by their extension names. You can see this in below screenshot.

ls –X /home/angelo/

Display files sort by file extension

Sort by Modified Files

We can use combination of options -ltr to show last modification file or directory date. Simply run below command to display the output.

ls -lrt

ls -lrt [path]

In conclusion we can say that the ls command, even when it is relatively easy to use, is one of the tools most used by system administrators.

I hope you like this article. Please follow our Facebook page and Twitter handle to get latest updates.

Read More:

Angelo Marquez
Summary
Article Name
11 Examples of Linux Command ls to List Files
Description
Today we will learn the ls command utility that allows us to list files and folders from the terminal. Here we will also describe different combinations and options that can be used with ls command to get different output.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *