Click the blue text to follow us

1. Introduction
The ls command (full English: list directory contents) is used to display the attribute information of specified working directories or files (listing the files and subdirectories contained in the current working directory).
The ls command is one of the most commonly used commands in Linux.
By default, when the ls command is used without any parameters, it lists the file information in the current working directory, often used in conjunction with the cd or pwd commands, making it very convenient. With parameters, it can provide more detailed information.
2. Syntax
Usage: ls [options]… [files]…
Common parameter descriptions:
|
Parameter |
Parameter Description |
|
|
Display all files (including hidden files starting with |
|
|
Display all files except |
|
|
List files in long format (detailed information such as permissions, owner, size, modification time, etc.) |
|
|
When used with |
|
|
Sort by modification time (newest first) |
|
|
Reverse the sort order (used with |
|
|
Sort by file size (largest files first) |
|
|
Recursively list the contents of subdirectories |
|
|
Append a character indicator to the file name (e.g., |
|
|
Color output (usually enabled by default, |
|
|
Display the inode number of the file |
|
|
Display UID and GID in numeric form (instead of user and group names) |
|
|
Only display the directory itself, not its contents (often used with |
|
|
Display one file per line (automatically enabled when terminal width is insufficient) |
|
|
Display files in a comma-separated list |
|
|
Enclose file names in quotes (suitable for file names with spaces) |
|
|
Show directories first, then files |
|
|
Customize the time display format (e.g., |
3. Practical Examples
1. List all files in the current directory in detail (including hidden files)
[root@app01 ~]# ls -la
Total 48
dr-xr-x---. 5 root root 262 Jul 19 17:23 .
dr-xr-xr-x. 18 root root 255 Jul 5 10:38 ..
-rw-------. 1 root root 2076 Jul 2 16:03 anaconda-ks.cfg
-rw-------. 1 root root 3549 Jul 19 12:37 .bash_history
-rw-r--r--. 1 root root 18 Dec 29 2013 .bash_logout
-rw-r--r-- 1 root root 177 Jul 5 11:09 .bash_profile
-rw-r--r--. 1 root root 176 Dec 29 2013 .bashrc
drwx------. 4 root root 31 Jul 2 16:07 .cache
………………(output truncated)………………
2. Sort files in reverse order by size
[root@app01 soft]# ls -lShr
Total 1.8G
-rw-r--r-- 1 app app 175M Jul 5 11:06 jdk-17.0.15_linux-x64_bin.tar.gz
-rw-r--r-- 1 app app 185M Jul 5 11:06 nacos-server-3.0.2.zip
-rw-r--r-- 1 root root 583M Jul 5 16:45 mysql-8.0.32-linux-glibc2.12-x86_64.tar.xz
-rw-r--r-- 1 app app 848M Jul 5 11:06 mysql-8.0.42-linux-glibc2.28-x86_64.tar.xz
3. Sort files by size (largest files first)
[root@app01 soft]# ls -lSh
Total 1.8G
-rw-r--r-- 1 app app 848M Jul 5 11:06 mysql-8.0.42-linux-glibc2.28-x86_64.tar.xz
-rw-r--r-- 1 app app 583M Jul 5 16:45 mysql-8.0.32-linux-glibc2.12-x86_64.tar.xz
-rw-r--r-- 1 app app 185M Jul 5 11:06 nacos-server-3.0.2.zip
-rw-r--r-- 1 app app 175M Jul 5 11:06 jdk-17.0.15_linux-x64_bin.tar.gz
4. Recursively list the contents of the /app directory
[root@app01 jdk]# ls -lhR /app
/app:
Total 0
drwxr-xr-x 9 app app 136 Jul 5 11:07 jdk
drwxr-xr-x 12 mysql mysql 163 Jul 6 17:49 mysql-8.0.32
drwxr-xr-x 7 app app 96 Jul 6 16:57 nacos
drwxr-xr-x 2 app app 176 Jul 5 16:45 soft
/app/jdk:
Total 24K
drwxr-xr-x 2 app app 4.0K Jul 5 11:07 bind
drwxr-xr-x 5 app app 123 Jul 5 11:07 conf
drwxr-xr-x 3 app app 132 Jul 5 11:07 included
drwxr-xr-x 2 app app 4.0K Jul 5 11:07 jmods
drwxr-xr-x 72 app app 4.0K Jul 5 11:07 legal
drwxr-xr-x 5 app app 4.0K Jul 5 11:07 lib
/app/jdk/bin:
Total 448K
-rwxr-xr-x 1 app app 13K Feb 20 18:49 jar
-rwxr-xr-x 1 app app 13K Feb 20 18:49 jarsigner
-rwxr-xr-x 1 app app 13K Feb 20 18:49 java
………………(output truncated)………………
-h parameter displays file sizes in a readable format (e.g., KB, MB).
5. Display detailed information of the directory only (non-recursive)
[root@app01 jdk]# ls -ld /app
drwxr-xr-x. 6 app app 62 Jul 5 16:46 /app
6. Sort by modification time (newest files last)
[root@app01 soft]# ls -ltr
Total 1832048
-rw-r--r-- 1 app app 183197735 Jul 5 11:06 jdk-17.0.15_linux-x64_bin.tar.gz
-rw-r--r-- 1 app app 193786188 Jul 5 11:06 nacos-server-3.0.2.zip
-rw-r--r-- 1 app app 888706200 Jul 5 11:06 mysql-8.0.42-linux-glibc2.28-x86_64.tar.xz
-rw-r--r-- 1 app app 610315332 Jul 5 16:45 mysql-8.0.32-linux-glibc2.12-x86_64.tar.xz
7. List all directories under the /app directory
[root@app01 soft]# ls /app
jdk mysql-8.0.32 nacos soft
4. Knowledge Expansion
When file names contain spaces, special characters, or start with a dash, you can escape them using a backslash (\) or enclose the file name in quotes. For example:
[root@app01 soft]# ls "my filename.txt" // List the file named "my filename.txt"
[root@app01 soft]# ls my\ filename.txt // List the file named "my filename.txt"
The ls command can also use wildcards for pattern matching, for example, * matches any character, ? matches a single character, and […] matches characters within a specified range. For example:
[root@app01 soft]# ls *.txt # List all files with the .txt extension
[root@app01 soft]# ls file?.txt # List files named file?.txt, where ? represents any single character
[root@app01 soft]# ls [abc]*.txt # List files starting with a, b, or c, with a .txt extension
List all files starting with my in the current working directory, sorted by newest last.
[root@app01 soft]# ls -ltr my*
-rw-r--r-- 1 app app 888706200 Jul 5 11:06 mysql-8.0.42-linux-glibc2.28-x86_64.tar.xz
-rw-r--r-- 1 app app 610315332 Jul 5 16:45 mysql-8.0.32-linux-glibc2.12-x86_64.tar.xz
When using the ls -l command, the first column of characters indicates the type and permissions of the file or directory.
drwxr-xr-x 2 app app 176 Jul 5 16:45 soft
The first character indicates the file type, with the following attributes:
-: 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
The remaining 9 characters in the first column indicate the access permissions of the file or directory, grouped in sets of three characters for rwx permissions, with the following attributes:
r: indicates read permission
w: indicates write permission
x: indicates execute permission
-: indicates no corresponding permission
The first three characters indicate the owner’s permissions, the middle three indicate the group’s permissions, and the last three indicate the permissions for other users. For example:
drwxr-xr-x 2 app app 176 Jul 5 16:45 soft
The soft directory has read, write, and execute permissions for the owner, while the group and other users have only read and execute permissions.
Find the most recently modified files (display the 5 most recently modified files)
[root@app01 log]# ls -lt | head -5
Count the number of files
[root@app01 app]# ls | wc -l
Note: Count the number of files in the current directory (excluding hidden files).
The output color of the ls command can be controlled by the –color option, with the following color meanings:
Blue: directory
Green: executable file
Red: compressed file
Cyan: link file
Yellow: device file
If there is no color output when using the ls command, refer to the “solution for vi editor not displaying colors”.
Note: The ls command may have slight differences across different Linux distributions; you can refer to the man ls for specific documentation.


CSDN Blog丨Stars in the Sky
https://blog.csdn.net/myself88129