Friday, August 5, 2022

10 Examples of ls command in Linux

Hey there, if you are wondering how to check what inside a directory or just wondering what is ls command in Linux and how to use it to list files then you have come to the right place. Earlier, I have shared examples of df command, du command, find command, grep command, tar command and chmod command and in  today’s article we shall be looking at 10 different examples of ls command in Linux. But before then you would need the meaning of ls and what it is being used for. The goal of this article is to get you comfortable with different things you could do with the ls command and this articles would show you in details how to use it, when to use it and it’s benefits. Let’s go


10 ways to use ls command in Linux

Now we are doing to see different ways to use ls command in Linux and how different options of ls command can be a great tool for any developer or system administrator.  Firstly what does ls means?, it stands for list directory contents and it is used to list all the files and directories in Linux.

In the first example, I would be showing you how to use ls and what it is being used for. This is the very ls command itself in it’s basics. There are of course so many things you could use or do with the ls command. But let see it in it’s simplest form first.

1. “ls”

If you type the above command exactly as it is with no other argument, it is going to give you the list of files and directories. But, it won’t provide any details for those files and directories. You use this when you want to have a view of your files and directories and it could even be that there is a particular folder you need to access but you can’t remember the name and you want to save yourself the stress of having to go through the file names one by one. By using the ‘ls’ command you are going to have a view of all your files and directories.


2. ’ls -a’

Now the command here has an option -a with it, when you use that it gives you the files and directories including the hidden ones. This command is used when you  want to see your hidden files and directories.

3. ’ls -l’

Using the command ’ls-l’ displays the list of the files and directories in such a format that gives you the date created, group, size, owner, permission etc. This is simply called long listing format. It displays with those options. When do you use this? You use this when you need to go a little bit further in the things associated with a particular file. 


4. ’ls *’

This is another cool one that gives the list of files or directories in a particular directory you are in. For instance let us say I have a folder with the name “school” and inside the school folder, I have different files like, students, teachers, admins etc. If I navigate to school at the moment and I use the command, It is going to give me everything present in the school directory.



5. ‘ls -lh’

The command “ls -lh” displays file size in a human-readable format. h stands for human-readable form. That is: M as Mb (megabyte), K as Kb (kilobyte) and G as Gb (gigabyte)

6. ‘ls -ld’

There could be an instance whereby you want to display an information about a particular directory (I.e folder), not with files now but only the directories alone. The command aboves does that. 

7. ‘ls -lt’

To put files in order according to the last time modified, the command above is used. By so doing it sorts the file displayed in the order of the previous time modified. You may want to have the files you have previously worked on sorted. And it would be sorted by the time it was modified

8. ‘ls -li’

The command ‘ls-li’ list the index number of each file. You may need to get the index number of a particular file. When you use the command it gives you the index numbers of all the files present in the directory you are in.

9. ‘ls -lrtS’

Assuming you need to list out the contents in a file in the order of their sizes. This command is used and it sorts a file’s content according to their  sizes.

10. ‘ls -i’

To display the Inode number of a file,the command above is used. This is very important in managing and maintaining your files.

An inode in Linux is an operating system which has important information that deals with files in a file system. In short inode is an index node. It is very unique because it holds specific piece of information on a given file system.


Conclusion

I hope by now You have a good understanding of ls, the meaning, what it is used for and why it is being used. You were made to understand that “ls” command can be used in it’s simplest form with no argument it would only list the files and folder. And there are many things you could do with it which works too.

Depending on what you need to do here are the few things you could so with ls command: Can be used to fetch out the hidden files and directory with the option -a, you could do long listing format with the option -l, you could visualize the files in a directory with the asterisk option *, you could display the size of a file in human readable format with the option -lh, you could sort files in the order of the last modified time with the option -lt, you may decide to find the index with -li, and there could be instances whereby you need to display the inode of a file with the option -i and many more.

 

No comments:

Post a Comment

Feel free to comment, ask questions if you have any doubt.