FUNDAMENTALS A Complete Guide for Beginners
ls is a popular shell command used in Linux to list the files and directories in a directory. The ls stands for “list” and when it runs, it automatically lists down all the files and folders in the current directory. But you can customize the behavior of this command by using different options with it. Since it’s one of the most used commands in Linux, you should learn the fundamentals of this command thoroughly.
Syntax of “ls” Command
The syntax for the ls
command is pretty simple which is the command itself followed by some specific options. Check the syntax:
ls [OPTION]... [FILE]...
Options of “ls” Command
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
Here are the most useful options:
Options | Description |
---|---|
-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. |
-r | Reverses the default order of listing. |
-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). |
-i | Lists all files and directories with inode (index number). |
-g | Displays the group ownership of files and directories instead of owner. |
Note: The options in Linux CLI(Command Line Interface) are all case-sensitive, So be cautious while using them.
7+ Examples of Using “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:
1. Using “ls” Command to List Files and directories
To list files and directories in Linux, you can use the ls
command. So type the following command in the command prompt of your terminal to list the contents of your current directory:
ls
You can see In the image that the contents of my current directory have been listed:
2. Using “ls” Command to List the Contents of Other Directories
To list the contents of other directories rather than your current directory, you can use ls
command. However, you have to specify the directory to do this. 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 typing the following command in the command prompt:
ls /home/softeko/
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
3. Using “ls” Command to Display Hidden Files and Directories in Linux
To display hidden files and directories in Linux, you cannot apply only the ls
command. To do so you have to use the -a
option along with the ls command. Hidden files can be recognized from their names as they start with a dot(.). You can do this task by writing this command:
ls -a
You can see In the image that the hidden contents of my desktop directory have been listed:
4. Using “ls” Command to List in Long Format
Listing in long format means to list contents with more information like file attributes, user name, group names, file size, date of modification along with the content name.
To list in long format, use the -l
option with the ls
command as follows:
ls -l
You can see In the image that the contents of my desktop directory have been listed in a long format:
5. Using “ls” Command to See Human Readable File Sizes
You can see the sizes of the contents with the ls -l
command however, they are not displayed in human-readable form like the following picture:
Therefore, to see human-readable file size like(1k, 5M, 20G), you can easily modify it by adding the option -h
with the option -l
:
ls -lh
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]
6. Using “ls” Command to Perform Recursive Listing of Subdirectories
To perform a recursive listing of the contents of the subdirectories, you can utilize the -R
option with the ls
command. Here’s how:
ls -R
You can see that I have listed the contents of the subdirectories of my Desktop directory with the help of the -R option:
7. Using the “ls” Command for Listing and Sorting in Linux
To list and sort the files and directories, you can use ls
command with different options. In this part, I will show you two cases of listing and sorting files and directories in Linux.
Case 1: 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. So, to sort by time, run the following command in the command prompt:
ls -lt
You can see that the listed contents were sorted according to their modification times(newest first):
Case 02: Sorting by Size
Again, you can sort the contents of your directory by size with the help of option -s
. It will display the largest content first. So, to sort by size, run the following command in the command prompt:
ls -lhS
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.
ls -lhSr
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.
People Also Ask
Why use ls command in Linux?
The ls
command is used in Linux to list the files and directories inside a directory. You can customize the output to show more detailed info, show hidden files, sort, and more. It’s a great tool for navigating directories and looking at files on the command line.
How to use ls?
To use the ls
command, you can simply write this command in your terminal. Additionally, to perform several tasks, you can use different options. You can find the options on the mas ls
page.
Is “ls” a system command?
Yes, in operating systems like Unix and Linux, the “ls” command is actually a system command.
How to read the output of “ls” command?
The ls
command’s output is simple to read. The 1st column shows the file’s permissions. The 2nd column shows how many links to the file there are. The 3rd column shows who owns the file, and the 4th column shows what group the file belongs to. The 5th column shows how big the file is in bytes, the 6th column shows when it was last changed, and the 7th column shows what the file or directory is called.
What is ls -l command in Linux?
The ls -l
command in Linux is used to list files and directories including all the information like file and directory name, permissions, ownership, modification time, size, group, etc. Here, -l
stands for long format.
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]