ls is a command to list files and directories in a Linux or Unix system. The ls command is one of the most commonly used commands either by regular Linux users or more advanced users such as system admins. And you’ve probably also used the ls command.
Despite many Linux users already knowing about the ls command, there are still some functionalities that Linux users might still not know.
Therefore, in this article, we will learn about the ls command, how to use the ls command, the ls command options, and some examples of the ls command.
I will guide you with my best effort to explain the ls command, and each option it has, so you can understand it better.
Introduction to ls command
ls command is a simple yet powerful command in Linux. You can display and sort files and directories with many parameters. For example, you can sort files by date, file size, file extension, and many other parameters. Not just sorting, you can do other things too.
To use the ls command, follow the format below
ls [option] [directory]
the [option] and [directory] parts are optional, so you can just run plain ls command. You will see the usage of [option] and [directory] later in this guide.
Here for example we just run the ls command only
ls
and the output would be like this
linuxspin@myubuntu:~$ ls
Desktop Downloads ls.txt Pictures Public Templates
Documents history.txt Music ping.txt snap Videos
Basically, if you run ls, it will list all the files and directories that are available in your current working directory, but it does not include the hidden files and directories.
ls command options
It is correct that you can run the plain ls command without any options/arguments. But, what if you want more advanced features from ls command? here is come into play the options in the ls command.
To use the option, you can add the options you want to use after the ls command.
Here are some of the possible options. You can either use them individually or combine them with other options. In the next part of this tutorial, you will learn how to use and combine these options to do some specific things.
Option | Description |
---|---|
-a, –all | list all files and directories including the hidden ones |
-b, –escape | print escapes for nongraphic/special characters |
-d, –directory | list only directories, with additional */ argument |
-h, –human-readable | print file size with human-readable format(124K, 12M 3.6G) |
-i, –inode | print the index number of each file |
-l | list files and directories with long format |
-m | display separator using a comma instead of a space |
-N, –literal | print name without quoting |
-r | list in reversed order |
-R, –recursive | list all subdirectories recursively |
-s, –size | list all files including their sizes |
-S | sort files by size(largest first) |
-t | sort files by time(newest first) |
-X | sort files by extension(in alphabetical order) |
-1 | list only one file per line |
–help | show the help page to see more about ls documentation |
–version | show the version information |
ls command examples
Now that you already know the basic ls options, let’s see the ls command in action by looking at some of the examples.
List files in long format
ls -l
ls -l
basically will print files and directories in long format. The format is divided into 7 parts/columns, those parts are:
- file permissions
- number of links to the file
- file owner
- group owner of the file
- file size(bytes)
- last modified date of the file
- file/directory name
That’s a lot of information we can get by just using the ls -l
command. But, there are more, if you combine the -l
option with other options, you can get more results.
List files in the long format, including the hidden files.
ls -la
As you can see, all the hidden files that start with .
are now visible. That’s because we are adding the -a
option, which means that every hidden file or directory will be shown.
List files with human-readable file size
ls -lh
With ls -lh
, the file size now will be shown in human-readable format, which is easier to read. The format is usually shown as B, KB, MB, GB, or TB. for example 125K, 21M, 2.6G, and so on. Just to keep note that it only displays in a single-character format.
List specific directory
ls [directory]
Instead of listing the current working directory, you can list other specific directories too. Just specify the directory you want to list right after the ls command.
For example, you want to list the Documents directory. You can do that by running a command like this
ls Documents
The example result would be like this
linuxspin@myubuntu:~$ ls Documents/
docs.txt image.jpg sound-effect.wav template.svg
file.pdf important.pdf sounds.wav
List only directories
ls -d */
Here, the ls -d */
will show the directory only, so any other files won’t be visible. Also need to keep note that, the hidden directories also won’t be visible too. If you want to show the hidden directories, add the -a
option to the command. So the command would be like this ls -da */
Sort files by file size
ls -lSh
You can sort the list by file size using ls -lSh
. let’s break the -lSh
and see what each option does.
l
option will display the files in a long formatS
option will sort files by size, the sorting order is from largest to smallesth
option will show the file size in a human-readable format, so it is easier to read
Sort files by date
ls -ltr
Yes, you can sort the files by date/or time by using the -ltr
option.
- l option will display the files in a long format
- t option will sort files by date. the sort order is from newest to oldest
- r option to reverse the sort operation
By combining all these 3 options, we get the result of a long format sorted by date from oldest to newest date.
List all subdirectories recursively
ls -R
Basically, it will list all directories and every file or subdirectory inside it recursively. It is useful if you want to quickly look at every folder and see what is inside it.
Sort files by extension name
ls -lX
You can sort files by their extension name with the command ls -lX
.
Show the ls version
ls --version
Checking the version of ls can be done by using the --version
flag. There you can see the details about the ls version and some additional information.
Show the help menu
ls --help
With the --help
option, you can see the details about all available options you can use. There are also explanations for each option too, so you can easily understand what each option does.
Conclusion
ls
is a very useful command that we often use on a daily basis. Learning about the ls command and its options will make your workflow more efficient. Of course, there are quite a lot of options that we need to learn. but no need to rush, just learn the command that you like or the command that you use often.
And that’s everything about ls and its command examples. I hope you learn a lot from this guide and become more efficient with the ls command.