December 21, 2024

How to Use the ‘ls’ Command in Linux: A Beginner’s Guide

The ls command is one of the most frequently used commands in Linux. It is used to list the contents of a directory.

This primer will walk you through the basics of using it and some of its most useful options.

Basic Usage

To use the ls command in its simplest form, open a terminal and type:

 ls

This will display a list of files and directories in your current working directory.

using the ls command

Common Options

List all files, including hidden ones:

 ls -a

This shows all files, including those that start with a dot (.), which are usually hidden.

show hidden files using ls

Display detailed information:

 ls -l

This shows a long listing format, including file permissions, number of links, owner, group, size, and timestamp.

long listing format using ls

Combine options:

 ls -la

display hidden contents in long listing format

This combines the above two options, showing all files in long listing format.

Display file sizes in human-readable format:

 ls -lh

This shows file sizes in KB, MB, GB as appropriate, instead of in bytes.

list file and directories in human-readable form

List subdirectories recursively:

 ls -R

This lists the contents of directories recursively.

list directory contents recursively

Advanced Usage

List files of a specific type:

 ls *.txt

This lists all files ending with .txt in the current directory.

display only text files using ls command

List files in reverse order:

 ls -r

list directories in the reverse order using ls

This reverses the order of the sort.

List directories only:

 ls -d */

This lists only the directories in the current folder.

list only directories using ls

Pro Tips

You can combine multiple options like:

 ls -lhart


This lists all files and directories, including hidden ones, in human-readable format, sorted by time (newest first).

using ls -lhart


Man page for ls

As always with most Linux commands, refer to man ls to access the full manual for the ‘ls’ command.

man page for the ls command

All done.

By admin

Related Post

Leave a Reply

Your email address will not be published. Required fields are marked *