Linux ls command

Linux 命令大全Linux Command Reference

Linux ls(Full English spelling: list directory contents) The command is used to display the contents of the specified working directory (listing the files and subdirectories contained in the current working directory).

Syntax

 ls [-alrtAFR] [name...]

Options:

OptionsDescription
-aor--allDisplay all files (including those starting with.a dot, i.e., hidden files).
-Aor--almost-allDisplay all files except.and..for the . and .. entries (including hidden files).
-lList files in long format (detailed information) (permissions, owner, size, modification time, etc.).
-hor--human-readableand-lWhen used together, display file sizes in human-readable format (such as KB, MB).
-tSort by modification time (newest first).
-ror--reverseReverse sorting (used with-t、-Setc.).
-SSort by file size (largest files first).
-Ror--recursiveRecursively list subdirectory contents.
-For--classifyAppend an indicator to filenames (such as/indicating a directory,*indicating an executable file).
--colorColor output (usually enabled by default,--color=auto)。
-ior--inodeDisplay the file's inode number.
-nor--numeric-uid-gidDisplay UID and GID in numeric form (instead of user and group names).
-dor--directoryShow only the directory itself, not its contents (often used with-l)。
-1Display only one file per line (automatically enabled when the terminal width is insufficient).
-mDisplay files as a comma-separated list.
-Qor--quote-nameEnclose filenames in quotes (useful for filenames containing spaces).
--group-directories-firstShow directories first, then files.
--time-style=<格式>Customize the time display format (e.g.,+%Y-%m-%d)。

Examples

ls -l                    # List 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 of the /etc/passwd file

Examples

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

ls -la

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

ls -lShr

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

ls -lhR /var/log

Show only detailed information of the directory (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 of all directories and files under the /bin directory:

ls -lR /bin

When filenames contain spaces, special characters, or start with a dash, you can use backslash (\) for escaping, or enclose the filenames in quotes. For example:

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

The ls command can also use wildcards for pattern matching, for example*represents matching any characters,?represents matching one character,[...]represents matching 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 the newest ones at the end:

ls -ltr s*

When using thels -lcommand, the first column of characters indicates the type and permissions of the file or directory. The first character indicates the file type, for example:

  • - indicates a regular file
  • d indicates a directory
  • l indicates a symbolic link
  • c indicates a character device file
  • b indicates a block device file
  • s indicates a socket file
  • p indicates a pipe file

When using thels -lcommand, the remaining 9 characters in the first column represent the access permissions of the file or directory, corresponding to three-character groups ofrwxpermissions. For example

:
  • r indicates read permission
  • w indicates write permission
  • x indicates execute permission
  • - indicates no corresponding permission

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

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

Indicates the file named file.txt; the owner has read and write permissions, while the group and other users have only read 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 number of files in the current directory (excluding hidden files).


Notes

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

  • Blue: directory
  • Green: executable file
  • Red: compressed file
  • Cyan: link file
  • Yellow: device file

When using ls in scripts, note that directly parsing the output of ls may be unreliable; it is recommended to use other methods.

The ls command may differ slightly across Linux distributions; you canman lsview the specific help documentation.

Linux 命令大全Linux Command Reference

Other Extensions