Ls Command in Linux (List Files and Directories)

Ls Command in Linux (List Files and Directories)

What is ls?

The ls command stands for “list”. It is used in Linux/Unix to display the contents of a directory.

Basic Syntax

ls [options] [path]
  • options: Optional flags to modify the output

  • path: The directory to list (default is the current directory)

Step-by-Step Usage

 Step 1: List Files in Current Directory

ls

This shows files and directories in the current directory (excluding hidden ones).

Step 2: Show Hidden Files

Hidden files start with a dot (.), like .env or .git.

ls -a

Output includes files like .bashrc, .gitignore.

Step 3: Long Listing Format (Detailed Info)

ls -l

Shows:

  • File type and permissions

  • Number of links

  • Owner

  • Group

  • Size

  • Date/time

  • Filename

 Example:

-rw-r--r-- 1 user group 1234 Apr 5 12:34 file.txt

Step 4: Combine Options (e.g., Long + Hidden)

ls -la

Shows all files with detailed info.

Step 5: Human-Readable Sizes

ls -lh

File sizes are shown in KB/MB/GB instead of bytes.

Step 6: Sort by Time (Newest First)

ls -lt

Lists files sorted by modification time (latest first).

Step 7: Reverse Order

ls -lr

Lists in reverse alphabetical or time order (if combined with -t).

Step 8: List Files in a Specific Directory

ls /etc

List contents of the /etc directory.

Step 9: Recursively List All Files and Sub-directories

ls -R

 Shows the entire directory tree starting from the current directory.

Step 10: Colored Output

Most modern distros support ls --color or just color by default.

ls --color

 Colors differentiate file types (directories, links, executables).

Common ls Options Summary

OptionDescription
-aInclude hidden files
-lLong listing format
-hHuman-readable sizes (use with -l)
-RRecursive list
-tSort by time (newest first)
-rReverse sort order
-SSort by file size
-dList directories themselves, not contents

Examples

1. List files in /var/log sorted by size:

ls -lhS /var/log

2. Show hidden files with details in home:

ls -la ~

3. List only directories:

ls -d */

Pro Tips

  • Combine options freely like ls -lhtr

  • Use alias ll='ls -lh' for quicker long-listing

Practice Challenge

Try running:

ls -lahR /etc

To explore how /etc is structured recursively with all files and directories, including hidden ones.

Soeng Souy

Soeng Souy

Website that learns and reads, PHP, Framework Laravel, How to and download Admin template sample source code free.

Post a Comment

CAN FEEDBACK
close