Friday, November 15, 2024
Google search engine
HomeGuest Blogs19 Crucial Linux ls Commands to Know

19 Crucial Linux ls Commands to Know

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.

Linux ls commands every user must learn.Linux ls commands every user must learn.

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.

ls Commands With Examples

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.

ls command examplels command example

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:

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

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

List directories separated by comma using ls command.List directories separated by comma using ls command.

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

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

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

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

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:

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.

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:

List hidden files in Ubuntu using ls command.List hidden files in Ubuntu using 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 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.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.Display additional information on directory tree with ls -lR command.

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:

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

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:

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

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:

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

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:

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

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.

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

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:

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

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.

Was this article helpful?
YesNo

RELATED ARTICLES

Most Popular

Recent Comments