Linux 中的 ls 命令使用教程
ls 命令
![](/data/attachment/album/202310/08/200201p8sfy8shyyyspxxx.jpg)



> 
> ls 属于 Linux 中那些简单又常用的命令之一。在本教程里，你将学到如何高效使用它。
> 
> 
> 


对我个人而言，`ls` 无疑是 [最常被使用的 Linux 命令](https://itsfoss.com/essential-ubuntu-commands/)，因为我们总是借助它 [列出目录内的内容](https://itsfoss.com/list-directory-content/) 来检验上一步操作的结果。


`ls` 命令是 “<ruby> 列表 <rt>  List </rt></ruby>” 的缩写，其功能就是列出你指定目录下的所有内容。


我将在本教程中，通过一系列的实例，带你一步步掌握 `ls` 命令的使用。


另外也会给出一些练习题，以检验你的学习成果。


### 如何运用 ls 命令


要使用 `ls` 命令，你只需遵循以下简单的命令格式：



```
ls [OPTIONS] Targeted_Directory

```

这里，


* `[OPTIONS]`：用来改变 ls 命令的默认行为。
* `Targeted_Directory`：在这里输入目录名或目录的绝对路径。


你可能会想，如果没有任何选项地使用 `ls` 命令会发生什么。为了回答这个问题，我在当前工作目录中使用了 `ls` 命令：


![使用 ls 命令列出工作目录的内容](/data/attachment/album/202310/08/200241vtij69midd88iq2i.png)


如你所见，它列出了当前工作目录中所有可用的目录和文件。但你可以做的远不止于在当前工作目录中获取名称和文件。


接下来让我给你展示一下。


#### 1、列出带有所有权的文件和目录


`ls` 命令的重要用途之一就是查出具体的文件或目录的权限以及所有权信息。


这就需要你在使用 `ls` 命令时，带上 `-l` 选项（这也被称为长列表格式）：



```
ls -l

```

执行该命令后，你会看到类似的输出：



```
$ ls -lh
-rwxrw-r-- 1 sagar sagar 666M Dec 10 18:16 Fedora.iso

```

注意到我是同时使用了 `–h` 选项吗？我们稍后会详细说明这个。


如果目前为止的输出结果看起来太复杂，那就让我来帮你简化一下：


![利用 ls 命令在 Linux 中解读文件权限和所有权](/data/attachment/album/202310/08/200241v9alyc09cysy9juy.png)


如你所见，每个文本块都有特定的含义，它们分别对应文件拥有者、组和其他用户的各项权限。


对于想对文件权限有更深入理解的朋友，我推荐你阅读 [我们关于 Linux 文件权限的详尽指南](https://linuxhandbook.com:443/linux-file-permissions/)。


#### 2、获取以人类可读的方式显示的信息


默认状态下，文件大小以字节为单位显示，显然不是最佳的查阅方式。那如果想以更易读的方式来得知相同的信息，我们怎么做呢？


其实很简单。你只需使用 `-h` 选项与 `ls` 命令配合：



```
ls -l -h

```

接下来，我们比较下默认显示状态和可读性更强的显示方式的区别：


![以人类可读的方式通过 ls 命令列出文件](/data/attachment/album/202310/08/200242ji4j4q7zeltjddkd.png)


明显更好了，对吧？



> 
> ? 尽管 `ls` 命令很好地展示了文件大小，但它并不能告诉你文件夹的大小，普遍情况都会显示为 4K（LCTT 译注：这其实是“目录”这个文件所占用的大小，而不是该目录下文件的总计大小。）。如果需要了解文件夹的大小，那么你可以尝试使用 `du` 命令。
> 
> 
> 


#### 3、列出隐藏文件


和其他所有文件管理器一样，`ls` 命令在默认情况下并不会显示隐藏文件（这正是隐藏文件的意义所在，对吧？）。


那么，如果你期望在常规文件之外，一并 [列出隐藏文件](https://itsfoss.com/show-hidden-files-linux/) 呢？只需在使用 `ls` 命令时带上 `-a` 选项就行：



```
ls -a

```

你会注意到，以 `.` 开头的文件名就是所谓的隐藏文件。


![用 ls 命令在 Linux 终端列出隐藏文件](/data/attachment/album/202310/08/200242g1zbqvhiboqub4uq.png)



> 
> ? 你还可以使用 `ls -A`，它的功能与 `ls -a` 类似，但它不包括 `.` 和 `..` 这两个目录。
> 
> 
> 


#### 4、递归列出文件


事实上，有 [多种方法可以递归列出文件](https://linuxhandbook.com:443/list-files-recursively/)，使用 `ls` 命令就是其中之一。


如果你还不清楚，递归列出文件就是指展示所有子目录中的文件，直到每个子目录的最深层次。


你可以通过使用 `-R` 参数来递归列出文件：



```
ls -R

```

![在 Linux 中使用 ls 命令递归列出文件](/data/attachment/album/202310/08/200242tstd49xrx5ky9rl2.png)


这种方式实际上为你展示了当前目录的结构，我个人非常喜欢这种方式，但你需要先进行安装。



> 
> ? 其实你并不需要进入某个目录才能查看其内容，你可以直接通过提供它的绝对路径或相对路径来列出目录内容，比如：`ls /var/log`。
> 
> 
> 


#### 5、在使用 ls 时对文件和目录做区分


通常，不同的颜色就足以区分文件和目录。但是如果你基于某种原因需要给文件和目录添加符号标识，这就有一个办法。


在 `ls` 命令中，使用 `-F` 选项，它会在每个目录名末尾添加一个正斜杠 `/`：



```
ls -F

```

![使用 ls 命令在文件和目录间做区分](/data/attachment/album/202310/08/200243hzxsur5xt7yffduu.png)


#### 6、列出指定扩展名的文件


有些情况下，你可能只希望列出具有特定扩展名的文件，实际上，这是最简单的操作之一。


做到这一点，你无需使用任何选项。只需在星号 `*` 后附上文件扩展名，如 `*.png`，`*.txt` 等：



```
ls *.extension

```

例如，如果我只想列出 ISO 文件，那么我将使用以下命令：



```
ls *.iso

```

![列出特定扩展名的文件](/data/attachment/album/202310/08/200243dzqwspuquuz6jqpy.png)


#### 7、基于大小对输出内容排序


若希望基于文件大小来对输出内容排序，你需要使用 `-S` 选项，它会以从大到小的方式（降序）列出文件：



```
ls -lhS

```

![使用 ls 命令基于文件大小排序](/data/attachment/album/202310/08/200243rtc4qu224vtvccc6.png)


相反，如果你想颠倒这个顺序，使得最小的文件首先被列出，你可以利用 `-r` 选项来实现反向排序：



```
ls -lhSr

```

![使用 ls 命令从小到大排序文件](/data/attachment/album/202310/08/200244y73izpooanph9ii4.png)


#### 8、根据日期和时间排序文件


`ls` 命令在列出的内容中包括文件的修改时间。


如果你希望最新的文件最先被列出，可以使用 `-t` 选项，如下所示：



```
ls -lht

```

![使用 ls 命令最先列出最新文件](/data/attachment/album/202310/08/200244wyjjwycqcjcc4qcf.png)


你也可以像我前面提到的那样，在这里使用 `-r` 选项进行反转排序。



```
ls -lrt

```

这会使得最近修改的文件在列表的底部显示，当目录中存在大量文件，而你希望查看最近被修改过的文件时，这一命令会特别有用。我在解决我的软件项目问题时就经常用到它。


![使用 ls -lrt 命令](/data/attachment/album/202310/08/200244a0tiz96o5zo7mnt0.png)


### 让我们来总结一下至此你已经学习到的！


下面，我会共享一个表列出本教程中介绍过的一些与 `ls` 命令一起使用的选项：




| 命令 | 描述 |
| --- | --- |
| `ls -l` | 长格式列出文件和目录 |
| `ls -lh` | 以人类可读的方式显示信息 |
| `ls -a` | 在列出的内容中包括隐藏文件 |
| `ls -R` | 递归列出文件 |
| `ls -F` | 在目录名后添加正斜杠 |
| `ls *.ext` | 列出具有特定扩展名的文件 |
| `ls -lS` | 根据文件大小排序 |
| `ls -lt` | 根据修改时间排序 |
| `-r` | 反转排序顺序（与 `-S` 或 `-t` 结合使用） |


### ?️ 并练习你所学


实践你所学总是一个好主意，这就是我们在每一份终端指南中都试图添加实践部分的原因。


所以，这里有一些关于 `ls` 命令的简单练习：


* 列出 `/var/log` 的内容
* [将命令的输出](https://itsfoss.com/save-command-output-to-file-linux/) 保存到名为 `output.txt` 的文件中
* 找出最近的 3 个文件（使用基于时间的排序）
* 基于大小显示文件，但排序方式要反转
* 检查是否存在任何隐藏文件


这些练习对你来说会很有帮助。期待之后更多关于 Linux 命令的学习。


如果你是初次接触终端，别忘了关注我们的终端基础系列。


祝你日有进益 ?


*（题图：MJ/8fb35776-5192-43ca-b96d-31bbd77c3318）*




---


via: <https://itsfoss.com/ls-command/>


作者：[Sagar Sharma](https://itsfoss.com/author/sagar/) 选题：[lkxed](https://github.com/lkxed/) 译者：[ChatGPT](https://linux.cn/lctt/ChatGPT) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
![Warp Terminal](/assets/images/warp-terminal.webp)


![Warp Terminal](/assets/images/warp-terminal.webp)

In my opinion, the ls command is the [most used Linux command](https://itsfoss.com/essential-ubuntu-commands/) as it is often used to verify the outcome of the previous operation by [listing the directory contents](https://itsfoss.com/list-directory-content/).

The ls command stands for a list; all it does is list the contents of the specified directory.

In this tutorial, I'll walk you through multiple examples of using the ls command.

I will also share some practice questions to test the learnings.

## How to use the ls command

To use the ls command, you'd have to follow the simple command syntax:

`ls [OPTIONS] Targeted_Directory`


Here,

`[OPTIONS]`

: It is used to tweak the default behavior of the ls command.`Targeted_Directory`

: This is where you provide the directory name or the absolute path to the directory.

You might wonder what happens when you use the ls command without any options. And to answer that, I used the ls command in my current working directory:

`ls `


![use the ls command to list the contents of the working directory](https://itsfoss.com/content/images/2023/07/use-the-ls-command-to-list-the-contents-of-the-working-directory.png)

As you can see, it listed all the directories and files available in the current working directory. But you can do a lot more than just getting the names and files present in the current working directory.

Let me show you how.

### 1. List files and directories with ownership

One of the primary use of the ls command to find the permissions and ownership of a particular file or a directory.

For that you'd have to use the `-l`

option (also called long listing) with the ls command:

`ls -l`


Once you do that, you can expect similar output:

```
sagar@itsFOSS:~$ ls -lh
-rwxrw-r-- 1 sagar sagar 666M Dec 10 18:16 Fedora.iso
```


Notice how I used an addition `-h`

option? I'll discuss it in the next section.

For now, if the output looks too complicated, then let me simplify things for you:

![explain read write permissions with file ownership in Linux using the ls command](https://itsfoss.com/content/images/2023/07/explain-read-write-permissions-in-linux-filesystem.png)

As you can see, each text block has its own meaning such as the permissions for owner, group, and others.

If you want to dive deep into file permissions, I would recommend [our detailed guide on file permissions in Linux:](https://linuxhandbook.com/linux-file-permissions/)

[Linux File Permissions and Ownership Explained with ExamplesLinux file permissions explained in simpler terms. Also learn how to change the file permissions and ownership in Linux in this detailed beginner’s guide.](https://linuxhandbook.com/linux-file-permissions/)![](https://linuxhandbook.com/content/images/2020/06/linux-file-permission-featured.jpeg)


![](https://linuxhandbook.com/content/images/2020/06/linux-file-permission-featured.jpeg)

### 2. Get information in a human-readable format

By default, the file size is shown in bytes which is not the best way to know your file size. So how do you get the same info but in human-readable form?

Simple. You use the `-h`

option with the ls command:

`ls -l -h`


And here's the comparison between the default and the human-readable form:

![list files using the ls command in human readable format](https://itsfoss.com/content/images/2023/07/list-files-using-the-ls-command-in-human-readable-format.png)

Much better. Isn't it?

The ls command is good to see the file sizes. However, it won't give you the directory size which is almost always displayed as 4K. To get directory size, use the du command.

### 3. List hidden files

Like any other file manager, the ls command won't list the hidden files (I mean they are supposed to be hidden. Right?).

But what if you want to [list the hidden files](https://itsfoss.com/show-hidden-files-linux/) along with the regular files? To do so, you can use the `-a`

option:

`ls -a`


As you can see, the filename starting with a dot `.`

are hidden files.

![list hidden files in Linux terminal using the ls command](https://itsfoss.com/content/images/2023/07/list-hidden-files-in-Linux-terminal-using-the-ls-command.png)

You may also use `ls -A`

which works almost the same as `ls -a`

except it won't include the `.`

and `..`

directories.

### 4. List files recursively

There are [multiple ways to list files recursively](https://linuxhandbook.com/list-files-recursively/) and using the ls command is one of them.

In case you don't know, listing files recursively means listing files of all the sub-directories present until the last element of every sub-directory is shown.

And to list files recursively, you can use the `-R`

flag as shown:

`ls -R`


![list files recursilvely in Linux using the ls command](https://itsfoss.com/content/images/2023/07/list-files-recursilvely-in-Linux-using-the-ls-command.png)

In a way, it gives you the current directory structure. Personally, I prefer the for this purpose but you'll have to install it first.

You don't have to be in the directory to list its content. You can also list directory contents by providing its absolute or relative path like this: `ls /var/log`


### 5. Differentiate between files and directories while using ls

While the different colors for files and directories should do the job. But due to some reason, if you want to symbolize files and directories here you have it.

In the ls command, you have a `-F`

flag that adds a forward slash `/`

to every directory name:

`ls -F`


![differenciate between files and directories while using the ls command](https://itsfoss.com/content/images/2023/07/differenciate-between-files-and-directories-while-using-the-ls-command.png)

### 6. List only files with certain file extensions

There are times when you only want to list files with specific file extensions and trust me this is the easiest of all.

To do that, you don't have to use any options. Just append the file extension to the Asterisk `*`

such as `*.png`

, `*.txt`

, etc:

`ls *.extension`


For example, if I only want to list the ISO files, then, I will be using the following command:

`ls *.iso`


![list files of a specific file extensions](https://itsfoss.com/content/images/2023/07/list-files-of-a-specific-file-extensions.png)

### 7. Sort output based on size

To sort the output based on the file size, you'd have to use the `-S`

flag and it will list files from largest to the smallest manner (descending):

`ls -lhS`


![sort files based on their file size using the ls command](https://itsfoss.com/content/images/2023/07/sort-files-based-on-their-file-size-using-the-ls-command.png)

Similarly, if you want to reverse this order to list the smallest files first, you can use the `-r`

flag to reverse the order:

`ls -lhSr`


![sort files from smallest to largest using the ls command](https://itsfoss.com/content/images/2023/07/sort-files-from-smallest-to-largest-using-the-ls-command.png)

### 8. Sort files based on date and time

The ls commands includes the modified time in its listing.

To list the newest files first, you can use the `-t`

flag as shown:

`ls -lht`


![show newest files first while using the ls command](https://itsfoss.com/content/images/2023/07/show-newest-files-first-while-using-the-ls-command.png)

You can use the `-r`

flag as I explained previously to reverse the order here too.

`ls -lrt`


This will give you the latest modified files at the bottom of the display. This is particularly helpful if you have too many files in the directory and want to see which files were modified recently. I used this while troubleshooting my software project.

![Using ls -lrt command](https://itsfoss.com/content/images/2023/07/output_for_ls_-lrt-1.png)

## Let's summarize what you've learned so far!

Here, I will share a table with multiple options that were used with the ls command in this tutorial:

Command | Description |
---|---|

`ls -l` |
Long list of files and directories |

`ls -lh` |
Prints information in human-readable form |

`ls -a` |
Include hidden files in listing |

`ls -R` |
List files recursively |

`ls -F` |
Add a forward slash to the directory name |

`ls *.ext` |
List files having specific extensions |

`ls -lS` |
Sort files based on file size |

`ls -lt` |
Sort files based on time |

`-r` |
Reverse the sorting (combined with S or t) |


## 🏋️And practice your learning

Practicing what you've learned is always a good idea, which is why we try to add a practice section in each terminal guide.

So here are some simple practice exercise of the ls command:

- List the contents of the
`/var/log`

[Save the command output](https://itsfoss.com/save-command-output-to-file-linux/)in a file named output.txt- Identify the 3 most recent files (Use time based sorting)
- Display the files based on their size but in reverse order
- Check if there are any hidden files

That would be good practice for you. Stay tuned for more Linux command learning.

And if you are new to the terminal, don't forget to follow our Terminal Basics series

[Linux Command Tutorials for Absolute BeginnersNever used Linux commands before? No worries. This tutorial series is for absolute beginners to the Linux terminal.](https://itsfoss.com/tag/terminal-basics/)![](https://itsfoss.com/content/images/2023/03/terminal-basics-series-tag.png)


![](https://itsfoss.com/content/images/2023/03/terminal-basics-series-tag.png)

Enjoy :)