How to Use ls Command?

To view a directory’s files and directories, use ls command in Linux. One of the most frequently used commands in Linux, it may be found in the majority of Linux distributions. The command’s fundamental syntax is as follows:

Use of Ls command Example SYNTAX

ls [options] [directory]
note : options are the available flags and directory is the path where you want to list the contents

The command will show the files and directories in the current working directory if you run it without specifying any parameters or a directory.

ls command without flags and directory
note: the command above is a basic use of ls command without any flags or directory
use of ls command with -l flag and /usr directory
note: above is an example of ls command with “-l” flag and “/usr” directory path.

Common Flags to use

  • -a : Using this option, all files, even hidden files and folders (files that begin with a dot), are displayed (.)
  • -l : With the -l option, you may see information about the files and directories, including their size, last modification time, owner, group, and rights.
  • -t : With the newest files displayed first, the -t option sorts files and folders according to when they were last modified.
  • -r : Using the -r option, you can see the files and directories in reverse.
  • -h : File sizes are shown with the -h option in a format that is understandable by humans.
  • -R : When using the -R option, you can list files and directories recursively, which includes listing the contents of all subdirectories

To get the desired outcome, you can also combine several alternatives. For instance,

ls -ltr [directory]

The command above will show the files and directories in long format. Furthermore, the result will be arranged by the time of last modification, and in the opposite direction.
A very potent tool that can be used to list files and directories in various ways is the ls command. When working with several files and directories and when you need to locate particular files quickly, it is helpful.

Leave a Comment

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

Scroll to Top
Scroll to Top