When it comes to using the Linux command line, one of the most basic and important commands is the ls command, which is used to list the files and directories in the current directory.
At first glance, the output of the ls command may seem simple and self-explanatory. However, there are actually a number of options and modifiers that you can use with this command to customize its output and make it more useful for your needs.
Basic Usage
To use the ls command, simply open a terminal window and type ls followed by the Enter key. This will display a list of all the files and directories in the current directory, as shown in the following example:
$ ls
Desktop Documents Downloads Music Pictures Public Templates Videos
In this example, the ls command is used to list the contents of the home directory. The output shows that there are several directories in this directory, including Desktop, Documents, Downloads, Music, Pictures, Public, Templates, and Videos.
Listing Files in Long Format
One useful option that you can use with the ls command is the -l option, which displays the output in a long format. This format includes additional information about each file, such as the file permissions, number of links, owner, group, size, and modification time. Here is an example of using the ls command with the -l option:
$ ls -l
total 104
drwxr-xr-x 2 user user 4096 Feb 10 14:24 Desktop
drwxr-xr-x 2 user user 4096 Feb 10 14:24 Documents
drwxr-xr-x 2 user user 4096 Feb 10 14:24 Downloads
drwxr-xr-x 2 user user 4096 Feb 10 14:24 Music
drwxr-xr-x 2 user user 4096 Feb 10 14:24 Pictures
drwxr-xr-x 2 user user 4096 Feb 10 14:24 Public
drwxr-xr-x 2 user user 4096 Feb 10 14:24 Templates
drwxr-xr-x 2 user user 4096 Feb 10 14:24 Videos
In this example, the ls command is used with the -l option to display the long format output for the contents of the home directory. The output shows that each directory has a file size of 4096 bytes, and was last modified on February 10, 2023 at 14:24. The first column shows the file permissions, which are represented by a string of 10 characters. The first character indicates the file type (in this case, a directory), and the remaining nine characters indicate the file permissions for the owner, group, and others.
Listing Hidden Files
Another useful option that you can use with the ls command is the -a option, which displays hidden files. Hidden files are files that have a name that starts with a dot (.). These files are often used to store configuration settings and other important data, and are typically hidden from view to prevent accidental modification or deletion. Here is an example of using the ls command with the -a option:
$ ls -a
. .. .bash_history .bash_logout .bashrc .cache .config .local .nano
In this example, the ls command is used with the -a option to display the hidden files in the home directory. The output shows that there are several hidden files in this directory, including .bash_history, .bash_logout, .bashrc, .cache, .config, .local, and .nano.
Listing Files in Reverse Order
If you want to list the files in reverse order (i.e., from the most recently modified file to the least recently modified file), you can use the -r option with the ls command. Here is an example of using the ls command with the -r option:
$ ls -r
Videos Templates Public Pictures Music Downloads Documents Desktop
In this example, the ls command is used with the -r option to display the contents of the home directory in reverse order. The output shows that the Videos directory was most recently modified, followed by the Templates directory, and so on.
Listing Files by Size
If you want to list the files in order of size, you can use the -S option with the ls command. Here is an example of using the ls command with the -S option:
$ ls -S
total 104
drwxr-xr-x 2 user user 16384 Feb 10 14:24 Videos
drwxr-xr-x 2 user user 4096 Feb 10 14:24 Pictures
drwxr-xr-x 2 user user 4096 Feb 10 14:24 Music
drwxr-xr-x 2 user user 4096 Feb 10 14:24 Downloads
drwxr-xr-x 2 user user 4096 Feb 10 14:24 Documents
drwxr-xr-x 2 user user 4096 Feb 10 14:24 Desktop
drwxr-xr-x 2 user user 4096 Feb 10 14:24 Templates
drwxr-xr-x 2 user user 4096 Feb 10 14:24 Public
In this example, the ls command is used with the -S option to display the contents of the home directory in order of size. The output shows that the Videos directory is the largest (16384 bytes), followed by the Pictures directory (4096 bytes), and so on.
In this article, we have discussed the ls command and its various options and modifiers. We have seen how to use this command to list the files and directories in the current directory, as well as how to customize its output to display additional information about each file. By mastering the ls command and its options, you will be well on your way to becoming a proficient Linux user.
References
| Title | Author | Publication | Date |
|---|---|---|---|
| ls invocation | GNU Coreutils | GNU Coreutils Manual | 2021 |
| ls | Michael Kerrisk | Linux man-pages project | 2021 |