How to Use the ls Command to List Files and Directories on Linux

As a beginner to Linux operating systems, it is important to understand the basics of the command line interface. The ls command is one of the most essential commands you need to know. This command helps you to list files and directories on your Linux system.
The ‘ls’ command lists files and directories in the current directory by default. However, you can also use it to list the files in a specific directory in the system. To use the ls command, open your terminal and enter the ‘ls’ command followed by any options or arguments you may require. Here is a beginner-friendly guide to using the ls command on Linux.
Listing Files and Directories in Current Directory
To list files and directories in your current directory, you can simply use the command ‘ls’ without any options. Open your terminal and write ‘ls’ as shown below:
Ls
This command will display all files and directories in your current working directory. The items are listed out in alphabetical order. Directories are displayed first, followed by files.
Listing Files and Directories in Specified Directory
To list files and directories in a specific directory, you need to specify the directory path as an argument. For example, to see the contents of the /etc directory, you can write:
ls /etc
The command above will display all items in the ‘/etc’ directory.
Viewing Hidden Files and Directories
Sometimes, you may need to view hidden files and directories. Hidden files and directories are those that start with a period (‘.’) in their names. They may contain hidden configuration files that applications use. By default, the ‘ls’ command does not display hidden files and directories. To view these files, you need to use the ‘-a’ option.
ls -a
This command will display all files and directories in your current working directory, including hidden files and directories.
Sorting Files and Directories
You can sort the files and directories displayed by the ls command. The default sorting is alphabetical. However, the ‘-t’ option sorts files by the time they were last modified. To sort the files and directories in ascending order based on size, you can use the ‘-S’ option. Here are some examples:
ls -t: Sorts files by time modified, newest first.
ls -S: Sorts files by size, biggest first.
ls -r: Reverses the default sorting order.
ls -l: Displays files in long format with detailed information.
There are many ways to use the ls command on Linux. However, these are some of the most basic and useful commands you’ll need as you work with the command line.
Conclusion
The ls command is a vital tool for every Linux user. Use it to see the contents of directories and to find files in your system. Use the -a option to view hidden files and directories, -t to sort them by time periods, and -S to sort them by their size. Once you master the basic usage of ‘ls’ command, you will be more confident in your use of the Linux command line interface.