Signup/Sign In

How to List Files/Directories in Linux (ls command)

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

  • If we use ls command without any arguments it lists the files and sub-directory in the current working directory.

  • The 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.

Syntax of the ls command

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

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

Here is how you can use the ls command,

ls

Yes, that's it. Just write ls, and hit enter to see all the files and directories present in the current directory.


mydirectory myfile.txt

There are many options available that you can use with the ls command.

Linux ls command options

Here are a few commonly used options with the ls command.

Options Description
-a, --all It is used to show all the files, even the files starting with .
-l
-author It is used to print the author information of each file.
-d List the directories but not their content
-B, --ignore-backups It is used to ignore the listing of backup files.
-c To sort the files by ctime (last modification time), and show the newest first.
-l Provide a detailed long listing format with details like owner, permissions, size, etc.
-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. Run the ls command with --help to see the list of all the options.
--version It is used to print version information of the ls command.
  • There are many more options like -h which can be used with -l option to make the size of the files human readable.

  • You can use the -n with -l option to list the numeric user ID instead of the username for files and directories.

  • The -t flag can sort the files and directories based on time, showing the newest first.

Example 1: List the contents of a directory

In this example, the ls command displays the list of files and directories present in the current directory.

We have used the -l option because this shows the list with some useful additional information.

ls -l


drwxr-xr-x 2 bitnami bitnami 4096 Jul 4 08:02 mydirectory
-rw-r--r-- 1 bitnami bitnami 571 Jul 4 08:02 myfile.txt

As you can see in the output above, you get a lot of extra information like size, author, permissions, etc. along with the list of files and directories.

Example 2: Lists hidden contents of directories

In this example, we are using ls -a or ls --all command that displays all files including hidden files(files starting with . or .. are hidden files or directories).

ls -a


. .. .hidden_file mydirectory myfile.txt

You see the hidden files are also listed.

To get a better output, combine the -a option with the -l option, like this:

ls -la


drwxr-xr-x 3 bitnami bitnami 4096 Jul 4 08:16 .
drwxr-xr-x 5 bitnami bitnami 4096 Jul 4 08:02 ..
-rw-r--r-- 1 bitnami bitnami 0 Jul 4 08:16 .hidden_file
drwxr-xr-x 2 bitnami bitnami 4096 Jul 4 08:02 mydirectory
-rw-r--r-- 1 bitnami bitnami 571 Jul 4 08:02 myfile.txt

You see it's easier this way to read the output.

Example 3: Make the output more human-readable

You can use the -h option with the -l option to show the size of the files or directories in a more human-readable format, like 4K, 10M, etc.

ls -lh


total 8.0K
drwxr-xr-x 2 bitnami bitnami 4.0K Jul 4 08:02 mydirectory
-rw-r--r-- 1 bitnami bitnami 571 Jul 4 08:02 myfile.txt

Example 4: Making combinations of ls command options

The ls command has many options and you can combine various different options and try the ls command.

For example, we used the human-readable option,

ls -lh

Then you can use the -t option to sort the list by time,

ls -lt

Try some combinations, go ahead, the ls command is harmless.

Example 5: Use of append indicator with the 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.

Let's try this command,

ls -lF


drwxr-xr-x 2 bitnami bitnami 4096 Jul 4 08:02 mydirectory/
-rw-r--r-- 1 bitnami bitnami 571 Jul 4 08:02 myfile.txt

The directory has a / at the end to show that it is a directory.

Example 6: 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 -lR


.:
total 8
drwxr-xr-x 3 bitnami bitnami 4096 Jul 4 08:32 mydirectory
-rw-r--r-- 1 bitnami bitnami 571 Jul 4 08:02 myfile.txt

./mydirectory:
total 4
drwxr-xr-x 2 bitnami bitnami 4096 Jul 4 08:32 subdir

./mydirectory/subdir:
total 0

In our current directory, we had a directory with the name mydirectory, inside which we have another directory subdir, and the same is displayed above in the output.

The End

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 commands are also available to list a directory and its content.

You should practice the different options along with the ls command and see what output you get.



About the author:
Pradeep has expertise in Linux, Go, Nginx, Apache, CyberSecurity, AppSec and various other technical areas. He has contributed to numerous publications and websites, providing his readers with insightful and informative content.