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 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 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 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 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 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 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

按日期和时间排序(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 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 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
翻译自: https://www.poftut.com/linux-ls-command-tutorial-to-list-files-and-directories/