Bash ls Command – List Directory Contents

Navigating a Linux system can feel like exploring a new city, and ls is your trusty map. Ever felt lost in your terminal, wondering where your files went? You are not alone! The ls command is one of the most fundamental and frequently used commands in the Linux/Unix terminal. It is used for listing directory contents and understanding your file system. This post is for anyone looking to master basic Linux navigation, from absolute beginners to those wanting to deepen their foundational knowledge.

What is ls?

At its core, ls is an abbreviation for “list directory contents.” Its primary purpose is to display the files and subdirectories present in a specified directory. If you don’t provide a path, it will show you the contents of your current working directory.

Basic Usage of ls

Let’s start with the basics of how to use this powerful command.

Simple Execution

The simplest way to use ls is just by typing it:

ls

This command lists the contents of your current working directory.

Example Output:

documents  downloads  pictures  public_html  videos

Listing a Specific Directory

You can also use ls to peek inside another directory without actually changing your current location.

ls /home/user/documents

This shows you what’s inside the /home/user/documents directory.

Listing Multiple Directories

Need to quickly check the contents of several locations at once? ls can do that too!

ls /path/to/dir1 /path/to/dir2

This is useful for quickly checking contents of several locations at once.

Understanding ls Output

By default, ls presents a simple, column-based output, usually sorted alphabetically.

Most terminals also use color-coding to differentiate file types. For instance, you might see blue for directories, green for executable files, and red for archives. This handy visual cue is often configured via an alias like ls --color=auto.

Common ls Options (Flags)

The real power of ls comes from its options, also known as “flags.” These modify how ls displays information.

-l (Long Listing Format)

The -l flag provides a detailed, long listing format for each file and directory.

ls -l

Example Output:

-rw-r--r-- 1 user group    1234 May 15 10:30 myfile.txt
drwxr-xr-x 2 user group    4096 Apr 20 08:00 mydirectory

Let’s break down each column:

  1. Permissions (-rw-r--r--): This string defines who can read, write, or execute the file/directory.
    • The first character (- or d) indicates the file type (- for a regular file, d for a directory).
    • The next three characters (rw-) are permissions for the owner.
    • The next three (r--) are permissions for the group.
    • The last three (r--) are permissions for others.
  2. Number of Hard Links (1): For files, this is usually 1. For directories, it indicates the number of subdirectories plus itself and its parent directory (typically 2 for an empty directory).
  3. Owner Username (user): The user account that owns the file or directory.
  4. Group Name (group): The group that owns the file or directory.
  5. File Size (1234): The size of the file in bytes. For directories, this usually shows the size of the directory entry itself, not its contents.
  6. Modification Date/Time (May 15 10:30): When the file or directory was last modified.
  7. Filename (myfile.txt): The name of the file or directory.

-a (All Entries)

Hidden files in Linux start with a dot (e.g., .bashrc). By default, ls doesn’t show them. The -a flag reveals these “dotfiles.”

ls -a

-h (Human Readable Sizes)

This flag is almost always used with -l to make file sizes easier to read. It converts byte sizes into more understandable units like KB, MB, or GB.

ls -lh

Example Output (with -lh):

-rw-r--r-- 1 user group  1.2K May 15 10:30 myfile.txt
drwxr-xr-x 2 user group  4.0K Apr 20 08:00 mydirectory

-R (Recursive Listing)

The -R flag lists the contents of subdirectories, then their sub-subdirectories, and so on. Think of it like seeing the entire file tree structure from a starting point.

ls -R

-t (Sort by Modification Time)

By default, ls sorts alphabetically. Use -t to sort files by their modification timestamp, with the newest files appearing first.

ls -t

-r (Reverse Order)

This flag reverses the default sorting order. It’s often used in conjunction with other sort flags. For example, ls -ltr would show the oldest files first when sorted by time.

ls -r
ls -ltr

-d (Directory Itself)

Normally, when you run ls -l on a directory, it lists its contents. The -d flag, especially when combined with -l, tells ls to list information about the directory itself, not its contents.

ls -ld /tmp

Example Output:

drwxrwxrwt 14 root root 4096 Jul 20 15:30 /tmp

-F (Classify)

The -F flag appends a character to each entry to classify it, making it easier to quickly identify file types.

ls -F

Common classifications:

  • /: Directory
  • *: Executable file
  • @: Symbolic link
  • |: FIFO (named pipe)
  • =: Socket

Combining ls Options

The true power of ls comes from combining these flags to get exactly the information you need.

Common Combinations

  • ls -lah: “Show all files (including hidden ones), in long format, with human-readable sizes.” This is a highly popular combination for a comprehensive, easy-to-read listing.
  • ls -lR /home/user/projects: “List all files and subdirectories recursively in long format within the /home/user/projects directory.”

Combining flags saves you time and provides highly specific output tailored to your needs.

Advanced Tips and Tricks

As you become more comfortable, here are a few ways to supercharge your ls usage.

Aliases

Typing common ls combinations repeatedly can be tedious. You can create a custom alias in your .bashrc or .zshrc file to shorten them. For example:

# In your ~/.bashrc or ~/.zshrc file
alias ll='ls -lhF'

After saving and reloading your shell (e.g., source ~/.bashrc), you can just type ll to get the output of ls -lhF!

Wildcards

Wildcards are powerful tools for pattern matching with ls.

  • * (Asterisk): Matches any sequence of zero or more characters.Bashls *.txt # Lists all files ending with .txt ls doc* # Lists files starting with 'doc'
  • ? (Question Mark): Matches any single character.Bashls doc?.md # Matches doc1.md, docA.md, but not doc10.md

Piping ls Output

The output of ls can be piped (|) to other commands for further processing.

  • Filter with grep: Find specific files.Bashls -l | grep ".txt" # Lists only .txt files in long format
  • Paginate with more or less: If ls output is very long, paginate it.Bashls -R | less # View recursive listing one screen at a time
  • Redirect to a file: Save the output to a text file.Bashls -l > file_list.txt # Saves the long listing to file_list.txt

FAQ’s – Linux/Unix Bash Command – ls


What is the ls command in Bash?
The ls command is used to list files and directories in the current working directory (or a specified path). It is one of the most commonly used commands in Unix-like systems to view directory contents.


How do I use the ls command to list files in a directory?
Simply type:

ls

This lists all non-hidden files and directories in the current location.

To list files in another directory:

ls /path/to/directory

How can I see hidden files using ls?
Use the -a option to show hidden files (those beginning with a dot .):

ls -a

Hidden files like .bashrc or .gitignore will now be shown.


How can I list files with details like size, permissions, and date?
Use the -l (long listing) option:

ls -l

Example output:

-rw-r--r--  1 user group   1024 Jul 18 10:00 file.txt

Explanation of columns:

  1. Permissions
  2. Number of links
  3. Owner
  4. Group
  5. File size
  6. Last modified date and time
  7. Filename

How do I list files sorted by modification time?
Use the -t flag:

ls -lt

To show the newest files last, reverse the order with -r:

ls -ltr

How do I list files in human-readable sizes?
Use -lh (long listing with human-readable sizes):

ls -lh

This displays file sizes in KB, MB, GB, etc.


How do I list only directories using ls?
Use a combination of ls and grep:

ls -l | grep "^d"

Or use ls -d */ to show only directories in the current folder:

ls -d */

How do I recursively list all files in subdirectories?
Use the -R flag:

ls -R

This shows the content of the current directory and all nested subdirectories.


How can I colorize the output of ls?
Use the --color=auto flag (this is often enabled by default):

ls --color=auto

Files, directories, symlinks, etc., will appear in different colors for better readability.


What does ls -la do?
It combines:

  • -l: long format listing
  • -a: includes hidden files

Example:

ls -la

This displays detailed information about all files, including hidden ones.


How do I list files in reverse alphabetical order?
Use the -r (reverse) option:

ls -r

To combine with long listing:

ls -lr

How do I sort files by size using ls?
Use the -S flag:

ls -lS

This lists files by size, largest first.

To reverse the order:

ls -lSr

How do I use wildcards with the ls command?
You can use patterns like * and ?:

ls *.txt       # Lists all .txt files
ls file?.sh    # Matches file1.sh, file2.sh, etc.

How can I count the number of files using ls?
Use ls with wc -l:

ls | wc -l

To include hidden files:

ls -a | wc -l

Why does ls output differ across systems?
The behavior and options of ls can vary depending on the shell (e.g., Bash, Zsh) and OS (Linux, macOS, BSD).
Use man ls to read the manual specific to your system.


How can I alias ls to always show colors and details?
Add this to your ~/.bashrc or ~/.bash_aliases:

alias ls='ls --color=auto -lh'

Then run:

source ~/.bashrc

Now ls will always show detailed, colorized listings.


What is the difference between ls and ls -F?
ls -F appends a character to indicate the file type:

  • / for directories
  • * for executable files
  • @ for symbolic links

Example:

ls -F

Author

Debjeet Bhowmik

Experienced Cloud & DevOps Engineer with hands-on experience in AWS, GCP, Terraform, Ansible, ELK, Docker, Git, GitLab, Python, PowerShell, Shell, and theoretical knowledge on Azure, Kubernetes & Jenkins. In my free time, I write blogs on ckdbtech.com

Leave a Comment