Skip to main content

The `ls` Command in Linux

The ls command is one of the most frequently used command in Linux. It stands for 'list', and as the name suggests, it's used to list the contents of a directory. This tutorial will walk you through the basic usage of ls command as well as some of its powerful options.

Basic ls Command

The simplest form of ls command is to run it without any options. Open your terminal and type:

ls --options arguments

This command will list the contents of the current directory.

ls Command Options Table

OptionLong OptionDescription
-a--allDo not ignore entries starting with .
-A--almost-allDo not list implied [Current Directory] and .. [Parent Directory]
-l--format=longUse a long listing format to display the information
-h--human-readableDisplay file size in human readable format (KB, MB, etc.)
-t--sort=timeSort by modification time, newest first
-r--reverseReverse order while sorting
-R--recursiveList subdirectories recursively
-p--indicator-style=slashAppend / indicator to directories
-F--classifyAppend indicator (one of */=>@ ) to entries
-S--sort=sizeSort by file size, largest first
-d--directoryList directories themselves, not their contents

Again, please note that the long options may not work in some systems, but the short options are universally supported in all Unix-like systems.

Listing Contents of Other Directories

You can also use ls to list contents of any other directory. To do so, you just need to provide the path of that directory:

ls /path/to/directory

Using ls Command Options

The ls command has a number of options that can be used to modify the output.

-l Option

The -l (lowercase 'L') option stands for 'long format', which includes additional information such as permissions, number of links, owner, group, size, and time of last modification:

ls -l

-a Option

The -a option stands for 'all'. By default, files that start with a dot (.) are hidden. If you want to display these files, use the -a option:

ls -a

-h Option

The -h option stands for 'human-readable'. It displays file size in KB, MB, etc., which is easier to comprehend than bytes:

ls -lh

Sorting ls Output

-t Option

The -t option sorts the output by modification time, newest first:

ls -lt

-r Option

The -r option reverses the order of sort:

ls -ltr

This will list the files and directories sorted by modification time, oldest first.

Displaying Directories Recursively

The -R (uppercase 'R') option stands for 'recursive'. It displays the contents of directories recursively:

ls -R

Listing Files by File Type

-p Option

The -p option appends a character to each entry indicating the file type. For instance, a slash (/) is appended to directories:

ls -p

-F Option

The -F option classifies files by outputting a character at the end of each. This will add a / at the end of directories, * at the end of executable files, @ at the end of symbolic links, etc.:

ls -F

Listing Files Based on Pattern

You can list files based on a specific pattern. For example, if you want to list all the text files, you can do so by:

ls *.txt

In summary, ls is a powerful command with numerous options. The examples above only scratch the surface. To learn more, you can always refer to the ls manual page by typing man ls in your terminal. The manual page provides a comprehensive list of all available options.

TypeScript Course Instructor Image
TypeScript Course Instructor Image

Time To Transition From JavaScript To TypeScript

Level Up Your TypeScript And Object Oriented Programming Skills. The only complete TypeScript course on the marketplace you building TypeScript apps like a PRO.


What Can You Do Next 🙏😊

If you liked the article, consider subscribing to Cloudaffle, my YouTube Channel, where I keep posting in-depth tutorials and all edutainment stuff for ssoftware developers.

YouTube @cloudaffle