Hello everyone! Today, we’re going to talk about one of the most commonly used commands in the Ubuntu system: the ls command. Its function is very straightforward—it’s like opening a folder to see what’s inside. However, knowing just ls isn’t enough; learning to use its “options” will make it more flexible for viewing file and directory information. Let’s break down this “nanny-level” tutorial step by step.
1. The Basics: The ls Command¶
Simply type ls in the terminal and press Enter to see a list of all files and folders in the current directory. These items are displayed in alphabetical order (by default, in English letters) and do not show hidden files (we’ll cover those later).
For example, if you’re in the “Downloads” folder, entering ls might show something like this:
document.pdf image.jpg notes.txt 视频文件.mp4
2. Common Options: Making ls “Smarter”¶
The power of the ls command lies in its ability to use various “options” (parameters added after ls, starting with -). Here are the most commonly used options for beginners:
-a: Show Hidden Files¶
Some files or folders are “hidden” and start with a . (e.g., the system configuration file .bashrc). By default, ls does not display these. To see them, use the -a option:
ls -a
The result might include hidden files like:
. .. .bashrc .profile document.pdf image.jpg
Here, . represents the current directory, and .. represents the parent directory—both are hidden.
-l: Show Detailed Information¶
By default, ls only shows filenames, which is too limited. The -l option provides more details, including file permissions, owner, size, modification time, etc.:
ls -l
The output might look like this:
总用量 20
drwxr-xr-x 2 user user 4096 6月 10 15:30 图片
-rw-r--r-- 1 user user 123 6月 10 14:00 document.pdf
-rw-r--r-- 1 user user 456 6月 10 13:20 image.jpg
Each column’s meaning (from left to right):
- Permissions: drwxr-xr-x (the first character d denotes a directory, - denotes a regular file). The next three rwx groups represent permissions for the owner, group, and others (r=read, w=write, x=execute).
- Hard Link Count: The second number is the number of links to the file (directories default to 2, regular files to 1).
- Owner/Group: The third and fourth columns are the owner and group of the file.
- Size: The fifth column is the file size (default unit: bytes; directories typically 4096 bytes).
- Modification Time: The sixth column is the last modified date and time.
- Filename: The last column is the filename or directory name.
-h: Make Sizes “Friendly”¶
The size shown with -l is in bytes, which can be large for big files. The -h option displays sizes in KB, MB, GB, etc. (must be used with -l):
ls -lh
The output might look like this:
总用量 10K
drwxr-xr-x 2 user user 4.0K 6月 10 15:30 图片
-rw-r--r-- 1 user user 123 6月 10 14:00 document.pdf
-rw-r--r-- 1 user user 456 6月 10 13:20 image.jpg
-t: Sort by Modification Time¶
By default, ls sorts by filename. The -t option sorts by the last modification time (newest first):
ls -lt
Newer files will appear at the top.
-r: Reverse Order¶
To reverse the sort order (e.g., alphabetical from Z to A, or time from oldest to newest), use -r:
ls -r
-S: Sort by File Size¶
The -S option sorts files by size (largest first):
ls -Sl
-d: Show Only Directories¶
To list only directories (not their contents), use -d:
ls -d *
Here, * matches all items, but -d shows only the directories themselves.
--color=auto: Add Color Coding¶
Ubuntu uses colors to distinguish file types (e.g., blue for directories, green for executables). If colors aren’t showing, add --color=auto:
ls --color=auto
3. Combining Options: Supercharging ls¶
You can combine multiple options for more powerful results (no spaces between options):
ls -lha # Detailed info + show hidden files + human-readable sizes
ls -ltr # Detailed info + sort by time + reverse (newest at end)
ls -Srh # Sort by size + reverse + human-readable sizes (large files first)
4. Viewing Other Directories¶
ls can also list contents of specified paths. For example, to view /home/user/Documents:
ls /home/user/Documents
5. Quick Summary¶
The ls command, while simple, becomes versatile with options:
- Basic view: ls
- Detailed info: ls -l
- Show hidden files: ls -a
- Hidden + detailed + human-readable sizes: ls -lha
- Sort by size: ls -Sl
Remember these combinations to flexibly view file and directory information in Ubuntu!
If you have questions, run man ls in the terminal for detailed help documentation.