Want to lift files in Linux? All you have to do is execute the ls command in the terminal as shown:
But you can do much more with the ls command and fine-tune the output per your workflow demands.
Such as you can list files with file permissions, list symbolic links, and more.
Want to know how? here you have it!
How to list files using the ls command
Before I walk you through the how-to part, let's have a look at the syntax of the ls command which eventually help you understand how you can use the ls command:
ls [options] [path/to/directory]
Let's break this syntax for better understanding:
[options]: Used to fine-tune the default behavior of the ls command.
[path/to/directory]: This is where you specify the path to a directory in which you want to list files.
Here, the both
[options] and the
[path/to/directory] options are options and if you use the ls command without any, it will list the file of the current directory (as shown at the beginning of this guide).
Now, let's take a look at some examples of how you can list the files using the ls command.
1. List files in long format (shows file permission)
By far this is the most popular implementation of the ls command where you use it with the
-l flag to list files in the long format which shows the file permissions and ownership:
ls -l [path/to/directory]
For example, here, I listed files in the long format of the current directory:
Looks confusing? Here's a simple explanation of what the output of the
ls -l command means:
Still confused? Refer to our detailed guide on checking the file permissions with the ls command:
2. List hidden files using the ls command
To list hidden files using the ls command, you'd have to use the
-a flag and it will list the filenames starting with the
ls -a [/path/to/directory]
For example, here, I wanted to list the hidden files located inside of the
test directory, so I used the ls command in the following manner:
ls -a ~/test
In the above image, you'll see 3 filenames starting with the
.vimrc which are hidden files.
But the best way to use the
-a flag (in my opinion) is to pair it with the
ls -la [path/to/directory]
3. Get output in human-readable form
If you use the
ls -l command, it will show the filesize in the bytes which is not the best way to read the file size, and in that case, you can use the
-h flag as shown:
ls -lh [path/to/directory]
4. List files recursively
Listing files recursively means listing files that are present inside of the sub-directory.
To list files recursively, all you have to do is add the
-R flag with the ls command:
ls -R [path/to/directory]
Let's say I want to list files recursively of the
test directory, then I will be using the following command:
ls -lhR ~/test
5. List only a specific type of files
There are times when you only want to work with specific file types and in that case, you can instruct the ls command to list specific file types using its file extension:
For example, if I want to list only the text files, then, I will be using the following:
Pretty cool. Right?
More on the ls command
The best way to learn any command is to start with the practical life scenario and for that case, we made a detailed guide covering practical examples of the ls command:
Here's how to sort files by date and time using the ls command:
I hope you will find this guide helpful.