Skip to content

ls Linux Command Guide

The ls command in Linux is used to list files and directories within a specified directory. It provides a simple way to view the contents of a folder quickly. Users can customize the output by using various options such as -l for detailed information, -a to show hidden files, and -h for human-readable sizes. By mastering the ls command, you can improve your efficiency in managing files and navigating through the Linux file system.

ls Syntax:

Terminal window
ls [option] [parameter]

Options:

OptionDescription
-aShows all files, including hidden files
-lDisplays long format listing
-hHuman-readable sizes (e.g., 1K 234M 2G)
-rReverse order while sorting
-tSort by modification time
-RList subdirectories recursively
-SSort by file size
-dList directories themselves, not contents
-iPrint the index number of each file

Parameters:

ParameterDescription
fileSpecifies the file or directory

ls Command Samples:

List all files and directories in the current directory

Terminal window
ls

This command lists all files and directories located in the current working directory.

List details of all files in a directory

Terminal window
ls -l

Using the -l flag provides a detailed list of all files in the specified directory.

List all files and directories in a specific directory

Terminal window
ls /path/to/directory

Lists all files and directories located in the specified directory path.

List all files and directories with hidden files

Terminal window
ls -a

The -a flag shows all files, including hidden files that start with a dot (.)

Reverse the order of listing

Terminal window
ls -r

Listing files in reverse order (Z to A or newest to oldest).

List files in a human-readable format

Terminal window
ls -lh

Lists files with sizes in a readable format like KB, MB, etc.

List files with a specific file extension

Terminal window
ls *.txt

Lists only files with a specific file extension, such as .txt files.

How do I use ls in Linux?

To use the ls command in Linux, execute the following command:

Terminal window
ls --option <value>

How can I list all files, including hidden files?

To list all files, including hidden files, you can use the following ls command:

Terminal window
ls -a

How can I display file sizes in human-readable format with ls?

To display file sizes in human-readable format while using the ls command, use the following command:

Terminal window
ls -lh

How do I sort the files by modification time using ls?

To sort the files by modification time, you can utilize the ls command with the following options:

Terminal window
ls -t

How can I view detailed information about files with ls?

To view detailed information about files, such as permissions, owner, group, size, and modification date, execute the ls command with the following flags:

Terminal window
ls -l

How can I list only directories with ls?

To list only directories, you can utilize the ls command with the following option:

Terminal window
ls -d */

Applications of the ls command

  1. List files and directories in a specified directory:
Terminal window
ls
  1. List all files and directories, including hidden ones:
Terminal window
ls -a
  1. List files and directories with detailed information:
Terminal window
ls -l
  1. List files and directories with human-readable file sizes:
Terminal window
ls -h
  1. List files and directories sorted by modification time:
Terminal window
ls -t
  1. List files and directories sorted by size:
Terminal window
ls -S
  1. List files and directories with colorized output:
Terminal window
ls --color