In this article, we are going to learn about ls Command in Linux. ls is probably the most used command and for good reason. With it, we can see directory contents and determine a variety of important file and directory attributes. As we have seen, we can simply enter ls to see a list of files and subdirectories contained in the current working directory.
ls Command in Linux
$ ls
Besides the current working directory, we can specify the directory to list, like so:
$ ls /usr
or even specify multiple directories. In this example we will list both the user’s home directory (symbolized by the ~ character) and the /usr
directory:
$ ls ~ /usr
We can also change the format of the output to reveal more detail:
$ ls -l
By adding -l to the command, we changed the output to the long format.
Options and Arguments
This brings us to a very important point about how most commands work. Commands are often followed by one or more options that modify their behavior and, further, by one or more arguments, the items upon which the command acts. So most commands look something like this:
command -options arguments
Most commands use options consisting of a single character preceded by a dash, such as -l. But many commands, including those from the GNU Project, also support long options, consisting of a word preceded by two dashes. Also, many commands allow multiple short options to be strung together. In this example, the ls command is given two options, the l option to produce long format output, and the t option to sort the result by the file’s modification time:
$ ls -lt
We’ll add the long option –reverse to reverse the order of the sort:
$ ls -lt --reverse
The ls command has a large number of possible options. The most common are listed below:
Option | Long Option | Description |
-a | –all | List all files, even those with names that begin with a period, which are normally not listed(i.e., hidden). |
-d | –directory | Ordinarily, if a directory is specified, ls will list the contents of the directory, not the directory itself. Use this option in conjunction with the -l option to see details about the directory rather than its contents. |
-F | –classify | This option will append an indicator character to the end of each listed name (for example, a forward slash if the name is a directory). |
-h | –human-readable | In long format listings, display file sizes in human-readable format rather than in bytes |
-l | Display results in long format. | |
-r | –reverse | Display the results in reverse order. Normally, ls displays its results in ascending alphabetical order. |
-S | Sort results by file size. | |
-t | Sort by modification time. |
A Longer Look at Long Format
As we saw before, the -l option causes ls to display its results in long format. This format contains a great deal of useful information. Here is the Examples directory from an Ubuntu system:
Let’s look at the different fields from one of the files and examine their meanings:
ls Long Listing Fields
[linuxips@ubuntu ~/Downloads]$ ls -l drwxrwxr-x 2 linuxips linuxips 4096 Aug 12 12:30 Backup drwxrwxr-x 2 linuxips linuxips 4096 Aug 12 12:30 Data -rw-rw-r-- 1 linuxips linuxips 0 Aug 12 12:30 Info -rw-rw-r-- 1 linuxips linuxips 276990976 Apr 15 02:07 wps-office.deb
Field | Meaning |
-rw-r—r– | Access rights to the file. The first character indicates the type of file. Among the different types, a leading dash means a regular file, while a d indicates a directory. The next three characters are the access rights for the file’s owner, the next three are for members of the file’s group, and the final three are for everyone else. |
1 | File’s number of hard links. |
2 | Directory’s number of hard links. |
linuxips | The user name of the file’s owner |
linuxips | The name of the group that owns the file |
4096 | Size of the file in bytes. |
Aug 12 12:30 | Date and time of the file’s last modification. |
Backup | Name of the file |
Conclusion
So this is the ls Command in Linux. This article was intended to share the five most practical examples of using the “ls” command in Linux. However, if you want to know more about this command’s usage, you can execute the “ls –help” command in Linux for accessing the help manual of this command. You can use it as you want. Enjoy!!!
Related Linux Tutorials: