Menu
×
   ❮   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP C C++ C# AWS W3.CSS HOW TO BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING HTML & CSS BASH RUST TOOLS

Bash Tutorial

Bash HOME Bash Intro Bash Get Started

Basic Commands

Bash Commands Bash List (ls) Bash Change Dir (cd) Bash Print Dir (pwd) Bash Echo (echo) Bash Concatenate (cat) Bash Copy (cp) Bash Move (mv) Bash Remove (rm) Bash Timestamp (touch) Bash Make Dir (mkdir) Bash Find Files (find) Bash Manual (man) Bash Alias

Pipes and Redirection

Bash Redirection Bash Pipes (|) Bash Build Commands (xargs)

Text Processing

Bash Search Text (grep) Bash Pattern Scan (awk) Bash Stream Editor (sed) Bash Remove Section (cut) Bash Sort Lines (sort) Bash View End (tail) Bash View Start (head) Bash Count (wc) Bash Unique Lines (uniq) Bash Translate (tr) Bash Compare Files (diff)

System Monitoring

Bash Process Status (ps) Bash List Processes (top) Bash Disk Space (df) Bash Directory Usage (du) Bash Memory Usage (free) Bash Terminate (kill) Bash Uptime

Networking

Bash Ping Bash URL Transfer (curl) Bash Downloader (wget) Bash Remote Connect (ssh) Bash Secure Copy (scp) Bash File Sync (rsync)

File Compression

Bash Compress (zip) Bash Extract (unzip) Bash TAR Archive

File Permissions

Bash Ownership Bash Modify (chmod) Bash Ownership (chown) Bash Group (chgrp)

Scripting

Bash Syntax Bash Script Bash Variables Bash Data Types Bash Operators Bash If...Else Bash Case Bash Loops Bash Functions Bash Script Arguments Bash Read Input (read) Bash Exit Codes Bash Arrays Bash Strings Bash Here Documents Bash Trap Signals (trap) Bash Schedule (cron)

Bash Cert

Bash Certificate

Bash Reference

Bash Glossary

Exercises and Quiz

Bash Compiler Bash Exercises Bash Challenges Bash Quiz

Bash ls Command - List Directory Contents


Share

Using the ls Command

The ls command is used to list the contents of a directory.

The ls command can display files, directories, and information about them.


Basic Usage

To see what's in the current folder, use ls:

Example

ls
copy_of_my_file.txt                 images       myfolder    voiceover.wav
Cosmere_RPG_Beta_Rules_Preview.pdf  my_file.txt  report.csv
Try it Yourself »

In a terminal, ls shows the names in columns to save space, as in the examples on this page. When the output goes to a file, another command or the Try it editor, it shows one name per line.


Options Overview

The ls command has a variety of options to customize its output:

  • -l - Long listing format
  • -a - Include hidden files
  • -h - Human-readable sizes
  • -t - Sort by modification time
  • -r - Reverse order while sorting
  • -R - List subdirectories recursively
  • -S - Sort by file size
  • -1 - List one file per line
  • -d - List directories themselves, not their contents
  • -F - Append indicator (one of */=@|) to entries

Long Listing Format

The -l option gives you detailed information about files and folders.

It displays information such as:

  • file permissions
  • number of links
  • owner name
  • owner group
  • file size
  • time of last modification
  • file or directory name

This format is useful for getting a comprehensive overview of the file attributes.

Example: Long Listing Format

ls -l
total 24244
-rw-r--r-- 1 user user      890 Apr  9 07:50 copy_of_my_file.txt
-rw-r--r-- 1 user user 23777028 Jan 15 20:38 Cosmere_RPG_Beta_Rules_Preview.pdf
drwxr-xr-x 2 user user     4096 Apr  9 07:51 images
-rw-r--r-- 1 user user      890 Apr  9 07:48 my_file.txt
drwxr-xr-x 3 user user     4096 Apr  9 07:52 myfolder
-rw-r--r-- 1 user user   305366 Apr  9 07:48 report.csv
-rw-r--r-- 1 user user   720974 Apr  9 07:47 voiceover.wav
Try it Yourself »

Lines that start with d are directories. The total line is the disk space used, counted in 1K blocks.



Include Hidden Files

The -a option includes hidden files in the listing.

Hidden files in Unix/Linux systems start with a dot (e.g., .bashrc).

This option is helpful when you need to view or manage configuration files that are not visible by default.

Example: Including Hidden Files

ls -a
.                                   images           report.csv
..                                  my_file.txt      voiceover.wav
copy_of_my_file.txt                 myfolder
Cosmere_RPG_Beta_Rules_Preview.pdf  .my_secret_file
Try it Yourself »

. is the current directory and .. is the parent directory.


Human-Readable Sizes

The -h option makes file sizes easier to read by converting byte counts into kilobytes (K), megabytes (M), gigabytes (G), etc.

This option is particularly useful when you want to quickly assess the size of files and directories without manually converting bytes.

Example: Human-Readable Sizes

ls -lh
total 24M
-rw-r--r-- 1 user user  890 Apr  9 07:50 copy_of_my_file.txt
-rw-r--r-- 1 user user  23M Jan 15 20:38 Cosmere_RPG_Beta_Rules_Preview.pdf
drwxr-xr-x 2 user user 4.0K Apr  9 07:51 images
-rw-r--r-- 1 user user  890 Apr  9 07:48 my_file.txt
drwxr-xr-x 3 user user 4.0K Apr  9 07:52 myfolder
-rw-r--r-- 1 user user 299K Apr  9 07:48 report.csv
-rw-r--r-- 1 user user 705K Apr  9 07:47 voiceover.wav
Try it Yourself »

Sorting by Time

The -t option sorts files and directories by modification time, with the most recently modified files first.

This option is useful when you want to see the most recently updated files first.

Example: Sorting by Time

ls -t
myfolder             my_file.txt    Cosmere_RPG_Beta_Rules_Preview.pdf
images               report.csv
copy_of_my_file.txt  voiceover.wav
Try it Yourself »

Reverse Order

The -r option reverses the order of the sort.

When used in combination with other options like -t, it can display the oldest files first.

This option is useful for reversing the default sorting behavior to meet specific needs.

Example: Reverse Order

ls -r
voiceover.wav  my_file.txt                         copy_of_my_file.txt
report.csv     images
myfolder       Cosmere_RPG_Beta_Rules_Preview.pdf
Try it Yourself »

Recursive Listing

The -R option lists directories and their contents recursively.

This is useful for viewing the entire directory tree.

Example: Recursive Listing

ls -R
.:
copy_of_my_file.txt                 images       myfolder    voiceover.wav
Cosmere_RPG_Beta_Rules_Preview.pdf  my_file.txt  report.csv

./images:
1.png  2.png  3.png  4.png

./myfolder:
my_file.txt  new_directory

./myfolder/new_directory:
Try it Yourself »

Sort by Size

The -S option sorts files by size, with the largest files first.

This is helpful for quickly identifying large files in a directory.

Example: Sort by Size

ls -S
Cosmere_RPG_Beta_Rules_Preview.pdf  copy_of_my_file.txt  report.csv
voiceover.wav                       my_file.txt
images                              myfolder
Try it Yourself »

One File per Line

The -1 option lists one file per line, which is useful for scripts or when piping output to other commands.

In a terminal, it turns the columns into a list with one name on each line.

Example: One File per Line

ls -1
copy_of_my_file.txt
Cosmere_RPG_Beta_Rules_Preview.pdf
images
my_file.txt
myfolder
report.csv
voiceover.wav
Try it Yourself »

Directories Only

The -d option lists directories themselves rather than their contents.

This is useful for seeing directory names without contents.

Example: Directories Only

ls -d */
images/  myfolder/
Try it Yourself »

Append Indicator

The -F option appends an indicator character to entries (e.g., / for directories, * for executables).

Example: Append Indicator

ls -F
copy_of_my_file.txt                 images/      myfolder/   voiceover.wav
Cosmere_RPG_Beta_Rules_Preview.pdf  my_file.txt  report.csv
Try it Yourself »

Using Multiple Options

You can combine multiple options to create more complex commands.

For example, ls -l -a will display a detailed listing of all files and directories, including hidden files.

Example

ls -l -a
total 24256
drwxr-xr-x 4 user user     4096 Apr  9 07:53 .
drwxr-x--- 5 user user     4096 Apr  9 07:42 ..
-rw-r--r-- 1 user user      890 Apr  9 07:50 copy_of_my_file.txt
-rw-r--r-- 1 user user 23777028 Jan 15 20:38 Cosmere_RPG_Beta_Rules_Preview.pdf
drwxr-xr-x 2 user user     4096 Apr  9 07:51 images
-rw-r--r-- 1 user user      890 Apr  9 07:48 my_file.txt
drwxr-xr-x 3 user user     4096 Apr  9 07:52 myfolder
-rw-r--r-- 1 user user      890 Apr  9 07:48 .my_secret_file
-rw-r--r-- 1 user user   305366 Apr  9 07:48 report.csv
-rw-r--r-- 1 user user   720974 Apr  9 07:47 voiceover.wav
Try it Yourself »

You can also combine multiple options without a space between them. For example, ls -la:

Example

ls -la
total 24256
drwxr-xr-x 4 user user     4096 Apr  9 07:53 .
drwxr-x--- 5 user user     4096 Apr  9 07:42 ..
-rw-r--r-- 1 user user      890 Apr  9 07:50 copy_of_my_file.txt
-rw-r--r-- 1 user user 23777028 Jan 15 20:38 Cosmere_RPG_Beta_Rules_Preview.pdf
drwxr-xr-x 2 user user     4096 Apr  9 07:51 images
-rw-r--r-- 1 user user      890 Apr  9 07:48 my_file.txt
drwxr-xr-x 3 user user     4096 Apr  9 07:52 myfolder
-rw-r--r-- 1 user user      890 Apr  9 07:48 .my_secret_file
-rw-r--r-- 1 user user   305366 Apr  9 07:48 report.csv
-rw-r--r-- 1 user user   720974 Apr  9 07:47 voiceover.wav
Try it Yourself »


Free account

Track your progress

XP earned 0 Day streak 0 Spaces 0 League —

New

Learn in Adventure Mode W3Schools Adventure App

Coding fundamentals as bite-sized lessons and challenges.

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and privacy policy.

Copyright 1999-2026 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.