The ls command stands for List; All it does is list the contents of the specified directory.
In this tutorial, I will walk you through several examples of using the ls command.
I will also share some practice questions to test your learning.
How to use the ls command
To use the ls command, you have to follow the simple command syntax:
ls [OPTIONS] Targeted_Directory
[OPTIONS]: is used to modify the default behavior of the ls command.
Targeted_Directory: This is where you provide the directory name or absolute path to the directory.
You may be wondering what happens when you use the ls command without any options. To answer this, I used the ls command in my current working directory:
As you can see, I have listed all available directories and files in the current working directory. But you can do more than just get the names and files in the current working directory.
Let me show you how.
1. List files and directories with ownership
One of the primary uses of the ls command is to find the permissions and ownership of a specific file or directory.
So you should use a file
-l option (also called long list) with the ls command:
Once you do that, you can expect similar outputs:
[email protected]:~$ ls -lh -rwxrw-r-- 1 sagar sagar 666M Dec 10 18:16 Fedora.iso
Notice how I used add
-h option? I will discuss it in the next section.
For now, if the output seems too complicated, let me simplify things for you:
As you can see, each block of text has its own meaning such as owner, group and others permissions.
If you want to dig deeper into file permissions, I recommend this Our detailed guide on file permissions in Linux:
2. Obtain information in a human-readable format
By default, the file size is shown in bytes which is not the best way to know your file size. So how do you get the same information but in a human readable format?
basic. You are using a file
-h option with ls command:
ls -l -h
Here’s a comparison between the default form and the human readable form:
Much better. is not it?
The ls command is good for knowing file sizes. However, it will not give you the directory size which is always displayed as 4K. To get the size of a directory, use the du command.
3. List of hidden files
Like any other file manager, the ls command will not list hidden files (I mean they are supposed to be hidden. Right?).
But what if you want to List of hidden files with normal files? To do this, you can use
As you can see, the file name starts with a period
. They are hidden files.
You can also use files
ls -A which works roughly the same as
ls -a It will not include
4. List files recursively
there Multiple ways to recursively list files One of them is using the ls command.
In case you didn’t know, listing files recursively means listing the files of all existing subdirectories until the last item of each subdirectory is displayed.
And to list files recursively, you can use an extension
-R Flag as shown:
In a way, it gives you the current directory structure. I personally prefer this one but you will have to install it first.
You don’t have to be in the directory to list its contents. You can also list the contents of a directory by providing its absolute or relative path like this:
5. Differentiate between files and directories while using ls
While different colors for files and directories should do the job. But for some reason, if you want to mark up files and directories here you have it.
In the ls command, you have a file
-F A tag that adds a forward slash
/ For each directory name:
6. List only files that have certain file extensions
There are times when you just want to list files with certain file extensions and trust me, this is the easiest of all.
To do this, you do not have to use any options. Just append the file extension with an asterisk
For example, if I just wanted to list the ISO files, I would use the following command:
7. Sort the output based on size
To sort the output based on file size, you must use the file extension
-S Flag and it will list the files from largest to smallest (descending):
Similarly, if you want to reverse this order to list the smallest files first, you can use the
-r Flag to reverse the order:
8. Sort files based on date and time
The ls command includes the modified time in its list.
To list the most recent files first, you can use an extension
-t Flag as shown:
You can use the
-r Flag as I explained earlier to reverse the order here as well.
This will give you the most recently modified files at the bottom of the screen. This is especially useful if you have a very large number of files in a directory and want to know which files have been modified recently. I used this while troubleshooting my software project.
Let’s recap what you’ve learned so far!
Here, I’m going to share a table with multiple options that were used with the ls command in this tutorial:
||Long list of files and directories|
||Prints information in a human-readable format|
||Include hidden files in the list|
||List files recursively|
||Add a forward slash to the directory name|
||List of files that have specific extensions|
||Sort files based on file size|
||Sort files based on time|
||reverse sort (with s or t)|
🏋️ and practice your learning
It’s always a good idea to practice what you’ve learned, which is why we try to add a practice section in every terminal guide.
So here are some simple exercises with the ls command:
- List of contents
- Save the command output in a file called output.txt
- Select the 3 most recent files (use time-based sorting)
- Display files based on their size but in reverse order
- Check if there are any hidden files
That will be a good practice for you. Stay tuned for more learning Linux commands.
And if you’re new to the terminal, don’t forget to follow our Terminal Basics series