Hurry! Try our new Interactive Courses for FREE. 🥳   🚀

How to List Directory Content ( ls command)

In Linux, to list out all the files of a directory or subdirectory, we can use ls command. This is the most commonly used command that every Linux user uses. We will learn about this command in detail with the available options and examples.

  • If we use ls command without any arguments it lists the files and directory in the current working directory.
  • ls command is also available in the EFI (Extensible Firmware Interface) shell.
  • In other environments, such as DOS (Disk Operating System) and Microsoft Windows similar functionality is provided by the dir (directory) command.
  • The output of the ls command is colored whereas dir command is not colored.

The general syntax of the ls command

The following is the syntax of the ls command in Linux.

ls [OPTION]... [FILE]...

Linux ls command Options

The following is a brief description of the options available with the ls command.

Options Description
-a, --all It is used to show hiding files.
-A, --almost-all do not list implied . and . .
-author It is used to print the author information of each file.
--block-size=SIZE It is used to set scale size before printing.
-B, --ignore-backups It is used to ignore the listing of backup files.
-F, --classify It is used to append indicator (one of */=>@|) to entries.
--file-type It is used to set file type.
-i, --inode It is used to print the index number of each file.
-r, --reverse It is used to list files in reverse order while sorting.
-R, --recursive It is used to list subdirectories recursively.
--help It is used to display help for the ls command.
--version It is used to print version information of the ls command.

Example: lists the contents of a directory

In this example, the ls command displays the list of files and directories.

ls command

Example: lists hidden contents of directories

In this example, we are using ls -a or ls --all command that displays all files included hidden files ( files start with . or .. are hidden files or directory).

ls command

Example: Display the author of each file with ls command

In this example, we are using the ls command with the -author option that prints the author of each file with permission. We can also check it for any specific file using ls -author <file name>.

ls command

Example: list the contents of directories excluding backup files

In this example, we excluded backup files (files ends with ~ sign) by using ls -B option in the ls command. We can also use ls --ignore-backups to filter the backup files.

ls command

Example: Use of append indicator with ls command

In this example, ls -F or --classify command classifies files into their types.

  • '/' sign indicates a directory.
  • '*' sign indicates an executable.
  • '@' sign indicates a symbolic link.
  • '%' sign indicates a whiteout.
  • '=' sign indicates a socket.
  • '|' sign indicates a FIFO.

Example: List Subdirectories Recursively

If a directory contains several subdirectories and we want to list out all the subdirectories and their content then -R option can be used along with the ls command. See the command below.

$ ls -R

Conclusion

In this tutorial, we covered how to list directory contents in the Linux operating system using the ls command with available options and suitable examples. In Linux, dir and vdir are also available for the list out a directory and its content but for other purposes. We will discuss these later in our next tutorial.