Which switch below can be added to the ls command to show a list of files and their type?

Introduction

If you have worked in Linux, you may be familiar with the ls command. Ls is short for “list”. This command lists information about directories and any type of files in the working directory.

In this article, you will learn to use crucial ls commands. Even if you have existing knowledge of these commands, this article will help you refresh your knowledge.

Which switch below can be added to the ls command to show a list of files and their type?

Prerequisites

  • Linux system
  • Access to the terminal or command prompt

ls Command Syntax

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

ls [options]

[options] – This lets you add additional instructions to the ls command. Examples will be provided in the following sections of the article.

Note: Additional instructions are case sensitive, so -r displays different output than -R.

The most basic ls command is without the use of options. It prints out files and directories in their bare format. With this command, you will not be able to see file types, dates, and permissions.

To execute this command, type ls in terminal window and press Enter on your keyboard.

Which switch below can be added to the ls command to show a list of files and their type?

ls Command Additional Options

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

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

Type ls -F in a terminal window to add “/” at the end of each directory. This command will help you to distinguish directories from files. See image below for further details:

Which switch below can be added to the ls command to show a list of files and their type?

When you type the ls -m command, the terminal prints out directories and files separated by a comma:

Which switch below can be added to the ls command to show a list of files and their type?

Run the ls -Q command to add quotation marks to all directories and files as in the image below:

Which switch below can be added to the ls command to show a list of files and their type?

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

Which switch below can be added to the ls command to show a list of files and their type?

Sorting Options

There are three different sorting options that you can use:

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

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

Which switch below can be added to the ls command to show a list of files and their type?

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

Which switch below can be added to the ls command to show a list of files and their type?

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

Which switch below can be added to the ls command to show a list of files and their type?

Note: If you want to learn how to sort file contents, refer to Linux sort 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:

Which switch below can be added to the ls command to show a list of files and their type?

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.

Which switch below can be added to the ls command to show a list of files and their type?

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.

Which switch below can be added to the ls command to show a list of files and their type?

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:

Which switch below can be added to the ls command to show a list of files and their type?

Advanced ls Commands

So far, you have learned simple ls commands that display a limited amount of information about directories and files. You should use advanced ls commands to access extensive information about your files and directories. Advanced ls commands are:

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

View Long Listing Format

Run the ls -l command to print out a long listing format of files and directories. The output displays information, such as file or folder name, owner of the file and its permission, size and modified date and time as in the image below:

Which switch below can be added to the ls command to show a list of files and their type?

List UID and GID of Files

Use the ls -n command in terminal to display the UID (unique identifier) and GID (group ID) assigned to all files and directories:

Which switch below can be added to the ls command to show a list of files and their type?

Display Files in Human Readable Format

If you want to check the size of files and directories in a human readable format, type ls -lh in terminal. The output displays content as in the image below:

Which switch below can be added to the ls command to show a list of files and their type?

View Reverse Output Order by Date

When you type ls -ltr, the terminal prints out a long listing format of files and directories with the latest modification date:

Which switch below can be added to the ls command to show a list of files and their type?

List Files by Size

Run the ls -lS command to receive long listing format of files and directories sorted by file size, from largest to smallest.

Which switch below can be added to the ls command to show a list of files and their type?

Display Files Under /tmp Directory

If you want to access files in the /tmp directory, type ls -l /tmp in terminal. The output should be similar to the one seen below:

Which switch below can be added to the ls command to show a list of files and their type?

Type ls -ld /tmp/ when you want to check information about the /tmp directory only, without accessing any files in the directory.

View All ls Commands

To list all ls command options, type ls --help. The system lists all available options.

Note: Next, learn how to create files in Linux text editors, such as Vim. See our Vim Cheat Sheet article that lists the most crucial Vim commands.

Conclusion

After reading this article, you have learned how to use the most important ls commands in Linux. Working with files and directories is crucial, and next we recommend learning how to create a file in Linux.

What command can be used to confirm which directory you are in at a command line prompt?

Use pwd to identify the current directory of your Terminal session. Use ls to list the files in the current directory of your Terminal session. Use cd and cd .. to change directories of your Terminal session.

What command can be used to display the last five lines of a text file?

To look at the last few lines of a file, use the tail command. tail works the same way as head: type tail and the filename to see the last 10 lines of that file, or type tail -number filename to see the last number lines of the file.

What mode does the vi editor open by default?

Command mode is the default mode for vi .

What key can be pressed to activate the bash shell's completion feature?

One extremely time-saving feature built into the Bash Shell is the ability to "tab-complete" commands. Simply hit the tab key while you are typing a command, and the shell will automatically finish the command for you.