Linux ls command
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:
| Options | Description |
|---|---|
-aor--all | Display all files (including those starting with.a dot, i.e., hidden files). |
-Aor--almost-all | Display all files except.and..for the . and .. entries (including hidden files). |
-l | List files in long format (detailed information) (permissions, owner, size, modification time, etc.). |
-hor--human-readable | and-lWhen used together, display file sizes in human-readable format (such as KB, MB). |
-t | Sort by modification time (newest first). |
-ror--reverse | Reverse sorting (used with-t、-Setc.). |
-S | Sort by file size (largest files first). |
-Ror--recursive | Recursively list subdirectory contents. |
-For--classify | Append an indicator to filenames (such as/indicating a directory,*indicating an executable file). |
--color | Color output (usually enabled by default,--color=auto)。 |
-ior--inode | Display the file's inode number. |
-nor--numeric-uid-gid | Display UID and GID in numeric form (instead of user and group names). |
-dor--directory | Show only the directory itself, not its contents (often used with-l)。 |
-1 | Display only one file per line (automatically enabled when the terminal width is insufficient). |
-m | Display files as a comma-separated list. |
-Qor--quote-name | Enclose filenames in quotes (useful for filenames containing spaces). |
--group-directories-first | Show directories first, then files. |
--time-style=<格式> | Customize the time display format (e.g.,+%Y-%m-%d)。 |
Examples
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 Command Reference