How to use ls Command in Linux (List Files and Directories)

How to use ls Command in Linux

In this article we are going to shows how to use ls command in Linux. If you want to learn about ls command in Linux with Examples then this post is ideal for you.

The ls command is a Linux shell command that is used to list files or directories and shows detailed information about them in Linux and other Unix-based operating systems.

By default, when run the ls command without any arguments, it will print a list of all files or directories in the current working directory. It is one of the basic and most used command that we use in Linux system.


How to Use the ls Command in Linux

In the Linux, the basic syntax of the ls command is show as below:

ls [OPTIONS] [FILES]

When you run the ls command without any arguments or options, It will display a list of all files or directories in the current working directory.

$ ls

By default, the ls command display list of all files or directories in alphabetical order.

If you want to list the files of a specific directory, use the directory path as an argument with ls command as show below:

$ ls /studiesonline

Ls command also allows you to use multiple directories and files as arguments in single command:

$ ls /studiesonline /var /etc

When you run the ls command and you doesn’t have read permissions to the directory, it will show the error message as show below:

$ ls /root
Output
ls: cannot open directory '/root': Permission denied

Long Listing Format (Display All Information About Files/Directories)

By default, the ls command output display only the names of the files and directories. If you want to get more information then use -l option with the ls command to display its output in long listing format

When you use -l option (long listing format), you will see the below file information:

  • File type.
  • File permissions.
  • Number of hard links to the file.
  • File owner.
  • The file group.
  • File size.
  • Date and Time.
  • File name.
$ ls -l /studiesonline
Output
-rw-r--r-- 1 root root 586 Feb 25 2021 /studiesonline

In the above example:

  • First character () – It shows the file Type.
  • File Permissions (rw-r--r--)  – It is indicates that the user can read and write the file, and the group and others can only read the file.
  • Number of links (1)  – It is shows the number of hard links to this file.
  • Owner and group (root root ) -These two fields specifies the owner and the group of the file.
  • Size (586) – It is specifies the size of file in bytes.
  • Last modified date and timeFeb 25 2021 ) – This field specifies the last modification date and time of the file
  • File name (studiesonline) – The last field shows name of the file.

Show Hidden Files With ls Command

By default, the ls command is do not show the hidden files. A hidden file is begins with a dot (.).

You can display all files including hidden files using -a option with the ls command:

$ ls -la ~/

Sorting the ls Command Output

By default, the ls command display list of all files or directories in alphabetical order. With help of --sort option, you can sort the ls command output by extension, time, size and version.

Use below --sort options to sort ls command output:

  • -X (or --sort=extension ) – It is sort alphabetically by extension.
  • -S (or --sort=size)  – sort by file size.
  • -t (or --sort=time) – It is sort by modification time.
  • -v (or --sort=version) – Natural sort of version numbers.

You can also sort the output in reverse order with the help of -r option.

For an example, if you want sort the files of /studiesonline directory by file sizes in reverse order, run the command below:

$ ls -lSr /studiesonline

List Subdirectories Recursively With ls Command

If you want to display the contents of the subdirectories recursively, use -R option with the ls command as show below:

$ ls -R

That’s all.

If you face any error and issue in above steps , please use comment box below to report.

If our tutorials helped you, please consider buying us a coffee. We appreciate your support!

Thank you for your support.

Leave a Comment

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.

Scroll to Top