ls command in Linux

ls command stands for listing the details of the directory for the particular user. This command is used frequently while working in Linux. In this article, we will learn about the usage of ls command and the various options that can be used with the ls command.

ls command

I have created a local user named tharani and I have given the below command,

ls

ls-linux

ls / command

By using the below command you can list the details of the parent directory[root] "/"

ls /

ls-command-linux

ls command with options

ls -l command

By using the below command, you can get the details of the file/directory of the specific user such as permissions, the last modified date and time, the owner of the file/directory.

ls -l

ls-l-command-linux

In the above image, there are 6 columns. Let us learn what each column represents.

  • First Column − It gives the detail about the file type and the permission given on the file. The different types of file types are,
    • -: Regular files, such as an ASCII text file, binary executable, or hard link.
    • b: Block special file. Block input/output device file such as a physical hard drive.
    • c: Character special file. Raw input/output device file such as a physical hard drive.
    • d: Directory file that contains a listing of other files and directories.
    • l: Symbolic link file. Links on any regular file.
    • p: Piped file. A mechanism for interprocess communications.
    • s: Socket used for interprocess communication.
  • Second Column −It gives the detail about the number of memory blocks taken by the file or directory
  • Third Column −It gives the detail about the owner of the file
  • Fourth Column − It gives the detail about the group of the owner
  • Fifth Column − It gives the detail about the file size in bytes
  • Sixth Column − It gives the detail about the month, date, and time when this file was created or modified for the last time
  • Seventh Column − It gives the detail about the file or the directory name
ls -a command

By using the below command, you can get the details of the file/directory including the details of the hidden files/directories.

ls -a

ls-hidden-files-directory-linux

ls -lh command

By using the below command you can get the details of the files/directories in the human-readable format.

The main difference between the ls -l and ls -lh is, in ls -l the size of the file/directory is displayed in terms of bytes and in ls -lh the size of the file/directory is displayed in terms of KB.

ls -lh

ls-humanreadableformat-linux

ls -F command

By using the below command, you can differentiate the directories and the files. For directories at the end of the folder name "/" is there and for files "/" is not there. In this way, you can differentiate the files and folders.

ls -F command

ls-differentiates-directory-linux

ls -al command

Compared to the ls command, ls -al command gives more detail about the files and directories including the hidden files.

ls -al /

ls-command-hidden-lists-linux

ls -li
command

This command shows the inode number for each file/directory. In the below command the files and directories under the parent folder are listed

ls -li /

list-inode-linux

ls -ltr
command

By using this command the newly edited directory/file will display at last in the list.

I have created a file named f1 in the parent directory by providing the below command

sudo touch /f1

The termsudo is present in the command because in Ubuntu we cannot log in to a root account. So to create a folder in the root directory(/), we have to provide sudo before any command. Technically to provide the local user admin privileges the term sudo is used before any command.
sudo-administrator-linux

After that when you give the command ls -ltr / you can see that the file f1 is last on the list.
ls-ltr-linux

ls -n command

By using the below command you can get the userID and groupID of the particular file.

ls -n

ls-n-command-linux

ls -r command

By using the below command you can print the list of directories/files in reverse order.

ls -r /

listing-reverse-linux

ls -R

This command is used to list the contents of the directory as well as subdirectories.

ls -R /

recurrsive-listing-linux

ls -lS command

To display the files/directories in descending order with respect to the size, the below command is used,

ls -lS /

listing-files-decending-order-linux

ls -i command

To display the inode number of the files/folders in a directory the below command is used,

ls -i /

listing-inode-linux

ls --version
command

The below command gives the version of the ls command.

ls --version

list-version-linux

ls --help
command

The below command lists all the available options which can be used with the ls command.

ls --help

help-list-linux

ls command and its Aliases

We can redefine the ls commands with the help of the alias.

For example, when you execute the command ls, you will get the output as in the below image.
ls-alias-command-linux

We can redefine the above command in such a way that if I execute the ls command it should list the file/directory from the parent directory(/). It can be achieved with the help of the below command,

alias ls="ls -l /"

To check whether the alias is created or not you can give the below command in which lists the available aliases.

alias

Now when you execute the command ls it lists the files/folders in the parent directory(/).

ls

To remove the alias (i.e) to change the function of the ls command back to normal the below command is used,

unalias ls

alias-ls-command-linux

ls -l --block-size=[SIZE]
command

This command is used to display the files in a specific size format.

You can replace [SIZE] with the following format :

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

For example, to display the file size in the MB format the command is

ls -l / --block-size=M

list-files-specific-size-format-linux

ls -d */
command

This command is used to display only subdirectories under the parent directory.

ls -d */

listing-subdirectories-linux

ls -g or ls -lG
command

By using the below command, you can exclude a column of group information.

ls -g / [OR] 
ls -lG /

list-without-groupowner-linux

ls -lX command

By using this command you can group and list the files with the same extensions.

ls -lX command

grouping-listing-files-linux

There are many other options that you can use along with the ls command. To view, the list of the available options give the below command.

man ls

After executing the above command the below UI will appear where you can find the list of available options.
manual-page-ls-linux

dir command in Linux

The dir command is also like the ls command. dir command lists the details of the directories present in a path. dir command is not much used in Linux instead of the that ls command is mostly used.

The syntax of the dir command is,

dir [OPTION] [FILE]
dir /
command

This command lists the folders in the parent directory.

dir /

dir-command-linux

dir /etc
command

This command lists the folders in the /etc directory.

dir /etc

dir-command1-linux

dir -1
command

This command lists the folders in the parent directory one by one line.

dir -1 /

dir-1-command-linux

dir -a
command

This command lists the folders as well as hidden folders in the parent directory.

dir -a /

dir-a-command-linux

dir -al
command

This command lists the folders as well as hidden folders in the parent directory in a detailed manner.

dir -al /

dir-al-command-linux

dir -d
command

This command lists the directory entries instead of directory contents.

dir -d /etc

dir-d-command-linux

dir -dl
command

This command lists the directory entries instead of directory contents in a detailed manner.

dir -dl /etc

dir-dl-command-linux

dir -il
command

This command lists the directory details along with the inode number.

dir -il /

dir-il-command-linux

dir -shl
command

To list the directories along with the size of the directory and in the human-readable format s and h are used in the command. To sort the details you can use uppercase "S" along with the other options.

dir -shl /

dir-shl-command-linux

dir -ashlS
command

To list the directories along with the size of the directory(s), in the human-readable format (h), to list the hidden directories (a), and to sort the folders based on the size (S) this command is used.

dir -ashlS /

dir-ashls-command-linux

dir -ashlt
command

To list the directories along with the size of the directory(s), in the human-readable format (h), to list the hidden directories (a), and to sort the folders based on the last modification (t) this command is used.

dir -ashlt /

dir-ashlt-command-linux

dir -ahgG
command

To list the directories in the human-readable format (h), to list the hidden directories (a), and to avoid the owner specification(g), to avoid the group owner specification(G) this command is used.

dir -ahgG /

dir-ahgg-command-linux

dir -l --group-directories-first
command

To list the directories first and after that to list the files this command is used.

dir -l --group-directories-first /

dir-l-command-linux

dir -R
command

To list the Sub-directories of all the directories recursively this command is used.

dir -R /

dir-r-command-linux

dir -al --author
command

To list the directories with the author name of the directory this command is used.

dir -al --author /

dir-al-author-command-linux

dir -nl --author
command

To list the directories with the group IDs and user IDs this command is used.

dir -nl --author /

dir-nl-command-linux

dir -am
command

To list the directories separated by comma this command is used.

dir -am /

dir-am-command-linux

0 results
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions