How to list files in Linux?

How to list files in Linux?

5 mins read570 Views Comment
Updated on Aug 27, 2024 17:51 IST

In this article we will study about How to list files in Linux?And this is done Using ls Command with flags.

2022_11_MicrosoftTeams-image-2.jpg

If you are a user of the LINUX operating system, you might be familiar with the Command Line Interface(CLI). It comes in with a set of commands that can be very helpful while working in a UNIX system. In this article, we will learn the ls command in Linux. The ls command in Linux is used to list all the files and directories within a directory. This command can also be used with various flags to adapt the command per your need.In this article we are going to see How to list files in Linux?

Table of contents

Recommended online courses

Best-suited Linux courses for you

Learn Linux with these high-rated online courses

– / –
200 hours
2 K
20 hours
– / –
3 months
– / –
6 months
– / –
3 months
Free
8 hours
10 K
60 hours
– / –
3 months

Prerequisites

Before moving ahead with this article, we need to ensure the following prerequisites are met, as they will give you a better understanding of the ls command.

  1. A Linux system with directories and files
  2.  Basic understanding of command line interface
How to Delete a File in Linux?
How to Delete a File in Linux?
Need to clear some space on your Linux system? It’s easier than you might think! With a simple command, you can swiftly delete any file. But what’s the right way...read more
Difference between Linux and Unix
Difference between Linux and Unix
Linux and Unix are operating systems similar to Windows, Ubuntu, and Apple macOS. Operating Systems contain and manage all the programs and applications that run on the desktop or mobile....read more
How to Find Files in Linux?
How to Find Files in Linux?
The find command in Linux is powerful tool that allows to quickly and easily scan through the file system to find files and directories. In this article, we will discuss...read more

Also read: Top 65 Linux Interview Questions and Answers (2022)

The ls Command in Linux

The ls command, as discussed above, can be used to list files and directories within a directory in Linux. Now follow the below steps to get a hands-on experience with the ls command:

Step 1: Fire up the terminal.

Step 2: Type in the ls command in the terminal.

Using ls Command with flags

We can also use various options or flags with the ls command to dictate the listing of the files and directories. Let’s take a look at some key flags.

1. Listing files/directories in a specific directory:

To list the files and folders within a specific directory, we make use of the following syntax:

ls [path_of_target_directory]

Example: In the below example, we are in the home directory and will list the files and folders inside the Downloads directory using the below command:


 
ls ~Dowloads
Copy code

2. Listing files/directories in the parent directory:

To list files and directories in the parent directory of the current directory, we make use of the following syntaxes:

For listing content of parent directory one level above:


 
ls ..
Copy code

For listing content of the parent directory two levels above:


 
ls ../..
Copy code

Example 1: In the below example, we are in the Downloads directory and will list the contents of the parent directory( ie, Home), which is exactly one level above the current directory.

Example 2: In the below example, we are in the test directory within the Downloads directory and will list the contents of the parent directory( ie, Home), which is precisely two levels above the current directory.

Note: This pattern can be continued to list contents up to n number of parent directory levels.

3. Listing files/directories in the root directory:

To list files and folders within the root directory, we make use of the following syntax:


 
ls /
Copy code

Example: We will use the above syntax to list all the files and directories in the root directory.

4. Listing all directories only in a directory:

We can make use of the following command to list only the directories within a directory:


 
ls-d */
Copy code

Example: In the below example, we will list all the directories only within the home directory as shown below:

5. Listing all files only in a directory:

We can make use of the following command to list only the directories within a directory:


 
ls -f
Copy code

Example: In the below example, we will list all the files only within the home directory as shown below:

6. Listing all files with the subdirectories:

We can make use of the following command to list all the files with their subdirectories within a directory:


 
ls *
Copy code

Example: In the below example, we will list all the files with their respective subdirectories within the home directory as shown below:

7. Listing files recursively:

We can make use of the following command to list all the files and directories recursively within the current system:


 
ls -R
Copy code

Example: In the below example, we will list all the files with their respective subdirectories within the current system as shown below:

8. Listing all files with their sizes:

We can make use of the following command to list all the files with their respective sizes within a directory:


 
ls -s
Copy code

Example: In the below example, we will list all the files with their respective subdirectories within the home directory as shown below:

9. Listing all files in long format:

We can make use of the following command to list all the files in log format that includes the following pieces of information:

  1. File/directory name
  2. Size of file or directory
  3. Content author/owner
  4.  Number of links to the content
  5.  File/directory permissions
  6.  Content creation and modification time

 
ls -l
Copy code

Example: In the below example, we will list all the files with their respective content information within the home directory as shown below:

10. Listing all files, including hidden files:

We can make use of the following command to list all the files, including the hidden files within a directory.


 
ls -a
Copy code

Example: In the below example, we will list all the files, including the hidden files, within the home directory as shown below:

11. Listing all files ordered by size:

We can make use of the following command to list all the files within a directory and order them by size:


 
ls -S
Copy code

To reverse the sorting order, we can make use of the -r flag, as shown in the below command:


 
ls -Sr
Copy code

Example: In the below example, we will list all the files within the home directory and sort them by size as shown below:

12. Listing all files ordered by date and time:

We can make use of the following command to list all the files within a directory and order them by date and time:


 
ls -t
Copy code

To reverse the sorting order, we can make use of the -r flag, as shown in the below command:


 
ls -tr
Copy code

Example: In the below example, we will list all the files within the home directory and sort them by date and time as shown below:

Conclusion

These are some frequently used commands associated with the listing of files in Linux. A combination of flags can be used to get the file listing per your need. The above-listed commands can also be chained with each other to get the desired result. In this article we have studied about How to list files in Linux?

I hope you find this an exciting read!

About the Author

This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio