
The ls command is an important utility used to list directories and to display information about the included files, and attributes. It's also known as list (ls) and enables you to efficiently navigate through a file system.
This article explains how to use the ls command in Linux to perform directory listing tasks.
ls Command SyntaxBelow is the basic ls command syntax:
Within the above command, [options] includes optional flags that modify the command's behavior, and [file/directory] specifies the file or directory to list. If no file or directory is specified, ls displays the working directory contents.
ls Command Optionsls CommandList files in your working directory.
The above command lists all files in your working directory in alphabetical order.
Output:
Long list the working directory to view detailed information about files and directories.
The above command lists all directory contents with detailed information about each file and sub-directory including the permissions, owner, group, size, and modification time.
Output:
List all files in the working directory including hidden files.
The above command lists all files and directories in your working directory including hidden files and directories.
Output:
Sort files by last modification time.
The above command sorts files and directories using the last modification time in descending order.
Output:
Display all files with the respective sizes in a human-readable format.
The above command lists all directory file sizes in a human-readable format for easier interpretation.
Output:
List files and directories recursively.
The above command recursively lists all files and directories, including sub-directories.
Output:
Display inode numbers of all files and directories.
The above command displays the inode numbers for all files in your working directory.
Output:
Display the group ownership of files and directories.
The above command displays the group ownership of files and directories in your working directory.
Output:
List all directories and exclude contents.
The above command lists directories without include the available contents and appends a slash / to each directory result.
Output:
List files with combined options to view more details with a human-readable output.
The above command combines long listing -l, human-readable -h, and sorting -S options to display detailed information.
Output:
List files with color-coding.
The above command adds color-coding to the output based on the file types to differentiate file types and directories.
Output:
List files with a specific pattern.
The above command lists all files with the .txt extension in your working directory.
Output:
Display directory sizes.
The above command combines ls with du (disk usage) to list directories and their respective sizes in a human-readable format.
Output:
List all files with the respective permissions and timestamps.
The above command lists all directory files and sorts the output using the last modification time.
Output:
List files with inodes and human-readable sizes.
The above command combines the -l, -i, and -h options to show detailed information, inode numbers, and human-readable sizes.
Output:
List files and pipe the output to grep to filtering specific files.
The above command lists files in a long format and pipes the output to grep results to display files that match the filename string.
Output:
List directories that match a specific pattern.
The above command lists directories matching a specific pattern.
Output:
List and count files and directories in the working directory.
The above command lists the directory contents and pipes the output to wc -l to count the number of files in the directory.
Output:
List and sort files by size in descending order.
The above command lists files in long format and sorts them by size in descending order.
Output:
List only files (not directories).
The above command lists all files and directories, and pipes the output to grep -v / to exclude directories.
Output:
Display files modified in the last 7 days.
The above command finds all files modified in the last 7 days and lists them in long format.
Output:
You have used the ls command in Linux to list directory contents, navigate the file systems, and manage files and directories. For more command options, run the man ls command to view the ls manual page.
0 Comments
Be the first to comment and share your perspective with the community.