35 ls Command Examples in Linux (The Complete Guide)

Do you know how to list contents (files/directories) in Linux?

It’s straightforward.

In this article, you will learn what the ls command and its use is.

Promise me, you will read this article entirely, and I promise you that the concept of your ls command will be cleared till the end of this article.

In my opinion, if someone starts learning Linux, the first command he should learn is ls command.

Table of Contents

What is the use of ls command?

In simple language, ls command helps to list the contents of a directory in Linux with useful information.

ls command provides the following information’s:

  • Permission details
  • Content owner and group information
  • Modified Date and Time
  • Size (Normally ls lists sizes in Bytes, but we can also check the size in Human Readable format)
  • Inode information and so on

Note: What we call a folder in Microsoft Windows is called a directory in Linux.

We usually use Microsoft Windows, and we are experts in it, that’s why I compare my Linux articles with Windows similar features so that your concept becomes clear.

What will you do if you want to list the contents of any directory in Microsoft Windows?

Just double click on the folder whose contents you want to list, and its contents will be listed in front of you because Windows is a GUI (Graphical User Interface) based operating system.

Similarly, since Linux provides us with a Command Line Interface (CLI), we have to use the ls command to see what is inside a directory.

ls was initially introduced in the Unix operating system, and as Linux is made up of the Unix OS, we are using this command in Linux.

How to remember Linux commands?

Often people ask how to remember Linux commands easily. I have an excellent trick to remember these commands.

Here it is:

Most of the commands of Linux are derived from ordinary words. Hence this command can be easily remembered. Here some examples:

  • ls command came from the Word “List.
  • mkdir command came from the phrase “Make Directory.
  • rmdir command came from the phrase “Remove Directory.
  • du command came from the phrase “Disk Usage.
  • pwd command came from the phrase “Print Working Directory” and so on…

So, let’s try to understand ls commands and all its options with example.

Every command of Linux comes with many options, and by using these options, we can use the complete features of that command.

In the table below, you will find all the options related to this command.

OptionsExplanation
-lLong listing of Files & Directories
--authorPrint authors of the Content
~Display the contents of Home Directory
-aList Hidden Files & Directories
-Ado not list implied (.) and (..)
-RPrint Files & Directories Recursively
-dList directory themselves and not their content
-hDisplay size in Human Readable format
-1Display one File/Directory per line
-sPrint the allocated size of each File & Directory
-FDisplay files & directories with different indicators at the end
-iList Inode numbers
-xDisplay the contents in a line pattern
-mList contents in a comma-separated format
-QDisplay content names within Quotes
-nDisplay UID and GID of Files & Directories
-gOnly list Group names in a long listing format
-GOnly list Owner names in a long listing format
-uList Files & Directories as per Access Time
-rList files & directories in Reverse order
-SSort contents as per its Size
-tSort contents as per its Modification Time
--hideExclude/Hide file types from the List
--time-style="+%Y-%m-%d $newline%m-%d %H:%M"Display contents by different styled modified date format
-XSort files as per its Extension
--color=auto

--color=yes

--color=no

--color=never
Enable/Disable colors in Output
--help

man
Help/Manual page access
--versionCheck the version of ls command

You can use these options with its core command with the help of hyphen (-).

For example:

~$ ls -l

Here ls is the core command, and -l is the option.

Note: You can also address the option as a flag.

Syntax:

You have to follow the below syntax to use ls command.

ls [OPTION]... [FILE]...

1. Simple listing of Files & Directories

Run the following command to list the files and directories of the current directory in a simple format.

You can check your current directory using the pwd command.

By default, it lists the contents in alphabetical order.

Output:

~$ ls
Desktop  Documents  Downloads  examples.desktop  Music  Pictures  Public  Templates  Videos
Simple listing of files & directories

What you see in blue is the directory, and What you see in white is the file.

Almost all Linux distributions have this similar color pattern.

2. Long listing of Files and Directories

You can use the -l (It’s lowercase L) option to list contents in a long listing format.

Output:

~$ ls -l
total 44
drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:33 Desktop
drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Documents
drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Downloads
-rw-r--r-- 1 ubuntu ubuntu 8980 Apr 28 05:18 examples.desktop
drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Music
drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Pictures
drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Public
drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Templates
drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Videos
Long listing of Contents

As you can see, the output is divided into Seven columns with a lot of useful information related to files and directories.

So let’s try to understand one by one.

Column #1: First, we will understand the permission section :

  • The first character indicates the file type. Linux has many file types. Refer to the list below:
File TypeExplanation
-Regular File
dDirectory
nNetwork File
lSymbolic Link
cCharacter Special File
sSocket
bBlock Special File
pPipe (FIFO)
  • In Linux, permission has been divided into nine characters, which are distributed in User, Group, and others.

The first three characters belong to the Owner (---), Next three characters belong to Group (---), and Finally, the last three characters belong to Others (---). Refer to the snapshot above.

Let me tell you a little bit about how to recognize permissions. Like any other operating system, we have three types of permission in Linux :

  • r – Read
  • w – Write
  • x – Execute

Now that we have understood the file types and permissions, let’s move toward other information that we got in a long listing of contents and try to understand them.

  • Column #2: Hard links belong to the file.
  • Column #3: Owner of the file/directory
  • Column #4: Group to which the file/directory belongs
  • Column #5: Size (By Default prints in Bytes but we can display them in a human readable format)
  • Column #6: Last Modified Month, Date & Time
  • Column #7: Name of Files & Directories

You can also use the following command to long listing of contents.

~$ ll

This is an alias of the ls -l command, and by default, you will find this in every Linux distribution.

Alias of ls Command

3. List authors of the contents

Now, as you know that using -l option we can list owner and group details.

There are many chances that the owner and the author can be the same.

I thought it necessary to include it in this guide because it is a feature of ls.

So to list the authors of the content you can use --author option. You have to use this option with the combination -l.

Output:

~$ ls -l --author
total 52
drwxr-xr-x 4 ubuntu ubuntu ubuntu 4096 Jun  4 23:46 data
drwxr-xr-x 2 ubuntu ubuntu ubuntu 4096 Jun  4 05:39 data1
drwxr-xr-x 2 ubuntu ubuntu ubuntu 4096 Apr 28 05:33 Desktop
drwxr-xr-x 2 ubuntu ubuntu ubuntu 4096 Apr 28 05:25 Documents
drwxr-xr-x 2 ubuntu ubuntu ubuntu 4096 Apr 28 05:25 Downloads
-rw-r--r-- 1 ubuntu ubuntu ubuntu 8980 Apr 28 05:18 examples.desktop
-rw-r--r-- 1 ubuntu ubuntu ubuntu    0 Jun  4 22:19 file.txt
drwxr-xr-x 2 ubuntu ubuntu ubuntu 4096 Apr 28 05:25 Music
drwxr-xr-x 2 ubuntu ubuntu ubuntu 4096 Apr 28 05:25 Pictures
drwxr-xr-x 2 ubuntu ubuntu ubuntu 4096 Apr 28 05:25 Public
drwxr-xr-x 2 ubuntu ubuntu ubuntu 4096 Apr 28 05:25 Templates
drwxr-xr-x 2 ubuntu ubuntu ubuntu 4096 Apr 28 05:25 Videos
List Authors

4. List contents of a specific Directory

You can list the contents of a specific directory using ls command.

To do so, pass the path of the directory to the ls.

Here is an example:

I want to list the contents of Documents/data/ directory.

~$ ls -l Documents/data/
total 0
-rw-r--r-- 1 ubuntu ubuntu 0 Apr 28 06:44 test1.txt
-rw-r--r-- 1 ubuntu ubuntu 0 Apr 28 06:44 test2.txt
-rw-r--r-- 1 ubuntu ubuntu 0 Apr 28 06:44 test3.txt
-rw-r--r-- 1 ubuntu ubuntu 0 Apr 28 06:44 test4.txt
-rw-r--r-- 1 ubuntu ubuntu 0 Apr 28 06:44 test5.txt

5. List contents of multiple Directories

This command is the same as the above one.

The only difference is we have to mention the path of multiple directories instead of one.

Let me show you an example.

Here I want to list the contents of the following directories :

  1. data1
  2. data2
  3. data3

So the command would be:

~$ ls -l data1 data2 data3
data1:
total 0
-rw-r--r-- 1 ubuntu ubuntu 0 May  2 06:40 file1.txt
-rw-r--r-- 1 ubuntu ubuntu 0 May  2 06:40 file2.txt
-rw-r--r-- 1 ubuntu ubuntu 0 May  2 06:40 file3.txt
-rw-r--r-- 1 ubuntu ubuntu 0 May  2 06:40 file4.txt
-rw-r--r-- 1 ubuntu ubuntu 0 May  2 06:40 file5.txt

data2:
total 0
-rw-r--r-- 1 ubuntu ubuntu 0 May  2 06:40 test1.txt
-rw-r--r-- 1 ubuntu ubuntu 0 May  2 06:40 test2.txt
-rw-r--r-- 1 ubuntu ubuntu 0 May  2 06:40 test3.txt
-rw-r--r-- 1 ubuntu ubuntu 0 May  2 06:40 test4.txt
-rw-r--r-- 1 ubuntu ubuntu 0 May  2 06:40 test5.txt

data3:
total 0
-rw-r--r-- 1 ubuntu ubuntu 0 May  2 06:41 data1.txt
-rw-r--r-- 1 ubuntu ubuntu 0 May  2 06:41 data2.txt
-rw-r--r-- 1 ubuntu ubuntu 0 May  2 06:41 data3.txt
-rw-r--r-- 1 ubuntu ubuntu 0 May  2 06:41 data4.txt
-rw-r--r-- 1 ubuntu ubuntu 0 May  2 06:41 data5.txt

6. List contents of the Home Directory

You don’t have to mention the entire path of the home directory to list the contents.

All you have to do is pass the Tilde (~) Special character to ls because Tilde (~) Indicates Home Directory in Linux.

Example:

~$ ls -l ~
total 44
drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:33 Desktop
drwxr-xr-x 3 ubuntu ubuntu 4096 Apr 28 06:44 Documents
drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Downloads
-rw-r--r-- 1 ubuntu ubuntu 8980 Apr 28 05:18 examples.desktop
drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Music
drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Pictures
drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Public
drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Templates
drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Videos

7. List contents of Parent Directory

Let me explain to you what is the parent and current directory.

The directory within which the current directory exists is called the parent directory.

The directory in which i am working now is called the current directory, also known as Working Directory.

Parent Directory

In this example my current directory is /home/ubuntu/data.

~/data$ pwd
/home/ubuntu/data
~/data$ ls
dir1  dir2  file1.txt  file2.txt  file3.txt  file4.txt  file5.txt

But our purpose is to list the contents of the parent directory.

In this case, its /home/ubuntu.

To do so, type the following command.

~/data$ ls ../
data   Desktop    Downloads         file1.txt  Music     Public     Videos
data1  Documents  examples.desktop  file.txt   Pictures  Templates

You can also list the contents of 2nd & 3rd level Parent Directories.

[email protected]:~/data$ ls ../../
ubuntu
 
[email protected]:~/data$ ls ../../../
bin    dev   initrd.img      lib64       mnt   root  snap      sys  var
boot   etc   initrd.img.old  lost+found  opt   run   srv       tmp  vmlinuz
cdrom  home  lib             media       proc  sbin  swapfile  usr
Parent Directory Levels

8. List only files using ls command

By default, ls lists all available contents of a requested directory, but you can list only files using the following command.

~$ ls -l | egrep -v '^d'
total 80
-rw-r--r-- 1 ubuntu ubuntu  8980 Apr 28 05:18 examples.desktop
-rw-r--r-- 1 ubuntu ubuntu    23 May  1 07:18 file1
-rw-r--r-- 1 ubuntu ubuntu    21 May  1 07:01 file1.txtx
-rw-r--r-- 1 ubuntu ubuntu     0 May  1 06:56 file2

9. List only directories

To list only directories instead of all contents, use the following commands.

Method 1:

[email protected]:~$ ls -l | grep "^d"
drwxr-xr-x 2 ubuntu ubuntu  4096 May  1 05:46 Desktop
drwxr-xr-x 2 ubuntu ubuntu  4096 Apr 29 21:19 docs
drwxr-xr-x 3 ubuntu ubuntu  4096 Apr 28 06:44 Documents
drwxr-xr-x 2 ubuntu ubuntu  4096 Apr 28 05:25 Downloads
drwxr-xr-x 2 ubuntu ubuntu  4096 Apr 28 05:25 Music
drwxr-xr-x 2 ubuntu ubuntu  4096 Apr 29 21:19 my
drwxr-xr-x 2 ubuntu ubuntu  4096 Apr 28 05:25 Pictures
drwxr-xr-x 2 ubuntu ubuntu  4096 Apr 28 05:25 Public
drwxr-xr-x 2 ubuntu ubuntu  4096 Apr 28 05:25 Templates
drwxr-xr-x 2 ubuntu ubuntu 20480 May  1 07:13 test
drwxr-xr-x 2 ubuntu ubuntu  4096 Apr 28 05:25 Videos

Method 2:

~$ ls -ld */

10. List hidden Files and Directories

How to identify hidden files and directories in Linux?

It’s Simple.

In Linux, the file or directory that starts with a period (.) is hidden.

So to list the Hidden contents use -a option with ls.

~$ ls -a
.              .bashrc  docs              file1.txtx     .mozilla  .profile                   Templates
..             .cache   Documents         .gnupg         Music     Public                     Videos
.bash_history  .config  Downloads         .ICEauthority  my        .ssh
.bash_logout   Desktop  examples.desktop  .local         Pictures  .sudo_as_admin_successful

As you can see, Implied is also listed in the above output.

Hidden Files & Directories with Implied
  • . – for Current Directory
  • ..– for Parent Directory

If you don’t want to list Implied pass the -A option to ls.

~$ ls -A
.bash_history  data       examples.desktop  .local    Public
.bash_logout   data1      file1.txt         .mozilla  .ssh
.bashrc        Desktop    file.txt          Music     .sudo_as_admin_successful
.cache         Documents  .gnupg            Pictures  Templates
.config        Downloads  .ICEauthority     .profile  Videos

11. Recursively list the contents of Directories and Sub-Directories

What is Recursive?

Recursive means the executed command in Linux will work on a complete directory tree.

As per the following scenario, if I run the ls command to list the contents of the directory dir1 recursively, it will display the contents of all three directories (dir1, dir2 & dir3).

Directory Tree

To list contents, recursively pass the -R option to ls.

Example:

Here I am listing the contents of the directory data, which has two sub-directories i.e., dir1 & dir2.

~$ ls -R data
data:
dir1  dir2  file1.txt  file2.txt  file3.txt  file4.txt  file5.txt

data/dir1:
f1.txt  f2.txt  f3.txt  f4.txt  f5.txt

data/dir2:
note1.txt  note2.txt  note3.txt  note4.txt  note5.txt
List contents Recursively

12. Display one File/Directory per line

Use the following command to list one file/directory per line.

Note: ls -1(It’s Numeric One)

~$ ls -1
Desktop
Documents
Downloads
examples.desktop
Music
Pictures
Public
Templates
Videos

13. List directory themselves and not their content

To list the directories themselves instead of their content, pass the -d option to ls.

Refer to the following command.

~$ ls -ld data
drwxr-xr-x 4 ubuntu ubuntu 4096 Jun  4 05:25 data

14. Display size in Human Readable format

Usually, ls command displays the size of the file or directory in Bytes, which is difficult to understand.

But if it displays the size of the file in Human Readable Format (GB, MB, KB), then it will be easy to understand, which is possible with the help of ls command.

  • KB (K) – Kilo Bytes
  • MB (M) – Mega Bytes
  • GB (G) – Giga Bytes

To print the size in Human readable format, use the -h option. You have to use this option with the combination -l.

~$ ls -lh Documents/data/
total 40K
-rw-r--r-- 1 ubuntu ubuntu 1.4K Apr 28 06:51 rsyslog.conf
-rw-r--r-- 1 ubuntu ubuntu  19K Apr 28 06:51 services
-rw-r--r-- 1 ubuntu ubuntu 2.7K Apr 28 06:51 sysctl.conf
-rw-r--r-- 1 ubuntu ubuntu 1.3K Apr 28 06:51 ucf.conf
-rw-r--r-- 1 ubuntu ubuntu 4.9K Apr 28 06:51 wgetrc

15. Print the allocated size of each File & Directory

Usually, ls command prints the file size in a long listing format, but if you want to display the size in the simple listing of contents, use the -s option.

~$ ls -hs
total 56K
4.0K Desktop  4.0K Documents   12K examples.desktop  4.0K Music  4.0K Pictures  4.0K Templates
4.0K docs     4.0K Downloads  4.0K file1.txtx        4.0K my     4.0K Public    4.0K Videos

16. List files & directories with different indicators at the end

What is the use of Indicators?

Indicators are used to identify the file type. Here is the list of different types of indicators:

  • /– Directory
  • |– Pipe
  • @– Symlink.
  • *– Executable files
  • =– socket.

Use the -F option to append indicator to the entries.

~$ ls -F
data/   Desktop/    Downloads/        [email protected]  Pictures/  Templates/
data1/  Documents/  examples.desktop  Music/      Public/    Videos/

17. List Inode numbers using ls command

What is the Inode number?

An Inode is a unique number in Linux that is assigned to files.

Whenever you create a new file, it is assigned a file name and an Inode number.

Inode number contains the following information’s:

  • UID (User ID)
  • GID (Group ID)
  • File Size
  • Permissions
  • Modified Month, Date and Time
  • Device ID
  • Hard links
  • Other meta information’s related to that file and many more.

When you access a file, you use the name of that file from outside, but in the back end, it remains mapped to an Inode number that processes all requested tasks.

To print Inode numbers, you can use -i option.

~$ ls -li
total 44
659121 drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:33 Desktop
659125 drwxr-xr-x 3 ubuntu ubuntu 4096 Apr 28 06:44 Documents
659122 drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Downloads
659057 -rw-r--r-- 1 ubuntu ubuntu 8980 Apr 28 05:18 examples.desktop
659126 drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Music
659127 drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Pictures
659124 drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Public
659123 drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Templates
659128 drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Videos
Inode Numbers

18. List contents by lines

By default, ls command lists the content by columns.

Contents listed by columns

Pass the -x option to ls command to list entries by lines.

~$ ls -x
Desktop    docs    Documents  Downloads  examples.desktop  file1.txtx  Music  my  Pictures  Public
Templates  Videos

19. List contents in a comma-separated format

You can list the contents in a comma-separated format.

To do so, Pass the -m option to ls command.

~$ ls -m
Desktop, docs, Documents, Downloads, examples.desktop, file1.txtx, Music, my, Pictures, Public,
Templates, Videos

20. Print content names within Quotes

Use the -Q option with ls to print the content names withing Quotes.

Refer to the following sample output.

~$ ls -lQ
total 44
drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:33 "Desktop"
drwxr-xr-x 3 ubuntu ubuntu 4096 Apr 28 06:44 "Documents"
drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 "Downloads"
-rw-r--r-- 1 ubuntu ubuntu 8980 Apr 28 05:18 "examples.desktop"
drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 "Music"
drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 "Pictures"
drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 "Public"
drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 "Templates"
drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 "Videos"

21. Display UID and GID of Files & Directories

What is UID & GID?

By default, when we create a new user in Linux, it is assigned a UID and a GID.

  • UID stands for User Identifier
  • GID stands for Group Identifier

Let’s check the ID information of a user named helpdesk:

UID & GID Information

Usually, ls -l command lists the name of Owner and Group, but we can use the -n option to list UID & GID information.

~$ ls -ln
total 44
drwxr-xr-x 2 1000 1000 4096 Apr 28 05:33 Desktop
drwxr-xr-x 3 1000 1000 4096 Apr 28 06:44 Documents
drwxr-xr-x 2 1000 1000 4096 Apr 28 05:25 Downloads
-rw-r--r-- 1 1000 1000 8980 Apr 28 05:18 examples.desktop
drwxr-xr-x 2 1000 1000 4096 Apr 28 05:25 Music
drwxr-xr-x 2 1000 1000 4096 Apr 28 05:25 Pictures
drwxr-xr-x 2 1000 1000 4096 Apr 28 05:25 Public
drwxr-xr-x 2 1000 1000 4096 Apr 28 05:25 Templates
drwxr-xr-x 2 1000 1000 4096 Apr 28 05:25 Videos

22. Only display Group names in a long listing format

As we have seen, a long listing format lists both owner and group names.

But you can use the -g option to list only Group name instead of both.

~$ ls -lg
total 44
drwxr-xr-x 2 ubuntu 4096 Apr 28 05:33 Desktop
drwxr-xr-x 3 ubuntu 4096 Apr 28 06:44 Documents
drwxr-xr-x 2 ubuntu 4096 Apr 28 05:25 Downloads
-rw-r--r-- 1 ubuntu 8980 Apr 28 05:18 examples.desktop
drwxr-xr-x 2 ubuntu 4096 Apr 28 05:25 Music
drwxr-xr-x 2 ubuntu 4096 Apr 28 05:25 Pictures
drwxr-xr-x 2 ubuntu 4096 Apr 28 05:25 Public
drwxr-xr-x 2 ubuntu 4096 Apr 28 05:25 Templates
drwxr-xr-x 2 ubuntu 4096 Apr 28 05:25 Videos

23. Only display Owner names in a long listing format

If you want to list only Owner names, pass the -G option to ls.

~$ ls -lG
total 44
drwxr-xr-x 2 ubuntu 4096 Apr 28 05:33 Desktop
drwxr-xr-x 3 ubuntu 4096 Apr 28 06:44 Documents
drwxr-xr-x 2 ubuntu 4096 Apr 28 05:25 Downloads
-rw-r--r-- 1 ubuntu 8980 Apr 28 05:18 examples.desktop
drwxr-xr-x 2 ubuntu 4096 Apr 28 05:25 Music
drwxr-xr-x 2 ubuntu 4096 Apr 28 05:25 Pictures
drwxr-xr-x 2 ubuntu 4096 Apr 28 05:25 Public
drwxr-xr-x 2 ubuntu 4096 Apr 28 05:25 Templates
drwxr-xr-x 2 ubuntu 4096 Apr 28 05:25 Videos

You can also use the following command to get the same output.

~$ ls -o

24. Print last access time of contents

You can display the last access time of files and directories using -u option.

~$ ls -lu
total 52
drwxr-xr-x 4 ubuntu ubuntu 4096 Jun  4 23:46 data
drwxr-xr-x 2 ubuntu ubuntu 4096 Jun  4 05:48 data1
drwxr-xr-x 2 ubuntu ubuntu 4096 Jun  4 05:39 Desktop
drwxr-xr-x 2 ubuntu ubuntu 4096 Jun  4 22:48 Documents
drwxr-xr-x 2 ubuntu ubuntu 4096 Jun  4 22:48 Downloads
-rw-r--r-- 1 ubuntu ubuntu 8980 Apr 28 05:18 examples.desktop
lrwxrwxrwx 1 ubuntu ubuntu   27 Jun  4 21:59 file1.txt -> /home/ubuntu/data/file1.txt
-rw-r--r-- 1 ubuntu ubuntu    0 Jun  4 22:19 file.txt
drwxr-xr-x 2 ubuntu ubuntu 4096 Jun  4 05:39 Music
drwxr-xr-x 2 ubuntu ubuntu 4096 Jun  4 22:48 Pictures
drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Public
drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Templates
drwxr-xr-x 2 ubuntu ubuntu 4096 Jun  4 22:48 Videos

With the combination of -ltu option, you can sort the contents by its access time.

Latest accessed File/Directory will come in the first place.

~$ ls -ltu
total 52
drwxr-xr-x 4 ubuntu ubuntu 4096 Jun  4 23:46 data
drwxr-xr-x 2 ubuntu ubuntu 4096 Jun  4 22:48 Documents
drwxr-xr-x 2 ubuntu ubuntu 4096 Jun  4 22:48 Pictures
drwxr-xr-x 2 ubuntu ubuntu 4096 Jun  4 22:48 Videos
drwxr-xr-x 2 ubuntu ubuntu 4096 Jun  4 22:48 Downloads
-rw-r--r-- 1 ubuntu ubuntu    0 Jun  4 22:19 file.txt
lrwxrwxrwx 1 ubuntu ubuntu   27 Jun  4 21:59 file1.txt -> /home/ubuntu/data/file1.txt
drwxr-xr-x 2 ubuntu ubuntu 4096 Jun  4 05:48 data1
drwxr-xr-x 2 ubuntu ubuntu 4096 Jun  4 05:39 Music
drwxr-xr-x 2 ubuntu ubuntu 4096 Jun  4 05:39 Desktop
drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Templates
drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Public
-rw-r--r-- 1 ubuntu ubuntu 8980 Apr 28 05:18 examples.desktop

25. List files & directories in Reverse order

With the help of -r option, you can sort the contents alphabetically in reverse order.

~$ ls -lr
total 56
drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Videos
drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Templates
drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Public
drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Pictures
drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 29 21:19 my
drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Music
-rw-r--r-- 1 ubuntu ubuntu   21 Apr 29 21:23 file1.txtx
-rw-r--r-- 1 ubuntu ubuntu 8980 Apr 28 05:18 examples.desktop
drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Downloads
drwxr-xr-x 3 ubuntu ubuntu 4096 Apr 28 06:44 Documents
drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 29 21:19 docs
drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:33 Desktop

26. Sort contents in reverse order by modification time

To list the contents in reverse order by modification time, pass the -ltr option to ls command.

The latest modified content will come in the last place.

~$ ls -ltr
total 56
-rw-r--r-- 1 ubuntu ubuntu 8980 Apr 28 05:18 examples.desktop
drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Videos
drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Templates
drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Public
drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Pictures
drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Music
drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Downloads
drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:33 Desktop
drwxr-xr-x 3 ubuntu ubuntu 4096 Apr 28 06:44 Documents
drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 29 21:19 my
drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 29 21:19 docs
-rw-r--r-- 1 ubuntu ubuntu   21 Apr 29 21:23 file1.txtx

27. Sort contents by Size

To sort the files & directories by Size, you can pass the -S option to ls command. Largest file comes first.

[email protected]:~$ ls  -lhS
total 52K
-rw-r--r-- 1 ubuntu ubuntu 8.8K Apr 28 05:18 examples.desktop
drwxr-xr-x 4 ubuntu ubuntu 4.0K Jun  4 23:46 data
drwxr-xr-x 2 ubuntu ubuntu 4.0K Jun  4 05:39 data1
drwxr-xr-x 2 ubuntu ubuntu 4.0K Apr 28 05:33 Desktop
drwxr-xr-x 2 ubuntu ubuntu 4.0K Apr 28 05:25 Documents
drwxr-xr-x 2 ubuntu ubuntu 4.0K Apr 28 05:25 Downloads
drwxr-xr-x 2 ubuntu ubuntu 4.0K Apr 28 05:25 Music
drwxr-xr-x 2 ubuntu ubuntu 4.0K Apr 28 05:25 Pictures
drwxr-xr-x 2 ubuntu ubuntu 4.0K Apr 28 05:25 Public

28. Sort Files & Directories by modification time

Use the -t option to sort files & directories by modification time. The newest file comes first.

~$ ls -lt
total 56
-rw-r--r-- 1 ubuntu ubuntu   21 Apr 29 21:23 file1.txtx
drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 29 21:19 docs
drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 29 21:19 my
drwxr-xr-x 3 ubuntu ubuntu 4096 Apr 28 06:44 Documents
drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:33 Desktop
drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Downloads
drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Music
drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Pictures
drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Public
drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Templates
drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Videos
-rw-r--r-- 1 ubuntu ubuntu 8980 Apr 28 05:18 examples.desktop

29. List the newest and oldest file as per its modification time

Here I have listed some contents from which we will filter the latest and oldest file by modification time.

~$ ls -l
total 52
drwxr-xr-x 4 ubuntu ubuntu 4096 Jun  4 05:25 data
drwxr-xr-x 2 ubuntu ubuntu 4096 Jun  4 05:39 data1
drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:33 Desktop
drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Documents
drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Downloads
-rw-r--r-- 1 ubuntu ubuntu 8980 Apr 28 05:18 examples.desktop
lrwxrwxrwx 1 ubuntu ubuntu   27 Jun  4 21:59 file1.txt -> /home/ubuntu/data/file1.txt
-rw-r--r-- 1 ubuntu ubuntu    0 Jun  4 22:19 file.txt
drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Music
drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Pictures
drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Public
drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Templates
drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Videos

Use the following command to filter out the latest modified file. For that we need to combine ls with head command.

Note: Head command helps to display the first part of the file.

~$ ls -t | head -1
file.txt

Use the following command to filter out the oldest modified file.

~$ ls -t | tail -1
examples.desktop

30. Usage of Wildcards with the help of ls command

What is Wildcard?

In simple language, a wildcard is a symbol or a special character.

Due to which we can match the pattern of a word or a string to get our desired output. It helps a lot to save time.

Here are some popular Wildcard Characters :

  • * – Asterisk
  • [] – Brackets
  • % – Percent
  • ? – Question mark
  • ! – Exclamation Mark
  • # – Number sign (Hash)
  • - – Hyphen

Let’s take some examples so that your concept becomes clear.

Here I have listed some contents, and with the help of this data, we will try to understand wildcard.

~$ ls
file1.doc  file3.doc  file5.doc     services     test1.txt  test3.txt  test5.txt  wgetrc
file2.doc  file4.doc  rsyslog.conf  sysctl.conf  test2.txt  test4.txt  ucf.conf

Ex. # 1 – List those files that start with “fi

~$ ls fi*
file1.doc  file2.doc  file3.doc  file4.doc  file5.doc

Ex. # 2 – List those files whose extension is “.txt

~$ ls *.txt
test1.txt  test2.txt  test3.txt  test4.txt  test5.txt

Ex. # 3 – Exclude those files from the list whose extension is “.txt

~/data$ ls -l --hide=*.txt
total 8
drwxr-xr-x 2 ubuntu ubuntu 4096 Jun  4 05:25 dir1
drwxr-xr-x 2 ubuntu ubuntu 4096 Jun  4 05:25 dir2

31. List contents by different styled modified date format

Usually, ls command prints the modified date in MM-DD format.

But with the help of --time-style option, we can display the date format of our own choice. In fact you can also choose your time format.

Let’s take an example:

Print the Date in YYYY-MM-DD format and Time in HH:MM format.

~$ ls -lh --time-style="+%Y-%m-%d %H:%M"
total 52K
drwxr-xr-x 4 ubuntu ubuntu 4.0K 2020-06-04 23:46 data
drwxr-xr-x 2 ubuntu ubuntu 4.0K 2020-06-04 05:39 data1
drwxr-xr-x 2 ubuntu ubuntu 4.0K 2020-04-28 05:33 Desktop
drwxr-xr-x 2 ubuntu ubuntu 4.0K 2020-04-28 05:25 Documents
drwxr-xr-x 2 ubuntu ubuntu 4.0K 2020-04-28 05:25 Downloads
-rw-r--r-- 1 ubuntu ubuntu 8.8K 2020-04-28 05:18 examples.desktop
lrwxrwxrwx 1 ubuntu ubuntu   27 2020-06-04 21:59 file1.txt -> /home/ubuntu/data/file1.txt
-rw-r--r-- 1 ubuntu ubuntu    0 2020-06-04 22:19 file.txt
drwxr-xr-x 2 ubuntu ubuntu 4.0K 2020-04-28 05:25 Music
drwxr-xr-x 2 ubuntu ubuntu 4.0K 2020-04-28 05:25 Pictures
drwxr-xr-x 2 ubuntu ubuntu 4.0K 2020-04-28 05:25 Public
drwxr-xr-x 2 ubuntu ubuntu 4.0K 2020-04-28 05:25 Templates
drwxr-xr-x 2 ubuntu ubuntu 4.0K 2020-04-28 05:25 Videos

32. Sort files by Extension

Run the following command to sort files by Extension.

~$ ls -X -1
rsyslog.conf
sysctl.conf
ucf.conf
file1.doc
file2.doc
file3.doc
file4.doc
file5.doc
test1.txt
test2.txt
test3.txt
test4.txt
test5.txt

33. Enable/Disable colors in Output

As you know, Linux uses different colors to display files and directories. Like:

  • Blue color – Directory
  • White color – File
  • Sky Blue color – Symlinks
Different Colors

You can enable/disable the colorized output.

The following command will allow Linux to choose default colors.

~$ ls --color=auto

Use the below command to enable colors on output.

~$ ls --color=yes

You can use the following command to disable color on output.

~$ ls --color=never

The following output contains directories, and as you can see, the Colorized output is disabled.

Colorized Output

34. Help/Manual page access

Use the following commands to access the Manual Page/Help Page of ls command.

~$ ls --help
~$ man ls

35. Check the version of ls command

Check the ls command version using the following command.

~$ ls --version
ls (GNU coreutils) 8.28

Infographic

Refer to this Infographic for complete ls command options.

You can visit at following websites to get more information on ls command.

Conclusion

I hope you have learned something from this article and you may have found that ls is a very important command in Linux.

I have tried my best to include all the features of ls command in this guide.

Now I’d like to hear your thoughts.

Was this guide useful to you?

Or maybe you have some queries.

Have I not included any command in this guide?

Leave a comment below.

If you like our content, please consider buying us a coffee.

Buy Me A Coffee

We are thankful for your never ending support.

Leave a Comment