SoFunction
Updated on 2025-04-14

Detailed explanation of Linux ls command operation

1. Introduction to the command

lsCommands (lists) are used to list directory contents and are one of the most commonly used commands in Linux. passlsBy command, we can view the files and subdirectories in the specified directory, and combine different options to obtain detailed file information, such as permissions, sizes, modification time, etc.

In Linux file management,lsIt is an indispensable tool, not only suitable for daily file browsing, but also can cooperate with other commands to perform complex file operations.

2. Basic syntax and usage of commands

2.1 Syntax format

ls [Options] [Directory or file]

in:

  • [Options]: Optional for controllsThe output format of the command.
  • [Directory or File]: Optional, specify the directory or file to view. If omitted, the current directory will be viewed by default (.)。

2.2 Use examples

2.2.1 List files and folders in the current directory

ls

illustrate: By default, files and subdirectories in the current directory are listed, and hidden files are not displayed.

2.2.2 List the contents in the specified directory

ls /etc

illustrate:Check/etcList of files in the directory.

2.2.3 Show hidden files (files starting with .)

ls -a

illustrate-aOptions show all files, including hidden files.

2.2.4 Display file information in a detailed list

ls -l

illustrate-lOptions display details of the file, such as permissions, owner, size, and modification time.

3. Common options and parameters of commands

Options effect Example
-a Show all files (including hidden files) ls -a
-l Display file details in long format ls -l
-h Display file size in a human-readable format ls -lh
-t Sort by modification time, most recently modified files are preferred ls -lt
-r Reverse sorting ls -lr
-R Recursively display subdirectory content ls -R

4. Command execution example

4.1 Display file details (long format)

ls -l

Output example

total 12
-rw-r--r-- 1 user user  1234 Mar  1 12:34
drwxr-xr-x 2 user user  4096 Mar  1 12:30 folder1
-rwxr-xr-x 1 user user  5678 Mar  1 12:40

explain

  • Column 1: File type and permissions (-rw-r--r--)。
  • Second column: number of hard links.
  • Columns 3 and 4: File owner and group to which it belongs.
  • Column 5: File size (bytes).
  • Columns 6 to 8: The last time of the file is modified.
  • Last column: file name.

4.2 Show all files, including hidden files

ls -a

Output example

.  ..  .bashrc  .profile    folder1

explain

  • .Represents the current directory.
  • ..Represents the previous directory.
  • by.The file that begins is a hidden file.

4.3 Display file size in human-readable format

ls -lh

Output example

total 12K
-rw-r--r-- 1 user user 1.2K Mar  1 12:34
drwxr-xr-x 2 user user 4.0K Mar  1 12:30 folder1
-rwxr-xr-x 1 user user 5.5K Mar  1 12:40

explain

  • 1.2KIndicates that the file size is 1.2 KB.
  • 4.0KRepresents the default size of the directory (usually 4 KB).

5. Advanced usage of commands

5.1 Sort by file size

ls -lS

illustrate-SThe options are sorted in descending order by file size.

5.2 Recursively list all files in the directory

ls -R

illustrate: Lists the files in the current directory and all subdirectories.

5.3 Filter specific files in combination with grep

ls -l | grep "txt"

illustrate: Show only containstxtKeyword file.

6. FAQs and Answers to Commands

6.1 The ls command displays ls: cannot access 'xxx': No such file or directory

Solution

  • Make sure the path is correct.
  • usepwdConfirm the current directory.

6.2 ls -l Display ????? instead of permission information

reason: It may be that the file system is corrupted or the user does not have permission to access it.
Solution:trysudo ls -lOr check the file system status.

6.3 ls Display color abnormality or no color

  • reason: Color support may not be enabled in the terminal environment.
  • Solution:usels --color=autoto enable color.

7. Summary and Suggestions

  • lsCommands are the most basic file management commands in Linux, often with-l-aUse it with options.
  • CombinedgrepsortTools such as this can improve efficiency.
  • When listing files recursively,ls -RSuitable for scenarios with deeper directory levels.
  • ls -lhMake the file size easier to read, and it is recommended for daily use.

I hope this tutorial will help you master itlsCommands to improve Linux file management efficiency!

This is the end of this article about the detailed explanation of Linux ls command. For more related Linux ls command content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!