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.
Table of Contents
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.
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
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
For a better explanation of command output, you can refer to the below image.
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
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
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
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
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 directory, regular 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
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
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
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
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
That’s all for now!
A man with a tech effusive who has explored some of the amazing technology stuff and is exploring more. While moving towards, I had a chance to work on Android development, Linux, AWS, and DevOps with several open-source tools.