How to list files in Linux?
In this article we will study about How to list files in Linux?And this is done Using ls Command with flags.
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
Best-suited Linux courses for you
Learn Linux with these high-rated online courses
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.
- A Linux system with directories and files
- Basic understanding of command line interface
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
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 ..
For listing content of the parent directory two levels above:
ls ../..
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 /
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 */
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
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 *
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
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
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:
- File/directory name
- Size of file or directory
- Content author/owner
- Number of links to the content
- File/directory permissions
- Content creation and modification time
ls -l
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
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
To reverse the sorting order, we can make use of the -r flag, as shown in the below command:
ls -Sr
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
To reverse the sorting order, we can make use of the -r flag, as shown in the below command:
ls -tr
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!
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