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:
Options:
Option | Description |
---|---|
-a | Shows all files, including hidden files |
-l | Displays long format listing |
-h | Human-readable sizes (e.g., 1K 234M 2G) |
-r | Reverse order while sorting |
-t | Sort by modification time |
-R | List subdirectories recursively |
-S | Sort by file size |
-d | List directories themselves, not contents |
-i | Print the index number of each file |
Parameters:
Parameter | Description |
---|---|
file | Specifies the file or directory |
ls Command Samples:
List all files and directories in the current directory
This command lists all files and directories located in the current working directory.
List details of all files in a directory
Using the -l flag provides a detailed list of all files in the specified directory.
List all files and directories in a specific directory
Lists all files and directories located in the specified directory path.
List all files and directories with hidden files
The -a flag shows all files, including hidden files that start with a dot (.)
Reverse the order of listing
Listing files in reverse order (Z to A or newest to oldest).
List files in a human-readable format
Lists files with sizes in a readable format like KB, MB, etc.
List files with a specific file extension
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:
How can I list all files, including hidden files?
To list all files, including hidden files, you can use the following ls command:
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:
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:
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:
How can I list only directories with ls?
To list only directories, you can utilize the ls command with the following option:
Applications of the ls command
- List files and directories in a specified directory:
- List all files and directories, including hidden ones:
- List files and directories with detailed information:
- List files and directories with human-readable file sizes:
- List files and directories sorted by modification time:
- List files and directories sorted by size:
- List files and directories with colorized output: