Some common options you can use with the ls command

As you know, the ls command is used to list all directories and files that are stored on a computer, and it is the most basic command that every Linux user is aware of how to list files on a terminal, but some of them are not using an option that is available for use.

So let me introduce to you some of the most common options which you can use to list files or directories, and not only that, after reading this article you can show your creativity to create your own personalized option as per your needs, which will help you to list files according to your preference.

Well, I’ll try to show you most of the common options that you can use with the ls command, like listing files in reverse order, accessing time, showing only a directory, and many other interesting options, but while writing, if I missed something, then please put that command in the comment section for our readers.

1. Basic and default usage of the ls command

First, let me start with the default usage of the ls command

When you execute the ls command without any options in your terminal window, it will list the available files and directories from the current directory in alphabetical order with a column-wise structure like in the image attached below.

Basic usage of ls command
Basic usage of the ls command

But the problem with the above output is that you are not able to get information about the file, such as when it was modified, who is the owner and group of the following files, and many other important details about the file, which need to be known.

In that case, you should follow the below steps.

List files separated by a comma and double quotes

The above was the default behaviour of the above command. If you want to print the same output in different ways, then you can use two other characters like commas (,) and double quotes (“”).

$ ls -m
$ ls -Q

There are several other quoting styles you can use with the ls command by specifying –quoting-style= along with the various arguments like (‘literal’, ‘shell’, ‘shell-always’, ‘shell-escape’, ‘shell-escape-always’, ‘c’, ‘c-maybe’, ‘escape’, ‘locale’, ‘clocale’).

Don’t worry, I’ll show you how to combine this argument with the ls command.

$ ls --quoting-style=clocale

The behaviour of the above command

Different type of quoting styles
Different type of quoting styles

2. List out file information in detail using the ls -l command

As I said, the above method is not reliable. When you want to list out file information in detail, in that scenario, you should use the below command, which will print out the following details: permission of the file, owner and group information, size of the file, along with the last modified date and time.

$ ls -l

The behaviour of the above command

List file in details command output
List out files in detail, command output

For a better explanation of command output, you can refer to the below image.

List file information in detail using the ls -l command output explanation
Individual Explanation of ls -l command output

3. List out all hidden files using the ls -la command

In your home directory, there are several configuration files that are hidden to prevent any changes from being made by the user unknowingly, but sometimes you need them to make some changes to the configuration file.

But how do I know whether hidden files exist on my system? At that time, you should use -a or -A to list out all the hidden directories and files in the current path.

$ ls -la

The behaviour of the above command

List out all hidden files using the ls -la command
List out all hidden files using the ls -la command

The above command will also include the. (current) and .. (parent) directories, which is unnecessary, and to avoid that, you can use the -A option, which will show only hidden files, not current and parent directory characters.

$ ls -lA

The behaviour of the above command

List out all hidden files using ls -lA

4. List only the Directory information using the ls command

When you just want to get overview information about the directory but not about its inner content, then you should invoke the below command, which will just print the directory information by ignoring its inner content.

$ ls -ld

If you want information about a specific directory, then pass the directory name as an argument.

$ ls -ld /etc

The behaviour of the above command

Show directory information
Show directory information

5. List Subdirectories Recursively using the ls command

From the above step, you have learned how to check information about individual directories, but what if you want to check what all files are stored in all particular subdirectories? At that time, you can take the help of the -R parameter, which will show the inner content of the current and its subdirectories, which will save a few extra keystrokes.

$ ls -R

To check the content files of a specific directory, use the below code:

$ ls -R /etc

The behaviour of the above command

Recursive way to list out files using the ls command
Recursive way to list out files using the ls command

6. Append File Type Indicators using the ls command

There might be a situation where you are not able to identify the type of file, whether it is a directoryregular file, or symbolic file, especially when alias ls=’ls –color=auto’ is missing in your ~/.bashrc file.

So to avoid any confusion, you can use the -F parameter, which will add the file type indicators next to the file name, which will help you identify the type of file.

$ ls -F

From the behaviour of the above command, you will be able to distinguish the directory, regular, and symbolic link files very easily by the following symbols or indicators.

  • Directory (/)
  • Soft link (@)

The behaviour of the above command

Add file type indicators
Add file type indicators

7. List out files in Human Readable Format using the ls command

By default, the ls command lists out files in byte format, which can be difficult to read when the file size is large. To avoid printing files in byte format, for which you can use -h along with the ls -l command.

You can use -h with the ls -l command to avoid printing files in byte format.

$ ls -lh

The behaviour of the above command

Show listed file information size in human-readable format
Show listed file information size in human-readable format

8. List out files with their Inode numbers using the ls command

All the meta-information about a file is stored in the inode number, which is also known as the index node. To get more information about them, you can refer to this complete guide on Inode numbers in Linux with an example.

To find out the inode number, you can use the -i option like in the below code snippet:

$ ls -li

The behaviour of the above command

Show Inode number information using ls -li command
Show Inode number information using ls -li command

9. List out files in Reverse Order by using the ls command

When you want to list out files in descending or reverse order, then you should use the -r option, which will print out the file name in reverse order wherever you execute the below command.

$ ls -lr

The behaviour of the above command

List out files in reverse order by using the ls command
List out files in reverse order by using the ls command

10. List out files in various Time Orders using the ls command

Sometimes you want to list out files in various time orders, like when someone has accessed or modified a file, or sort them out on the basis of creation time. Then you should use the –time= option which accepts several arguments like (‘atime’, ‘access, ‘use’, ‘ctime’, ‘status’, ‘birth’, ‘creation’).

If you want to list out files on the basis of newly created files, then you should use the below code:

$ ls --time=creation

When you want to list out files on the basis of ctime (change time) or modification time, then you can use the below code:

$ ls --time=ctime

Instead of that, you can use the below command for the same task.

$ ls -c
or
$ ls -lt

The behaviour of the above command

List out file on basis of change time(ctime)
List out files on basis of change time(ctime)

That’s all for now!

Leave a Reply