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:
ls [option] [parameter]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
lsThis command lists all files and directories located in the current working directory.
List details of all files in a directory
ls -lUsing the -l flag provides a detailed list of all files in the specified directory.
List all files and directories in a specific directory
ls /path/to/directoryLists all files and directories located in the specified directory path.
List all files and directories with hidden files
ls -aThe -a flag shows all files, including hidden files that start with a dot (.)
Reverse the order of listing
ls -rListing files in reverse order (Z to A or newest to oldest).
List files in a human-readable format
ls -lhLists files with sizes in a readable format like KB, MB, etc.
List files with a specific file extension
ls *.txtLists 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:
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:
ls -aHow 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:
ls -lhHow 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:
ls -tHow 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:
ls -lHow can I list only directories with ls?
To list only directories, you can utilize the ls command with the following option:
ls -d */Applications of the ls command
- List files and directories in a specified directory:
ls- List all files and directories, including hidden ones:
ls -a- List files and directories with detailed information:
ls -l- List files and directories with human-readable file sizes:
ls -h- List files and directories sorted by modification time:
ls -t- List files and directories sorted by size:
ls -S- List files and directories with colorized output:
ls --color