Linux Command Line (Part 3)
The ls command is very important. With it, you can view the contents of directories and determine the directory attributes of various important files, so there is ample reason to believe that the ls command is definitely one of the most used commands.
Basic Usage
<span>ls</span> (list) can list the files and subdirectories in the current working directory:
yusen@yusen:~$ ls
公共的 模板 视频 图片 文档 下载 音乐 桌面 playground snap
You can also specify the directory you want to view:
yusen@yusen:~$ ls /usr
bin games include lib lib32 lib64 libexec libx32 local sbin share src
You can even view multiple directories at once:
yusen@yusen:~$ ls ~ /usr
/home/yusen:
公共的 模板 视频 图片 文档 下载 音乐 桌面 playground snap
/usr:
bin games include lib lib32 lib64 libexec libx32 local sbin share src
Tip:
<span>~</span>represents the current user’s home directory.
Common Options
Adding options to the command can change the output format or behavior, with the basic syntax being:
command -options arguments
- • Short options: add a
<span>-</span>before a single character (e.g.,<span>-l</span>). - • Long options: add a
<span>--</span>before a word (e.g.,<span>--reverse</span>). - • Multiple short options can be combined (e.g.,
<span>-lt</span>).
Example:
ls -lt --reverse
- •
<span>-l</span>: long format output, showing detailed information. - •
<span>-t</span>: sort by modification time. - •
<span>--reverse</span>: reverse sorting (descending).
Summary of common options:
| Option | Long Option | Description |
|---|---|---|
<span>-a</span> |
<span>--all</span> |
Show all files (including hidden files that start with <span>.</span>) |
<span>-d</span> |
<span>--directory</span> |
Show the directory itself rather than its contents |
<span>-h</span> |
<span>--human-readable</span> |
Display file sizes in a human-readable format (e.g., KB, MB) |
<span>-l</span> |
— | Long format output |
<span>-r</span> |
<span>--reverse</span> |
Reverse sorting |
<span>-S</span> |
— | Sort by file size |
<span>-t</span> |
— | Sort by modification time |
Check File Type: <span>file</span>
<span>file</span> command can determine the file type:
file Firefox.png
Firefox.png: PNG image data, 1278 x 782, 8-bit/color RGBA, non-interlaced
It outputs a brief description of the file’s content rather than relying solely on the file extension.
View Text Files: <span>less</span>
Text files are essential. Many files containing system settings are in text format. Being able to read these files allows us to understand how the system works.
Similar to the <span>file</span> command, let’s take a look at the file that defines all users in the system:
yusen@yusen:~$ less /etc/passwd
There is a lot of output, so I won’t display it here; press the Q key to exit the <span>less</span> command.
Here are a few common commands for <span>less</span>:
- • Press
<span>Page Up</span>/<span>Page Down</span>to scroll pages - • Use the arrow keys to scroll up and down
- •
<span>/keyword</span>to search - • Press
<span>h</span>for help - • Press
<span>q</span>to exit
File System Exploration Summary
- 1. Use
<span>cd</span>to enter the target directory - 2. Use
<span>ls -l</span>to view directory contents - 3. Use
<span>file</span>to determine file type - 4. If it is a text file, use
<span>less</span>to browse - 5. If you accidentally opened a binary file causing terminal garble, use
<span>reset</span>to recover
As regular users, we can explore the system with confidence, without worrying about breaking anything. Even if an unsolvable problem arises, we can simply delete the virtual machine and reinstall it. In the world of Linux, there are no secrets.
<span>ls -l</span> Output Field Explanation
Taking a line example:
drwxr-xr-x 2 yusen yusen 4096 9月 7 18:06 桌面
- 1. Permission Information (drwxr-xr-x)
- • The first character (d): indicates the file type.
- •
<span>d</span>: indicates this is a directory. - •
<span>-</span>: indicates this is a regular file. - •
<span>l</span>: indicates this is a link file. - • The next nine characters (rwxr-xr-x): divided into three groups, each representing the permissions for the owner, group, and others.
- • r (read): read permission. Can view file content or the list of files in a directory.
- • w (write): write permission. Can modify file content or create/delete files in a directory.
- • x (execute): execute permission. For files, can run this file; for directories, can enter this directory.
- •
<span>-</span>: indicates the absence of that permission.
- • This number indicates the number of hard links to the file or directory. For directories, this number is usually 2 plus the number of subdirectories it contains.
- • This field shows the username of the file’s owner.
- • This field shows the user group to which the file belongs.
- • This number indicates the size of the file or directory, in bytes. For directories, this size is usually fixed and does not represent the total size of all files within.
- • This field shows the date and time of the last modification of the file.
- • This is the name of the file or directory.