Listing files is a commonly performed task for regular Linux users and system administrators. In Linux, the “ls” command is used to display the contents of a directory, whether it’s the current directory or any other directory on the system.

The “ls” command shows both files and subdirectories and often utilizes color codes to differentiate between various file types. When used without any command-line options, it simply lists all the contents of the directory. However, it offers a variety of useful command-line options to customize the output and display the desired information.

In this article, we will explore the fundamentals of using the “ls” command in Linux, along with a comprehensive overview of its various command options. We’ll delve into practical examples to illustrate how these options can be employed effectively.

ls Command Options in Linux

The ls command takes the following syntax:

$  ls [ options ] /path/to/directory

The options section comprises the command-line arguments that can be utilized to modify the output of the “ls” command.

In this tutorial, we will cover the following “ls” command arguments:

ls -m	Lists directory contents separated by a comma.
ls -Q	Displays directory contents enclosed by quotation marks.
ls -l	Displays files in a long-list format.
ls -lh	Display file size in a human-readable format.
ls -g	Omits group ownership column.
ls -F	Adds a forward slash to directories.
ls -i	Display inode number of files and directories.
ls -a	Display all files including hidden files.
ls *.	Filters files according to the file extension.
ls -la	Displays all files and directories in long list format.
ls -R	Display files and directories recursively.
ls -r	Sort Files in reverse.
ls -X	Sort files alphabetically by file extension.
ls -tl	Display files according to file creation date and time.
ls -n	List UIDs and GIDs.

1. List Files and Directories in Linux

When executing the “ls” command without any command-line options or arguments, it will display the contents of the directory in alphabetical order. However, it won’t provide detailed information such as file types, sizes, modified date and time, permissions, and links. The basic output will only consist of the names of the files and directories present in the directory.

$ ls

2. Long Listing of Files in Linux

The “-l” command option is used with the “ls” command to print detailed information about the contents of a directory. It displays the information in a columnar format, including the size of each file or directory, the modified date and time, the name of the file or directory, the owner of the file, and the file’s permissions. This option provides a more comprehensive view of the directory contents.

$ ls -l

Starting from the far left, we have:

1st column – File/directory permissions.

2nd column – Number of links.

3rd column – Name of the owner.

4th column – Name of the group that the file belongs to.

5th column – File size in bytes.

6th column to 8th column – Last modification date.

9th column – File / Directory name.

3. List Hidden Files and Directories

Hidden files are files that are prefixed with a dot (.) in their filenames. These files store user settings and configuration information used by running programs and services.

For instance, the “.bashrc” file is a script that holds user-specific settings and configurations for the currently logged-in user. It includes command aliases, shell history, terminal font coloring, and more.

The “.bash_logout” file, on the other hand, is executed when you log out of your bash sessions. It primarily serves as a means of performing cleanup operations or any necessary tasks upon exiting the bash shell.

To list hidden files, pass the -a option as shown, which displays both hidden files and directories.

$ ls -a

4. List All Files in Linux

Indeed, the “-a” option is used with the “ls” command to display all files and directories, including hidden files. To enhance the viewing experience and obtain detailed information, the “-l” option can be combined with the “-a” option, forming the “-la” option.

By using the “-la” option, the “ls” command will provide a detailed listing of all files and directories, including hidden files, in a columnar format. This option is particularly useful when you want to view comprehensive information about all the files and directories in a directory, including their permissions, sizes, owners, modified dates, and more.

$ ls -la

5. Display File Size in a Human-readable Format

To enhance the presentation of the output, you can include the “-h” flag in conjunction with other options. When used with the “ls” command, the “-h” flag formats the file sizes in a human-readable format, making them easier to understand. The file sizes are displayed in units such as Kilobytes (KB), Megabytes (MB), and Gigabytes (GB). This improves the readability and presentation of the file size information in the output of the “ls” command.

$ ls -lh

6. Distinguish Directories and Files in Linux

To distinguish between files and directories more clearly when using the “ls” command, you can utilize the “-F” option. When combined with the “ls” command, the “-F” option appends a forward slash (/) to directories in the output. This visual indicator helps differentiate directories from regular files, making it easier to identify and distinguish them within the listing.

$ ls -F

7. Sorting Files in Reverse Order

By default, the “ls” command sorts files and directories in alphabetical order, from A to Z. However, if you prefer to sort the directory contents in reverse order, you can utilize the “-r” option. When used with the “ls” command, the “-r” option reverses the sorting order, displaying the files and directories in reverse alphabetical order (from Z to A). This option can be handy when you want to quickly identify the latest or most recently modified files in a directory.

$ ls -lr 

In addition, you can sort the file extensions alphabetically using the -X flag.

$ ls -X

8. List Files Recursively in Linux

The “-R” flag is used with the “ls” command to list files recursively. When you execute the “ls” command with the “-R” flag, it starts by listing all the files and directories in your current directory. It then continues to display the files contained within each individual directory, as well as any subdirectories.

This recursive listing provides a comprehensive view of the directory structure, including the contents of nested directories. It can be helpful when you need to examine files and directories across multiple levels of hierarchy within a given directory.

$ ls -R

In the following example, the files in individual directories have been listed as well.

9. Sort Files By Modification Time in Linux

You are correct! The “ls -ltr” command combines multiple options to display files in a long listing format, sorted in reverse order based on their modification time. Let’s break down the options used in this command:

“ls” is the command itself.

“-l” is used to display the output in a long listing format, providing detailed information about each file or directory.

“-t” is used to sort the files and directories based on their last modification time, with the most recently modified files appearing first.

“-r” is used to reverse the sorting order, displaying the files in reverse chronological order based on their modification time.

So, the “ls -ltr” command will display the files and directories in a detailed format, with the most recently modified files appearing at the bottom of the output. This can be useful when you want to see the latest changes or updates in a directory.

$ ls -ltr

10. Sort Files By Newest to Oldest in Linux

You can sort files by time and date using the -t option, which sorts the files in order starting from the newest to the oldest.

$ ls -tl

11. Sort Files by File Size in Linux

By utilizing the “-lS” option combination, the “ls” command will present the file sizes in descending order, from the largest to the smallest. This allows for a display where the largest files are listed first, making it easier to identify and analyze files based on their size.

$ ls -lS

12. List File Inode Number in Linux

You can display the files and directories’ inode numbers using the -i option as shown.

$ ls -i

13. List Files and Directories Separated by Commas

The “-m” flag is used with the “ls” command to list the directory contents in a comma-separated format. When the “ls” command is executed with the “-m” flag, the files and directories are listed one after the other, with each entry separated by a comma. This format provides a compact representation of the directory contents, making it easier to scan through the file and directory names.

$ ls -m

With the -Q flag, all the directory contents are enclosed by double quotation marks as shown.

$ ls -Q

14. Omit Group Ownership in a Long-List Format

When the “-l” command option is used with the “ls” command, it displays detailed information about files, including both user and group ownership. However, if you prefer to omit the group column from the output, you can use the “-g” option. By passing the “-g” option alongside the “-l” option, the “ls” command will exclude the group column from the output, providing a simplified view that includes only the user ownership information. This can be useful when you don’t require the group ownership details and want a more concise listing.

$ ls -g

15. List Specific File Types or Extensions

To list specific file types or extensions using the “ls” command, you can utilize the wildcard notation (*) followed by the file extension. Here’s an example:

To display all files with a .jpg extension, you can run the following command:

ls *.jpg

The command will list all files in the current directory that have the .jpg extension. This is a handy way to filter and view files of a specific type or extension.

Similarly, to list all PDF files, run the command:

$ ls *.pdf

16. List the UID and GID of Files

To display the UID and GID of files and directories, use the -n option as shown.

$ ls -n

17. Check ls Command Version

If you are a little curious and want to check the version of the ls command, you can do so as follows:

$ ls --version

From the output, you can see that we are running ls version 9.1.

18. Show ls Command Help Page

To obtain a comprehensive list of all the command-line options available for the “ls” program, you can execute the following command:

$ ls --help

Optionally, you can visit the man pages by running:

$ man ls

19. List Directory Information in Linux

When you run the command “ls -l /tmp”, it will list the files and directories under the “/tmp” directory in a long listing format. This will include detailed information about each file or directory, such as file permissions, owner, size, and modification date.

On the other hand, when you use the “-ld” options with the “ls” command, as in “ls -ld /tmp”, it will display information specifically about the “/tmp” directory itself rather than its contents. This includes details about the directory itself, such as its permissions, owner, size (usually displayed as 4.0K for directories), and modification date.

In summary, “ls -l /tmp” lists the files and directories inside the “/tmp” directory, while “ls -ld /tmp” provides information about the “/tmp” directory itself.

$ ls -l /tmp
$ ls -ld /tmp/

20. Create ls Command Aliase

If you have created an alias for the “ls” command to always include the “-l” option by default and display a long listing, it means that whenever you execute the “ls” command, it will automatically incorporate the “-l” option and provide a long listing format.

To set up such an alias, you can add the following line to your shell configuration file (e.g., ~/.bashrc for Bash):

alias ls="ls -l"

After adding this alias, whenever you run the “ls” command, it will be equivalent to executing “ls -l” and will display the directory contents in a long listing format by default. This alias saves you from manually typing the “-l” option each time you want to see the long listing output.

To view a number of aliases available in your system, use the below alias command and the same can be unalias as shown below example.

$ alias

To remove an alias previously defined, just use the unalias command.

$ unalias ls

In this guide, we have provided an overview of how to utilize the “ls” command to observe the contents of a folder or directory. Furthermore, we delved into the exploration of various command options that can be used alongside the “ls” command in Linux. By leveraging these options, you can customize the output and obtain more detailed information about files and directories. The “ls” command is a versatile tool for navigating and examining directory contents in the Linux environment.

Comments to: 20 Basic ‘ls’ Command Examples in Linux

    Your email address will not be published. Required fields are marked *