While working with your projects, you obviously need to see the files and directories present in your project. Looking at your files and directories is a common verification task when you delete one or more files and/or directories from your machine. This task can be accomplished using the ls command in Linux. The Linux command is also useful when you wish to look at your files and directories structure recursively, which gives you more insights into your project. Besides, the ls command also helps to get more details, like size and permissions, associated with the files and directories. In this article, we will discuss various options/flags and command variations related to the ls command in Linux.
What is ls Command?
ls is short for “list” in Linux. It is a Linux terminal navigation command . So, the command basically lists all the files and directories present in your present working directory. You can provide additional flags and options to the ls command to list the contents of some other directory in the same machine. Using this command without any option will simply list all files and directories without any special pattern representation. It looks like this:
List Files Sorted by Modification Time
Using the -t option after the command will list the files sorted according to the last edited time. The first file will be the last edited file.
List files Recursively
Listing the files recursively means the root directory files and sub-directories get listed first followed by their child files, which forms a tree structure. This can be done by adding the -R option after the command as shown below:
Display Files Along With Their Size
Using the -lh option with the command will list the files along with the size of each file. “M” will denote size in Megabytes, “K” will denote Kilobytes and “G” will denote Gigabytes.
Display All the Information Related to the Files
You can use the -l option in order to list files along with the type of file, permissions, number of links, owner, group, size, last modified date and time and the name. All this information will be listed in the same order they were mentioned previously. There can be 4 types of files represented by the symbols.
- “-” : denotes the normal file
- “d” : denotes the directory
- “s” : denotes the socket file
- “l” : denotes the Link file
Below is the syntax for writing the ls command with the -l option:
Display Hidden Files
Adding the -a or -A option after the ls command displays all the hidden files as shown below:
In this article, we discussed the ls command in Linux. This is used to list the files and directories in various ways including sorting by modification date or recursively. Besides, you can also get details like what is the type of that particular file, the size of the file, and permissions and groups associated with the file. Hidden files present in your system can also be listed using the ls command. It is a handy Linux shell command that will get you complete details of your files and directories in your Linux machine.
People are also reading: