Linux ls命令教程列出文件和目录

ls is base command provided by all Linux distributions. We can use ls command to list files, folders or directories. In this tutorial we will look different use cases about ls.

ls是所有Linux发行版提供的基本命令。 我们可以使用ls命令列出文件,文件夹或目录。 在本教程中,我们将介绍有关ls的不同用例。

列出文件和目录 (List Files and Directories)

We will start with a simple example. We will use ls command without an option. This will list regular files and directories of the current working path.

我们将从一个简单的例子开始。 我们将使用不带选项的ls命令。 这将列出当前工作路径的常规文件和目录。

$ ls
List Files and Directories
List Files and Directories
列出文件和目录

列出详细信息(List Detailed Information)

While listing files and directories we may have need more detailed information. We can list information like

在列出文件和目录时,我们可能需要更多详细信息。 我们可以列出类似的信息

  • User, group, other read, write,execute permissions

    用户,组,其他读取,写入,执行权限
  • User and group owner information

    用户和组所有者信息
  • Size

    尺寸
  • Last edit time

    上次编辑时间

We will use -l option to list detailed information

我们将使用-l选项列出详细信息

List Detailed Information
List Detailed Information
列出详细信息

列出相对路径(List Relative Path)

There is two type of path definition. Relative path means according to current working path. For example if I want to list “current working path+Downloads” I can use only Downloads like below.

路径定义有两种类型。 相对路径是指根据当前工作路径。 例如,如果我要列出“当前工作路径+下载”,则只能使用如下所示的Downloads

$ ls Downloads

$ ls下载

List Relative Path
List Relative Path
列出相对路径

列出绝对路径(List Absolute Path)

Absolute path definition means we will provide the whole directory structure from the / root. In this example we will list /home/ismail/Downloads .

绝对路径定义意味着我们将从/ root提供整个目录结构。 在此示例中,我们将列出/home/ismail/Downloads

$ ls /home/ismail/Downloads/
List Absolute Path
List Absolute Path
列出绝对路径

列出父目录(List Parent Directory)

ls command and bash provides some shortcuts about specifying the parent directory. We can use .. to specify parent directory and list files and folders in the parent directory.

ls命令和bash提供了一些有关指定父目录的快捷方式。 我们可以使用..指定父目录,并列出父目录中的文件和文件夹。

$ ls ..
List Parent Directory
List Parent Directory
列出父目录

列为有色(List As Colored)

If there is no specific configuration the ls command output will be black and white which is boring for the most of the users. We can list files and directories in a meaningful color with --color option.

如果没有特定的配置, ls命令的输出将是黑白的,这对于大多数用户来说很无聊。 我们可以使用--color选项以有意义的颜色列出文件和目录。

$ ls --color
List As Colored
List As Colored
列为有色

仅列出目录(List Only Directories)

We have the ability to list only directories. We can use -d option for this.

我们可以只列出目录。 我们可以使用-d选项。

$ ls -d

列出所有文件或隐藏文件 (List All Files or Hidden Files)

By default ls command will list only files, directories and links. But there is a special file type named hidden file. Hidden files generally used to store configuration or temporary data or cache. We can list these files with -a option which means all.

默认情况下, ls命令将仅列出文件,目录和链接。 但是有一种特殊的文件类型,称为隐藏文件。 隐藏文件通常用于存储配置或临时数据或缓存。 我们可以使用-a选项列出所有文件。

$ ls -a
List All Files or Hidden Files
List All Files or Hidden Files
列出所有文件或隐藏文件

按日期和时间排序(Sort By Date and Time)

Files and directories created, changed during time. These date and time information is stored as attribute. We can list file and directories according to data time with the -t option like below.

创建的文件和目录,随时间更改。 这些日期和时间信息存储为属性。 我们可以使用-t选项根据数据时间列出文件和目录,如下所示。

$ ls -t -l
Sort By Date and Time
Sort By Date and Time
按日期和时间排序

按大小排序(Sort By Size)

Another sort parameter is the size. We can sort files and folders according to their size. But keep in mind that all directories  will be the same size which is 4096.

另一个排序参数是大小。 我们可以根据文件和文件夹的大小对其进行排序。 但是请记住,所有目录的大小均相同,即4096。

$ ls -s

递归列出 (List Recursively)

We can also list directories contents recursively. Recursively means looking child directories and files of all given path for multilevel. We will us -R option for this.

我们还可以递归列出目录内容。 递归意味着要在所有给定路径的子目录和文件中查找多层。 我们将为此使用-R选项。

$ ls -R
List Recursively
List Recursively
递归列出

列出特定文件扩展名(List Specific File Extension)

ls command also accept file and directory names to filter. We generally use * glob as file name or the dynamic side of the file. In this example we list files ends with tar.gz .

ls命令还接受要过滤的文件和目录名称。 我们通常使用* glob作为文件名或文件的动态端。 在此示例中,我们列出了以tar.gz结尾的文件。

$ ls *.tar.gz
LEARN MORE  How To Linux Bash For Loop In Files?
了解更多信息如何在Linux Bash中循环文件?

翻译自: https://www.poftut.com/linux-ls-command-tutorial-to-list-files-and-directories/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值