Basic ls Command in Linux

If you’ve worked with Linux before, you’re probably familiar with the ls command. Ls is an abbreviation for “list.” This command displays information about directories and files of any type in the current working directory.

This article will teach you how to use important ls commands. Even if you already know these commands, this article will help you refresh your memory. Every Linux user should be familiar with the ls commands.

Requirements

  • The Linux operating system
  • Terminal or command prompt access

ls Command Syntax

Let’s start by explaining the syntax of the ls command.

ls [options]

[options] – This allows you to supplement the ls command with additional instructions. The following sections of the article will provide examples.

Note: Because additional instructions are case sensitive, -r produces different results than -R.

ls Commands With Examples

Without the use of options, the most basic ls command is used. It prints files and directories in their bare form. You will not be able to see file types, dates, or permissions if you use this command.

To run this command, open a terminal window and type ls followed by entering on your keyboard.

sofija@sofija-VirtualBox:~$ls
Desktop Documents Downloads Music Pictures Public Templates Videos

ls Command Additional Options

The following additional options give users more flexibility when using the ls command:

  • ls -F
  • ls -m
  • ls -Q
  • ls -i

In a terminal window, type ls -F to append “/” to the end of each directory. This command will assist you in distinguishing between directories and files. For more information, please see the image below:

List content of home directory distinguishing files and directories using ls command.

By typing the ls -m command, the terminal prints out directories and files separated by a comma:

List directories separated by comma using ls command.

To add quotation marks to all directories and files, use the ls -Q command, as shown below:

List and add quotation marks to all directories and files with ls command.

In your terminal, type ls -i to get the Inode (index node) number of all directories and files:

List and get index node of all directories with ls command.

Sorting Options

There are three kinds of sorting options that you can use:

  • ls -r
  • ls -t
  • ls -X

You have the option to sort directories and files in reverse order by typing ls -r. See the example in the image below:

ls -r
Sort directories and files in reverse order with ls command.

Sort directories and files in reverse order with ls command.

Use the ls -t command to sort directories and files by time and date of creation or modification:

Sort directories and files by time and date using ls command.

Sort directories and files by time and date using ls command.

To sort directories and files alphabetically by entry extension type ls -X in a terminal:

List directories and files alphabetically with ls command.

List directories and files alphabetically with ls command.

View Hidden Files

When using the basic ls command, you can’t see hidden files and files starting with “.”. Type the ls -a command to display them as seen in the image below:

List hidden files in Ubuntu using ls command.

List hidden files in Ubuntu using the ls command.

To get a full list of hidden files, type ls -la in your terminal. The output displays information about the user, size of the file, and date and time of modification.

Display a full list of hidden files with ls command.

Display a full list of hidden files with the ls command.

Note: To learn how to hide and see hidden files in Linux, refer to our Show Hidden Files in Linux article.

Directory Trees With the ls Command

If you want to access long listing directory trees, type ls -R. The example below displays the expected output.

Access long listing directory trees with ls - R command.

Type ls -lR in the terminal to display additional information on the directory tree, such as the owner of the file, size, and date and time of the last modification, as seen in the image below:

Display additional information on directory tree with ls -lR command.

Advanced ls Commands

So far, you’ve only seen basic ls commands that display a limited amount of information about directories and files. To obtain detailed information about your files and directories, use advanced ls commands. Advanced ls commands include:

  • ls -l
  • ls -n
  • ls -lh
  • ls -ltr
  • ls -lS
  • ls -l /tmp
  • ls -ld /tmp/
  • ls –help

View Long Listing Format

To print a long listing format of files and directories, use the ls -l command. As shown in the image below, the output displays information such as file or folder name, owner of the file and permissions, size, and modified date and time:

Print out a lond listing format of files and directories with ls -l command.

List UID and GID of Files

In the terminal, type ls -n to see the UID (unique identifier) and GID (group ID) assigned to all files and directories:

Display the UID and GID assigned to all files and directories with ls -n command.

Display Files in Human Readable Format

In the terminal, type ls -lh to see the size of files and directories in a human-readable format. As shown in the image below, the output contains the following information:

Check the sizes of files and directories with ls -lh command.

View Reverse Output Order by Date

When you enter ls -ltr, the terminal displays a long listing format of files and directories with the most recent modification date:

Display a long listing format of files and directories with ls -ltr command.

List Files by Size

ls -lS returns a long listing format of files and directories sorted by file size, from largest to smallest.

Long listing format of files and directories sorted by file size with ls -lS command.

Long listing format of files and directories sorted by file size with ls -lS command.

Display Files Under /tmp Directory

In the terminal, type ls -l /tmp to access files in the /tmp directory. The output should look something like this:

List files in the tmp directory using ls -l /tmp command.

Using the ls -l /tmp command, you can list the files in the tmp directory.

When you want to check information about the /tmp directory without accessing any files, use ls -ld /tmp/.

View All ls Commands

Type ls —help to see a list of all ls command options. The system displays all of the available options.

Conclusion

You may now be able to use the most basic ls commands in Linux after reading this article. Working with files and directories is essential, and we recommend that you learn how to create a file in Linux next.

Leave a Reply