How to List Files in a Directory – ls command
If you are a user of the LINUX operating system, you might be familiar with the Command Line Interface (CLI) and ls command. CLI comes in with a set of commands that can be very helpful while working in a UNIX system.
In this article, we will look into the ls command in Linux. The ls command in linux is used for listing out all the files and directories within a directory. This command can also be used with a variety of flags to adapt the command as per your need.
Prerequisites:
Before moving ahead with this article, we need to make sure 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
The ls Command in Linux:
The ls command as discussed above can be used to list out 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.
Best-suited Linux courses for you
Learn Linux with these high-rated online courses
Using ls Command with flags:
We can also make use of 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
Output:
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 parent directory two level 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 exactly 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.
3. 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:
4. 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:
4. 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:
5. 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:
6. 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:
7. 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:
8. 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:
8. 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:
9. 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 as per your need. The above-listed commands can also be chained with each other to get the desired result.
Hope you find this an interesting 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