Skip to content

ls Command: List Directory Contents

Summary

The ls command is fundamental for listing files and directories within the Linux file system. It provides detailed information about the contents of a directory, allowing you to quickly identify and manage your files.

Introduction

The ls command, short for "list", is used to display the contents of a directory. Without any arguments, ls lists the files and subdirectories in the current working directory. It's a powerful tool that becomes even more useful when combined with various flags to customize the output.

Use case and Examples

Listing files in the current directory

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

Listing files with detailed information

ls -l
This command lists the files and directories in a long format, including permissions, number of links, owner, group, size, modification date, and name.

Listing all files, including hidden ones

ls -a
This command lists all files, including those that start with a dot (.), which are typically hidden files and directories.

Listing files in a specific directory

ls /home/user/documents
This command lists the files and directories located in the /home/user/documents directory. Replace /home/user/documents with the actual path you wish to examine.

Listing files with human-readable sizes

ls -lh
This command combines the long listing format (-l) with human-readable file sizes (-h), displaying sizes in units like KB, MB, and GB.

Commonly used flags

Flag Description Example
-l List in long format, showing detailed information. ls -l (displays permissions, owner, size, etc.)
-a List all files, including hidden files (those starting with .). ls -a (shows all files and directories)
-h Display file sizes in a human-readable format (e.g., 1K, 234M, 2G). ls -lh (long listing with human-readable sizes)
-t Sort by modification time, newest first. ls -lt (lists files sorted by modification time)
-r Reverse the order of the listing. ls -lr (lists files in reverse order of modification time)
-d List directories themselves, not their contents. ls -ld /home/user (shows details of the /home/user directory itself)


Share on Share on

Comments