The ls Command lists the attributes of files and directors. Ls command in Linux used for a basic file orations. Ls is the short form of list, As it name suggests it is use to list the files and directories inside a working directory.
In this article you will learn almost every possible use of ls command in Linux.
This post not only for those who don’t know about ls comment but it can help those people also who already know basics of ls command because we have covered almost every use use the command with options.
Manipulating files is one of the first things that a Linux user should know or I can say ‘Must Know’ and In order to manipulate files you should know how to list files.
How to list files using ls command?
Using following ls command syntax you can easily list the files in any Linux distro no matter what Linux OS you are using.
$ ls [options]
You must be thinking…
What is use of options here?
Well, we are not just going to explain what is the use of options with ls command but also you are going to explain what are the options that could be used with ls command.
Example of ls [option]-
ls -l is the most popular example of an option with ls command.
So what is the deference between ls and ls -l command?
You may think if you can list the files using ls command only then why do we need to run ls -l.
Well, when you run Ls command you will get the list of attributes in directory like this-
But when you run ls -l command in your Linux machine will produce long listing like this-
Have you got confused with the extra information?
drwxr-xr-x 5 factforgeeks factforgeeks 4096 Mar 21 19:19 Desktop drwxr-xr-x 3 factforgeeks factforgeeks 4096 Feb 5 15:25 Documents drwxr-xr-x 3 factforgeeks factforgeeks 4096 Mar 30 19:07 Downloads -rw-rw-r-- 1 factforgeeks factforgeeks 30 Feb 3 17:47 hello drwxr-xr-x 2 factforgeeks factforgeeks 4096 Apr 1 2020 Music drwxr-xr-x 3 factforgeeks factforgeeks 4096 Mar 30 19:06 Pictures drwxr-xr-x 6 factforgeeks factforgeeks 4096 Feb 2 22:13 Public drwxr-xr-x 4 factforgeeks factforgeeks 4096 Oct 25 07:25 snap drwxr-xr-x 2 factforgeeks factforgeeks 4096 Apr 1 2020 Templates drwxr-xr-x 2 factforgeeks factforgeeks 4096 Apr 1 2020 Videos
Well let me explain what are these codes used for-
We are going to explain the first attribute of the directory – drwxr-xr-x 5 factforgeeks factforgeeks 4096 Mar 21 19:19 Desktop
- drwxr-xr-x – This code tells us about file permissions.
- 5 – This indicates number of hard links.
- factforgeeks – Owner of the computer.
- factforgeeks – Group (User), In my case computer name and group name is same that’s why it is showing factforgeeks for two times.
- 4096 – It is the size of the file in byte.
- Mar 21 19:19 – The Time when file or directory was created.
- Desktop – The name of directory or file.
Listing files of any other Directory using ls command in Linux
You can also check the files inside any other directory using ls Command. Suppose, you have downloaded some files into your downloads directory then guess how will you check the download files?
Well, it is so simple; you will have to run the following command-
$ ls Downloads
Isn’t it so simple?
You should also try this command now.
Listing files in root directory using ls command
The root directory in any Linux machine is the most important directory it contains all the files that are important to our Linux operating System.
What if you want to list the Directory and files that are inside the root? Well run the following command-
Listing Only Directory Using ls Command
When you run ls command it shows the list of files and directories both mixed together. But what if you want to see the list of directories only but not files?
Well, these is a solution for that also.
In order to list directories only by excluding files you will have to use -d option */ special characters.
$ ls -d */
Why does -d option is used?
The -d option is used when you want get the list of directories but don’t want to get content inside the directories.
Have any confusion?
It will be clear after these examples-
Files and directories inside the Home directory of my Linux Machine-
I run ls command to see the files and directories in the Home Directory of my machine.
Now I am going to run $ ls -d */ command to get list of directories without files.
But happens if I remove -d option from the command?
The list of directories will be shown with contents inside it. Let’s see an example for it.-
Now suppose, you don’t want to get the content inside the directory but you want to get the get list of directories with it’s sub directories-
You will have to run ls * command to get the list of directories and sub directories.
Other Useful Options That are used with ls command
-a : -a option is used for listing all the files and directories including those whose name starts with a dot (.) also.
-l : -l option is used for listing files and directories with attributes. you can place -h after -l option to print file and directory size in KB, MB, or GB Instead of Bytes.
-G : Place this option of you don’t want to print the group ownership of the file.
-S : -S option is for shoring the files and directories by their size.
– t : It is an useful option if you want to short the files according to their last modification time.
-r : It is used for reversing the shorted order.
This is all about ls Command in Linux With Examples. ls command is one of the most common commands in Linux. The command is used for listing the files and directories.
ls command is used with various options like ls -l, ls -R, ls -a and ls -t. Its depend on the user how they want to use it.
In this post we tried to cover all of the uses of ls command. But if you feel anything lest then please let us know using the comment box.
Also if you have any confusion about ls command in Linux then feel free to ask questions.