Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Linux ls Command

Linux ls Command

Linux Command Reference Linux Command Reference

The Linux ls (short for "list directory contents") command is used to display the contents of a specified working directory (listing the files and subdirectories contained in the current working directory).

Syntax

 ls [-alrtAFR] [name...]

**Parameters**:

Parameters Description
-a or --all Display all files (including hidden files starting with . ).
-A or --almost-all Display all files except . and .. (including hidden files).
-l List files in long format (detailed information): permissions, owner, size, modification time, etc.
-h or --human-readable The difference between -l When used together, display file sizes in a human-readable format (such as KB, MB).
-t Sort by modification time (newest first).
-r or --reverse Reverse the sort order (used with -t, -S , etc.).
-S Sort by file size (largest first).
-R or --recursive List subdirectory contents recursively.
-F or --classify Append an indicator after the file name (such as / for a directory,* for an executable file).
--color Colorized output (usually enabled by default)--color=auto).
-i or --inode Display the inode number of files.
-n or --numeric-uid-gid Display UID and GID numerically (instead of user and group names).
-d or --directory Display only the directories themselves, not their contents (often used with -l).
-1 Display one file per line (enabled automatically by default when the terminal width is insufficient).
-m Display files as a comma-separated list.
-Q or --quote-name Enclose file names in quotes (useful for names containing spaces).
--group-directories-first List directories first, then files.
--time-style=<格式> Customize the time display format (such as +%Y-%m-%d).

Example

ls

-l

                   

Display files and directories in the current directory in long format

ls

-a

                   

Display all files and directories in the current directory, including hidden files

ls

-lh

                 

Display file and directory sizes in the current directory in a human-readable way

ls

-t

                   

Display files and directories in the current directory sorted by modification time

ls

-R

                   

Recursively display all files and subdirectories in the current directory

ls

-l

/

etc

/

passwd

       

Display detailed information about the /etc/passwd file

Example

List all files in the current directory in detail (including hidden files):



ls -la

Sort files by size in reverse order (largest first):



ls -lShr

Recursively list the contents of the /var/log directory with human-readable file sizes:



ls -lhR /var/log

Display detailed information for directories only (non-recursive):



ls -ld /etc

Sort by modification time (newest files shown last):



ls -ltr

List all directories under the root directory:



# ls /

bin               dev   lib         media  net   root     srv  upload  www

boot              etc   lib64       misc   opt   sbin     sys  usr

home  lost+found  mnt    proc  selinux  tmp  var

List detailed information for all directories and files under /bin:

ls -lR /bin

When a file name contains spaces, special characters, or begins with a dash, you can escape it with a backslash or enclose the file name in quotes. For example:

ls "my file.txt"    # 列出文件名为"my file.txt"的文件

ls my\ file.txt     # 列出文件名为"my file.txt"的文件

ls -- -filename     # 列出文件名为"-filename"的文件

The ls command also supports wildcard pattern matching; for example, * matches any characters, ? matches a single character, and [...] matches characters within a specified range. For example:

ls *.txt         # 列出所有扩展名为.txt的文件

ls file?.txt     # 列出文件名为file?.txt的文件,其中?表示任意一个字符

ls [abc]*.txt    # 列出以a、b或c开头、扩展名为.txt的文件

List all files in the current working directory whose names start with s, with newer ones listed later:

ls -ltr s*

When using the ls -l command, the characters in the first column indicate the file or directory type and permissions. The first character indicates the file type, for example:

When using the ls -l command, the remaining 9 characters in the first column represent the access permissions of the file or directory, corresponding to three groups of rwx permissions. For example

:

The first three characters indicate the owner's permissions, the middle three indicate the group's permissions, and the last three indicate other users' permissions. For example:

-rw-r--r-- 1 user group 4096 Feb 21 12:00 file.txt

indicates a file named file.txt where the owner has read and write permissions, while the group and other users have read-only permission.

Find recently modified files:



ls -lt | head -5

Display the 5 most recently modified files.

Count the number of files:



ls | wc -l

Count the files in the current directory (excluding hidden files).


Notes

The output colors of the ls command can be controlled with the --color option:

When using ls in scripts, note that parsing the output of ls directly can be unreliable; other methods are recommended.

The ls command may differ slightly across Linux distributions; you can man ls to view the specific help documentation.

Linux Command Reference Linux Command Reference