The `ls` command is a crucial tool in the repertoire of both novice and advanced Linux users. It’s a simple yet versatile command that provides a list of files and directories in the current directory by default. Beyond its most basic use, the ls command has numerous options that can provide extensive details about these files and directories, including permissions, size, last modification dates, and more.
In this article, you will learn about basic uses of the ls command in Linux along with few commonly used command-line options.
Basic Usage of
The most straightforward use of the ls command is to display the files and directories in your current directory. You simply type ls into the terminal and hit Enter.
Executing this command will provide a list of all non-hidden files and directories in your current directory.
Options in the
The ls command can be enhanced with a variety of options that customize and extend its utility. Here are some practical examples:
1. Listing Files in Long Format –
The -l option, or “long listing” option, provides additional information about each file and directory. This includes the file type and permissions, number of hard links to the file, the owner, the group, the size of the file in bytes, and the time of the last modification.
2. Displaying Hidden Files –
In Unix and Unix-like operating systems, files and directories that begin with a period (.) are hidden. The ls command will not display these files by default. However, you can view these hidden files with the -a or
3. Sorting Files by Time & Date –
If you want to view files based on their modification time, use the -t option. When combined with the -l option as -lt, it provides a detailed view of files, sorted by modification time (most recently modified first).
4. Displaying Files in Human Readable Format –
The -h or
--human-readable option prints file sizes in human-readable format (e.g., KB, MB, GB) rather than in bytes. This is especially useful when combined with the -l option to provide a more readable long format listing:
5. Displaying Directory Entries Instead of Contents –
By default, when you list a directory using ls, it will display the contents of the directory, not the directory itself. If you wish to view the directory as an entry, use the -d or
--directory option. This can be useful when you want to view details about the directory itself, such as its permissions or modification date.
ls -d */
Combining Multiple Options
Multiple options can be combined in a single ls command to provide powerful and customizable file and directory listings. For instance, if you want a long format listing (-l), including hidden files (-a), with human-readable sizes (-h), and sorted by modification time (-t), you can enter:
Listing Files in Other Directories
The ls command isn’t restricted to the current directory. You can specify a path to any other directory to list its files. For instance, to list the files in the /etc directory, you would enter:
While the ls command might seem simple on the surface, it is an incredibly powerful tool once you start exploring its various options. The examples provided here are just the tip of the iceberg. There are many more options available which you can explore by reading the ls manual page, accessed by typing man ls into the terminal.
Mastering the ls command, like any Linux command, requires practice. So, dive into your Linux terminal and start experimenting with these examples to unlock the full potential of the ls command.