ls Command in Linux / Unix with Example

Linux ls Command

ls command is one of the most frequently used commands in Linux. I believe ls command is the first command you may use when you get into the command prompt of Linux Box.

We use ls command daily basis and frequently even though we may not aware and never use all the ls options available. In this article, we’ll be discussing basic ls command where we have tried to cover as many parameters as possible.

Linux ls command options

ls optionDescription
ls -aIn Linux, hidden files start with dot(.) symbol and they are not visible in the regular directory. The (ls -a) command will enlist the whole list of the current directory including the hidden files.
ls -lIt will show the list in a long list format.
ls -lhThis command will show you the file sizes in a human-readable format. The size of the file is very difficult to read when displayed in terms of the byte. The (ls -lh)command will give you the data in terms of Mb, Gb, Tb, etc.
ls -lhSIf you want to display your files in descending order (highest at the top) according to their size, then you can use (ls -lhS) command.
ls -l – -block-size=[SIZE]It is used to display the files in a specific size format. Here, in [SIZE] you can assign size according to your requirement.
ls -d */It is used to display only subdirectories.
ls -g or ls -lgWith this, you can exclude the column of group information and owner.
ls -nIt is used to print group ID and owner ID instead of their names.
ls –color=[VALUE]This command is used to print the list as colored or discolored.
ls -liThis command prints the index number if the file is in the first column.
ls -pIt is used to identify the directory easily by marking the directories with a slash (/) line sign.
ls -rIt is used to print the list in reverse order.
ls -RIt will display the content of the sub-directories also.
ls -lXIt will group the files with the same extensions together on the list.
ls -ltIt will sort the list by displaying a recently modified filed at the top.
ls ~It gives the contents of the home directory.
ls ../It gives the contents of the parent directory.
ls –versionIt checks the version of ls command.

Linux ls -a command

It will give you the whole list of a directory including the hidden files also. In Linux, hidden files start with a dot (.) and can’t be seen in the regular directory.

$ ls -a
.              .bashrc    Downloads  Pictures         snap
..             .cache     .gnupg     .pki             .sudo_as_admin_successful
.anydesk       .config    .local     .profile         Templates
.bash_history  Desktop    .mozilla   Public           Videos
.bash_logout   Documents  Music      .python_history  .vscode

In the above example, you can see the whole list of files, including the hidden files.

Linux ls -l command

The ls command will only display the files. But if you want your files to be displayed in a long list format, then you can use ls -l command.

$ ls -l  
total 36
drwxr-xr-x 2 chetan chetan 4096 Oct 19 23:50 Desktop
drwxr-xr-x 2 chetan chetan 4096 Oct 19 23:50 Documents
drwxr-xr-x 2 chetan chetan 4096 Oct 20 00:15 Downloads
drwxr-xr-x 2 chetan chetan 4096 Oct 19 23:50 Music
drwxr-xr-x 2 chetan chetan 4096 Oct 20 22:05 Pictures
drwxr-xr-x 2 chetan chetan 4096 Oct 19 23:50 Public
drwxr-xr-x 4 chetan chetan 4096 Oct 20 22:11 snap
drwxr-xr-x 2 chetan chetan 4096 Oct 19 23:50 Templates
drwxr-xr-x 2 chetan chetan 4096 Oct 20 22:48 Videos

Here, as you can see the list is a long list format.

Columns above indicate specific things:

  • Column 1 indicates information regarding file permission.
  • Column 2 indicates the number of links to the file.
  • Column 3 & 4 indicates the owner and group information.
  • Column 5 indicates the size of the file in bytes.
  • Column 6 shows the date and time on which the file was recently modified.
  • Column 7 shows the file or directory name.

Linux ls -l –block-size=[SIZE]

If you want to display the file size of your list in a particular format or size, then you can use this command. Just put the size in place of [SIZE] as per your requirement.

ls -l –block-size=[SIZE]

Syntex
$ ls -l --block-size=M  
total 1M
drwxr-xr-x 2 chetan chetan 1M Oct 19 23:50 Desktop
drwxr-xr-x 2 chetan chetan 1M Oct 19 23:50 Documents
drwxr-xr-x 2 chetan chetan 1M Oct 20 00:15 Downloads
drwxr-xr-x 2 chetan chetan 1M Oct 19 23:50 Music
drwxr-xr-x 2 chetan chetan 1M Oct 20 22:05 Pictures
drwxr-xr-x 2 chetan chetan 1M Oct 19 23:50 Public
drwxr-xr-x 4 chetan chetan 1M Oct 20 22:11 snap
drwxr-xr-x 2 chetan chetan 1M Oct 19 23:50 Templates
drwxr-xr-x 2 chetan chetan 1M Oct 20 22:48 Videos

Here, all file sizes have listed in Megabyte.

You can replace [SIZE] with the following measures:

  • K = Kilobyte
  • M = Megabyte
  • G = Gigabyte
  • T = Terabyte
  • P = Petabyte
  • E = Exabyte
  • Z = Zettabyte
  • Y = Yottabyte

Linux ls -d */

If you only want to display the sub-directories excluding all other files, you can use this command.

$ ls -d */  
Desktop/    Downloads/  Pictures/  snap/       Videos/
Documents/  Music/      Public/    Templates/

The above result only shows sub-directories excluding all the other files.

Linux ls -g

If you don’t want to display the owner’s information on your list, then you can exclude this column with the help of this command.

$ ls -g  
total 36
drwxr-xr-x 2 chetan 4096 Oct 19 23:50 Desktop
drwxr-xr-x 2 chetan 4096 Oct 19 23:50 Documents
drwxr-xr-x 2 chetan 4096 Oct 20 00:15 Downloads
drwxr-xr-x 2 chetan 4096 Oct 19 23:50 Music
drwxr-xr-x 2 chetan 4096 Oct 20 22:05 Pictures
drwxr-xr-x 2 chetan 4096 Oct 19 23:50 Public
drwxr-xr-x 4 chetan 4096 Oct 20 22:11 snap
drwxr-xr-x 2 chetan 4096 Oct 19 23:50 Templates
drwxr-xr-x 2 chetan 4096 Oct 20 22:48 Videos

Here owner column is excluded.

Linux ls -lg

If you don’t want to display the group information in your list then you can exclude this column with the help of this command.

$ ls -lg
total 36
drwxr-xr-x 2 chetan 4096 Oct 19 23:50 Desktop
drwxr-xr-x 2 chetan 4096 Oct 19 23:50 Documents
drwxr-xr-x 2 chetan 4096 Oct 20 00:15 Downloads
drwxr-xr-x 2 chetan 4096 Oct 19 23:50 Music
drwxr-xr-x 2 chetan 4096 Oct 20 22:05 Pictures
drwxr-xr-x 2 chetan 4096 Oct 19 23:50 Public
drwxr-xr-x 4 chetan 4096 Oct 20 22:11 snap
drwxr-xr-x 2 chetan 4096 Oct 19 23:50 Templates
drwxr-xr-x 2 chetan 4096 Oct 20 22:48 Videos

Here group column is excluded.

Linux ls –color=[VALUE]

This command is used to colorize and decolorize the list. If you replace the [VALUE] by ‘auto’, it will display the colored list. But, if you will replace the [VALUE] by ‘never’, it will decolorize the list.

ls -l –color=[VALUE]

Syntex

ls -l –color=never

Example

ls -l –color=auto

Example
$ ls --color=never  
Desktop  Documents  Downloads  Music  Pictures  Public  snap  Templates  Videos

You can easily notice the difference between auto and never command in the above image.

Linux ls ~

Linux ls ~ command shows the contents of the home directory. Let us see the example of ls ~ command.

$ ls ~  
Desktop    Downloads  Pictures  snap       Videos
Documents  Music      Public    Templates

Linux ls ../

This command contains the list of the parent directory.

In the given example, our current directory is Downloads, and by using ls ../ command, we have listed out the content of its parent directory “home directory”.

$ ls ../  
chetan

Where can I learn more about Linux?

THCBin Learning have some Linux courses to learn check out following courses

2 COMMENTS

  1. I got this web site from my pal who shared with me concerning this site
    and at the moment this time I am browsing this site and reading very informative
    articles at this time.

LEAVE A REPLY

Please enter your comment!
Please enter your name here