Introduction

Hello! In this article, we’ll learn about the ls command.

ls stands for “List”, and it’s a basic command used to display a list of files and directories. Along with cd, it’s one of the most frequently used commands when working with Linux.

What is the ls Command?

The ls command is an external command that displays information about files and directories contained in a specified directory (or the current directory). It can show not only file names but also detailed information such as permissions, owners, sizes, and modification times.

Basic Syntax

1
ls [options] [file/directory]

Main Options

Option Description
-l Display detailed information (long format)
-a Show hidden files (files starting with .)
-A Show hidden files except . and ..
-h Display file sizes in human-readable format (use with -l)
-t Sort by modification time, newest first
-r Reverse the sort order
-R List subdirectories recursively
-S Sort by file size, largest first
-1 List one file per line
--color Colorize the output (auto/always/never)

Usage Examples

Example 1: Basic Usage

1
ls

Result:

1
Desktop  Documents  Downloads  Music  Pictures  Videos

Files and directories in the current directory are displayed.

Example 2: Display Detailed Information

1
ls -l

Result:

1
2
3
4
5
total 24
drwxr-xr-x 2 user user 4096 Oct 26 10:00 Desktop
drwxr-xr-x 3 user user 4096 Oct 25 14:30 Documents
drwxr-xr-x 2 user user 4096 Oct 26 09:15 Downloads
-rw-r--r-- 1 user user 1234 Oct 24 12:00 notes.txt

Column meanings:

  • Column 1: Permissions (d = directory, - = file)
  • Column 2: Number of hard links
  • Column 3: Owner
  • Column 4: Group
  • Column 5: Size (bytes)
  • Columns 6-8: Last modification time
  • Column 9: File name

Example 3: Show Hidden Files

1
ls -a

Result:

1
.  ..  .bashrc  .config  Desktop  Documents  Downloads

Hidden files starting with . are also displayed.

Example 4: Detailed Info + Hidden Files + Human-Readable Sizes

1
ls -lah

Result:

1
2
3
4
5
6
7
8
total 32K
drwxr-xr-x  6 user user 4.0K Oct 26 10:00 .
drwxr-xr-x 10 root root 4.0K Oct 20 08:00 ..
-rw-------  1 user user  156 Oct 26 09:30 .bash_history
-rw-r--r--  1 user user  220 Oct 20 08:00 .bashrc
drwxr-xr-x  2 user user 4.0K Oct 26 10:00 Desktop
drwxr-xr-x  3 user user 4.0K Oct 25 14:30 Documents
-rw-r--r--  1 user user 1.2K Oct 24 12:00 notes.txt

File sizes are displayed in readable format like 1.2K or 4.0K. Super convenient!

Example 5: Sort by Modification Time

1
ls -lt

Result:

1
2
3
4
5
total 24
drwxr-xr-x 2 user user 4096 Oct 26 10:00 Desktop
drwxr-xr-x 2 user user 4096 Oct 26 09:15 Downloads
drwxr-xr-x 3 user user 4096 Oct 25 14:30 Documents
-rw-r--r-- 1 user user 1234 Oct 24 12:00 notes.txt

Recently modified files appear at the top.

Example 6: Sort by Size

1
ls -lhS

Result:

1
2
3
4
5
total 56K
-rw-r--r-- 1 user user 28K Oct 26 08:00 big-file.txt
drwxr-xr-x 2 user user 4.0K Oct 26 10:00 Desktop
drwxr-xr-x 3 user user 4.0K Oct 25 14:30 Documents
-rw-r--r-- 1 user user 1.2K Oct 24 12:00 notes.txt

Largest files are shown first. Great for finding files that are taking up disk space!

Example 7: List Subdirectories Recursively

1
ls -R

Result:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
.:
Desktop  Documents  Downloads

./Desktop:
project1  project2

./Documents:
report.txt  work

./Documents/work:
tasks.md

Contents of all subdirectories are also displayed.

Example 8: List Specific Directory

1
ls -l /etc

Result:

1
2
3
4
5
total 1024
drwxr-xr-x  3 root root 4096 Oct 20 08:00 apache2
-rw-r--r--  1 root root 2319 Oct 15 10:00 bash.bashrc
drwxr-xr-x  2 root root 4096 Oct 22 12:00 cron.d
...

Shows the contents of the specified directory (here /etc).

Tips & Notes

  • Default sort order: Files are listed in alphabetical order by default
  • Color display: Most distributions color-code directories (blue), executables (green), etc.
  • Combining options: -l -a -h can be written as -lah
  • Using wildcards: You can display only files matching a specific pattern like ls *.txt
    1
    2
    3
    
    ls *.txt        # Files ending with .txt
    ls test*        # Files starting with test
    ls [abc]*       # Files starting with a, b, or c
    
  • Using aliases: Setting alias ll='ls -lah' lets you shorten frequently used options
  • Reading permissions: Understanding drwxr-xr-x
    • 1st character: File type (d=directory, -=regular file, l=symbolic link)
    • 2nd-4th characters: Owner permissions (rwx=read, write, execute)
    • 5th-7th characters: Group permissions
    • 8th-10th characters: Other users’ permissions

Practical Usage

Find Recently Modified Files

1
ls -lt | head -5

Shows the 5 most recently modified files.

Find Large Files

1
ls -lhS | head -10

Displays the top 10 largest files.

Show Only Directories

1
ls -d */

Lists only directories in the current directory.

Show Only Hidden Files

1
ls -ld .*

Displays only hidden files starting with ..

Show Inode Numbers

1
ls -li

Also displays inode numbers for each file. Useful for checking hard links!

Summary

In this article, we learned about the ls command.

Key Points:

  • ls is a basic command for listing files and directories
  • -l for detailed info, -a for hidden files, -h for readable sizes
  • -t for time-based sorting, -S for size-based sorting
  • Options can be combined (like -lah)
  • Wildcards let you display specific files only

The ls command is an essential tool for exploring the file system. Try different options to find the usage style that works best for you!

Keep learning Linux commands!