ls can be represented as an abbreviation for list. The ls command in Linux when executed by default lists down the files and directories in the current directory. it is one of the most used commands in Linux as you can view the contents of your desired directory without leaving the terminal. Therefore, you should learn this fundamental command thoroughly.
A. Description
The ls command by default gives you the lists of the contents of the current directory. However, you can use various available options to go even further with the ls command in Linux.
B. Syntax
The syntax for the ls command is pretty simple which is the command itself followed by some specific options.
ls [OPTION]... [FILE]...
C. Options
Many useful options are available for the ls command. I have listed some of the most used ones here. However, if you want to know more about options for ls command you can always check the man page for the ls command.
man ls
Useful Options
- -a: Displays hidden contents (Names starting with dot(.)).
- -d: Only lists the current directory, not its contents.
- -h: With option -l it shows file size in human-readable format(i.e. 1K, 100M, 10G).
- -l: Shows a detailed list consisting of owner name, group name, file size, date & time of modification, and the corresponding file name.
- -n: Similar to option -l but it lists numerically.
- -R: Performs listing on the subdirectories also.
- -s: Displays the size of the contents.
- -S: Displays in a sorted manner by size(largest first).
- -t: Lists contents in a sorted manner by time(newest first).
Practical Examples of “ls” Command in Linux
The ls command in Linux is an easy-to-use command that lists down the contents of a directory. Some of the most useful applications of the ls command have been illustrated below:
Example 1: Listing Files & Directories Using the “ls” Command
You can list files and directories using the ls command in Linux. To list the contents of your current directory you can follow the below procedure.
Steps to Follow >
➊ First, open the Ubuntu Terminal.
➋ Type the following command in the command prompt:
ls
➌ Now, press the ENTER button.
Output >
You can see In the image that the contents of my current directory have been listed.
Example 2: Listing the Contents of Other Directories
You can also list the contents of other directories rather than your current directory however you have to specify the directory. For example, Currently, I am in the directory named ‘Folder’ of my desktop directory. I can list the content of my home directory by using ls followed by the specific path to the directory. You can do the same by following the below procedure.
Steps to Follow >
➊ At first open the Ubuntu Terminal.
➋ Type the following command in the command prompt:
ls /home/softeko/
➌ Now, press the ENTER button.
Output >
You can see In the image that the contents of the home directory of the user named softeko on my pc have been listed.
Similar Readings
Example 3: Displaying Hidden Directories Using the “ls” Command in Linux
You cannot list the hidden files with only the ls command. Hidden files can be recognized from their names as they start with a dot(.). To list the hidden files in your directory you have to use the -a option. You can do the desired task by following the below procedure.
Steps to Follow >
➊ At first open the Ubuntu Terminal.
➋ Type the following command in the command prompt:
ls -a
➌ Now, press the ENTER button.
Output >
You can see In the image that the hidden contents of my desktop directory have been listed.
Example 4: Listing in Long Format Using the “ls” Command
You can list contents with more information like file attributes, user name, group names, file size, date of modification along with the content name by adding the -l option. You can execute this by following the below procedure.
Steps to Follow >
➊ At first open the Ubuntu Terminal.
➋ Type the following command in the command prompt:
ls -l
➌ Now, press the ENTER button.
Output >
You can see In the image that the contents of my desktop directory have been listed in a long format.
Example 5: Human Readable File Sizes Using the “ls” Command
You can see the sizes of the contents with the -l option however, they are not displayed in human-readable form.
ls -l
You can easily modify it to human readable forms like(1k, 5M, 20G) by adding the option -h with the option -l by following the below procedure.
Steps to Follow >
➊ At first open the Ubuntu Terminal.
➋ Type the following command in the command prompt:
ls -lh
➌ Now, press the ENTER button.
Output >
You can see In the image that the list contains sizes in human-readable form.
Similar Readings
- The “mv” Command in Linux [8 Practical Examples]
- The “cp” Command in Linux [6 Practical Examples]
- The “rm” Command in Linux [7 Practical Examples]
Example 6: Recursive Listing of Subdirectories as well
You can recursively list the contents of the subdirectories by utilizing the -R option in the following procedure.
Steps to Follow >
➊ At first open the Ubuntu Terminal.
➋ Type the following command in the command prompt:
ls -R
➌ Now, press the ENTER button.
Output >
You can see that I have listed the contents of the subdirectories of my Desktop directory with the help of the -R option.
Example 7: Listing & Sorting Using the “ls” Command in Linux
A. Sorting by Time
You can sort the contents of your directory by time with the help of option -t. It will display the newest content first. However, you have to follow the below procedure.
Steps to Follow >
➊ At first open the Ubuntu Terminal.
➋ Type the following command in the command prompt:
ls -lt
➌ Now, press the ENTER button.
Output >
You can see that the listed contents were sorted according to their modification times(newest first).
B. Sorting by Size
Again, you can sort the contents of your directory by time with the help of option -s. It will display the largest content first. You have to emulate the following process.
Steps to Follow >
➊ At first open the Ubuntu Terminal.
➋ Type the following command in the command prompt:
ls -lhS
➌ Now, press the ENTER button.
Output >
You can see that the listed contents were sorted according to their sizes(largest first).However, if you want to reverse the order of sorting you can simply add the option -r.
Steps to Follow >
➊ At first open the Ubuntu Terminal.
➋ Type the following command in the command prompt:
ls -lhSr
➌ Now, press the ENTER button.
Output >
You can see that the listed contents were sorted according to their sizes however, this time in reverse order(smallest first).
Conclusion
ls (list) is a basic command but one of the most used commands. As it empowers the user to view contents through the terminal without even using the GUI(Graphical User Interface). So, as a learner, you should explore more with the ls command in Linux. Hopefully, this article will show you the right path with the abovementioned practical examples.
Similar Readings
- The “touch” Command in Linux [8 Practical Examples]
- The “mkdir” Command in Linux [6+ Practical Examples]
- The “locate” Command in Linux [7 Practical Examples]
- The “find” Command in Linux [10+ Practical Examples]
- The “chmod” Command in Linux [6 Practical Examples]
- The “chown” Command in Linux [8 Practical Examples]