ls

ls. ls with -l, with -r, with -t. If you are working in corporate and somehow are part of a Unix project, well the command that you are going to encounter most is ls. And the most common options used with ls incorporate is the -lrt. However, there are very few who know exactly what and why they are doing with ls (ls, after all, being the easiest). So, this time we are going to dissect down the ls command and its features.

Now, below is the structure that we have –

1.png

As it is clear, there is a directory named ls with three files and a directory. Within the directory ls1, there are further many files.

ls -r or ls -R

When someone asks you to hit the command, ls -r/R, it is important for you to ask the question, “what exactly they need to see?”. In case, they want to see the entire list of directories and the files within the directory, it is ls -R that you need to type in. However, in case, you need to print the files in the reverse order of their existence( by default, ls shows the files present in the alphabetical order), you need to do ls -r.

Let us see how the results differ –

im.png

2.png

Observe carefully and you will be able to notice the difference. In case of ls -R, it not only lists the files present in the current directory but also lists down the files present in the directories within it. So, when it is listing the files of the current directory, you notice .: .Here, the . represents the current directory. And then, there is this ./ls1 which shows that it is displaying the files present in the directory ls1 which is present in the current directory.

However, in the case of ls -r, it is simply showing the files in the reverse order of their alphabets ( since there is no other option available). You can see the difference between the order of displaying files when compared with ls -R.

ls – l or ls -L

The next thing that comes into the picture is the -l/L option. What exactly is the difference between the two?

To understand the difference, it is important for you to understand the concept of links. Well, in case you are familiar with it great, if not watch out this space, and we will post about it soon!!

The result of ls – l

3.png

The -l option displays the entire list of the files present in the current directory that you fire the command in. Also, it not only lists the files but it gives the entire description of the files/ directories/links present. Now, when you look closely at the results of the command, you shall notice that there is an arrow made there, with mylink pointing to file1. This means that there is link present in the directory that is pointing to the file1. So, when you do ls -l it shows the information about the link in relation to the file.

Delving deeper –

What is the meaning of these different columns that ls -l shows?

In the order of their display, the columns mean the following –

  • file permissions
  • number of links
  • owner name
  • owner group
  • file size
  • time of last modification
  • file/directory name

Results of ls -L

5.png

In case of ls -L, observe carefully to notice that there is no color change in mylink. So, the link is not shown as a different component i.e. it shows the file the link refers to and not the link itself.  When showing file information for a symbolic link it shows information for the file the link references rather than for the link itself.

Well, don’t confuse this with the simple ls command. The results of ls are –

6.png

Notice, the color difference between mylink in both ls and ls -L.

ls -t or -T

Well, ls -t –> it sorts the file on the basis of modification time.

Note -: Modification time is different from the creation time. So, even if a file is created a year ago and modified today, it will be displayed above the file that was created yesterday. Because the former file got modified today.

8.png

Notice that ls -t shows the files present in the order of the modification time in descending order. So, the file that was modified the earliest would come first. Also, note that we have used ls -l option along with it because otherwise, you would not have noticed the time.

ls – T

This is quite an amazing option. Although, it has nothing to do with what gets displayed but surely has so much to with how it gets displayed.

man page: assume tab stops at each COLS instead of 8

Look carefully to notice the difference and understand what the -T option does –

9.png

Here, there is no space between the different text files. So, you observe the default space that is decided by Linux in between the files.

10.png

Here, observe that there is a tab space after every three files. So, Linux is actually trying to put three files together and then given a tab space and further put the three files together and so on.

12.png

Observe here, that Linux has put five files together and there is a tab space between after which all the other files have the default gap between them.

13.png

But, nowhere since the tab space provided is 342, so it tries to put the 342 files together and then give a gap. This obviously is not possible since there are only 9 files.

We hope, that you have understood the three most common options being used with ls. In case of doubts, comment below.

The Tech World



Leave a comment