Introduction to ls command in linux

ls command in Linux is to list down all the contents in a specified or otherwise in the current directory. It is a default part of core utilities of GNU Linux, There are various options while using the ls command, and each option has its own output.

How to use the ls command?

$ [options] [file|directory]

How to get help to use ls command

ls --help

How to check the version of ls command

ls --version

Various options to use ls command

List of contents in directory / ls

Ls command in linux without any option will show the content of the target directory, but only the names of files and subdirectories.

Terminal$ ls
Applications			Movies				eclipse-workspace		temp
Desktop				Music				intermediate.crt		testconnection

List of content in directory in long listing format / -l option

The -l option is here to show detailed information about the directory content. When long listing option is used, command output will be like

Terminal$ ls -l
-rwx---r-x    29  user2  group2      928  Oct 24   23:36 sample.txt

As we can see in output, It contains the following information about each file or directory:

  1. The file type
  2. The file permissions
  3. Number of hard links to the file
  4. The file owner
  5. The file Owner group
  6. The file size
  7. The file modification date and time
  8. The file name

Let’s discuss these file types one by one:

1.The file type

The first character in the output of ls command long listing format, which is ‘-’ in our output, shows the type of file. And here is the possible file types

  • Symbol ‘-’  for Regular file
  • Symbol ‘b’ for Block special file
  • Symbol ‘c’ for Character special file
  • Symbol ‘d’ for Directory
  • Symbol ‘l’  for Symbolic link
  • Symbol ‘n’ for Network file
  • Symbol ‘p’ FIFO
  • Symbol ‘s’ Socket

2.The file permissions

The next 9 characters represent the file permissions. Actually the 9 characters are 3 groups with 3 characters in each.

  • First 3 characters represent permissions for file owner.
  • First 3 characters represent permissions for file owner’s group members.
  • First 3 characters represent permissions for everyone else.

And each permission group has 3 characters with following possibilities:

  1. r   – represents having a read permission.
  2. w – represents having a write permission.
  3. x  – represents having an execute permission.
  4. s  – represents setgid bit 
  5. t   – represents sticky bit

For read further detail to understand the permissions read linux file permissions.

3.Number of hard links to the file

It shows how many hard links exist for this file, including the original file. So if a file has another link with the same file Inode, this count will become 2. For further detail to understand the hard links read Number of hard links.

4. & 5. The file owner and the owner’s group

As the title shows that these are the names of file owner and the group from which the owner belongs to.

6.The file size

It gives the size of the file but bytes, which is not very much understandable or human reading friendly. There is an option in ls command to show the size in more human friendly units like MB, GB, TB e.t.c.

7.The file modification date and time

It shows the last modification date and time for recently changed files and date only for files which has old modification dates.

8.The file name

It shows the file name in utf8 format.

 

How to show hidden files / –all, -a option

In Linux, Unix and all of their distributions a hidden file is denoted as a dot(.) file. That means if a file starts with character ‘.’. it will be hidden on normal user interface, whether it is on graphical Interface or on command line interface using command ls.

ls command by default shows non-hidden files only. So to list down hidden files too, ls command has an option –all or in short -a. As the option is self explanatory, it will show all the files and directories, whether they have plain name or dot(.) file i.e hidden.

Terminal$ ls -l -a
-rwx---r-x    29  user2  group2      9283080  Oct 24   23:36 sample.txt
-rwx---r-x    29  user2  group2      6808000  Oct 24   23:36 .sample-hidden.txt

Print the file size in units other than bytes 

As we can see that file size shown in the output is in byte units by default, which is feasible in case, if the file size is not big, like 100 bytes or 1000 bytes. But having files with huge size is very common these days. Like photos captured from a phone have sizes more than 1MB.

Trying to read those file sizes in bytes doesn’t seem like a good idea.

To enforce the ls command to show sizes in any big unit, there is an option –block-size. This option takes Unit declaration’s first letter as an input and force the ls command to show the files sizes in that unit. e.g

Terminal$ ls - l --block-size=M
-rwx---r-x    29  user2  group2      9MB  Oct 24   23:36 sample.txt
-rwx---r-x    29  user2  group2      6MB  Oct 24   23:36 .sample-hidden.txt

Possible sizes units are here:

  • K (Kilo bytes)
  • M (Mega bytes)
  • G (Giga bytes)
  • T (Tera bytes)
  • P (Peta bytes)
  • E (Exa bytes)
  • Z (Zetta bytes)
  • Y (Yotta bytes)

List of files with human readable format / -h option

As we know that the size of file shown in the output of long listing format is not very much readable or easily understandable, because the file size shown is in byte units by default, and to solve this problem we have an option to use –block-size. But this option has a drawback. That is, it can show sizes only in single unit, whether it is small or big. That means if a directory has files with sizes in single unit like KBs, MBs or GBs, that option will be good but when files list contains different sizes like some files in KBs and some in MBs, –block-size option will round off the size into the given unit. E.g.

If we force ls command to show file sizes in unit MB, Files with sizes 400 bytes and 1000 bytes both will be shown as 1MB. So to avoid this conflict and to make the output more readable for humans, ls command has another option, which is intelligent enough to understand the unit of file size and show the size in correct unit.

Terminal$ ls -l -h
-rw-r--r--   1 user1  group1  2GB Jul 21  2019 Sample1-file.zip
-rw-r--r--   1 user1  group1  22MB Jul 21  2019 Sample2-file.zip
-rw-r--r--   1 user1  group1  11KB Jul 21  2019 Sample3-file.zip
-rw-r--r--   1 user1  group1  110B Jul 21  2019 Sample4-file.zip

List of files in directory recursively -R

Ls command is to show the content of a directory. But if the requirement is to show all the contents of the directory and as well as the contents of all the subdirectories too, ls command has an option -R for this purpose. 

Terminal$ ls -lR
drwxr-xr-x  3 user2 group2    96 Oct 29 00:47 directory1
drwxr-xr-x  3 user2 group2    96 Oct 29 00:47 directory2
./directory1:
[email protected] 1 user2  group2 646 Jun 7 12:21 sample1-file.txt
./directory2:
[email protected] 1 user2  group2 646 Jun 7 12:21 sample2-file.txt

 

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.