如何在Linux上使用ls命令列出文件和目录

A terminal window on a Linux laptop. Fatmawati Achmad Zaenuri/Shutterstock Fatmawati Achmad Zaenuri / Shutterstock

We use the Linux ls command every day without thinking about it. That’s a pity. Pay it some attention, and you’ll find many useful options—including some you should add to your command-line arsenal.

我们每天都在使用Linux ls命令,而无需考虑它。 太可惜了。 注意一下,您会发现许多有用的选项,包括一些您应该添加到命令行库中的选项。

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

The ls command is probably the first command most Linux users encounter. Those of us who hang around the command line use it day in and day out without even thinking about it. That might explain why there is more to this command than most users realize. We list files with it to see what’s in a directory. We list files in long format when we want to look at the permissions on a file. Beyond that, it gets little consideration.

ls命令可能是大多数Linux用户遇到的第一个命令。 我们中那些徘徊在命令行中的人日复一日地使用它,甚至没有考虑它。 这也许可以解释为什么此命令比大多数用户意识到的更多。 我们用它列出文件以查看目录中的内容。 当我们想查看文件的权限时,我们以长格式列出文件。 除此之外,它几乎没有考虑。

The ls command is one of those commands with a wealth of options. Perhaps this is part of the problem. There are so many options, how do you sift through them to find the useful ones? And having found them, how do you remember them?

ls命令是具有众多选项的命令之一。 也许这是问题的一部分。 有这么多种选择,您如何筛选它们以找到有用的选择? 找到它们之后,您如何记住它们?

Useful permutations of the ls command with their strings of options and parameters are the perfect candidates for aliases. In fact, in most distributions, what you think of as the “naked” ls command is actually an alias. Amongst other things, the type command can be used to show the underlying definition of aliases. Let’s look at the definition of ls:

使用ls命令的选项字符串和参数字符串进行有用的排列是别名的理想选择。 实际上,在大多数发行版中,您认为“裸” ls命令实际上是一个别名。 除其他外, type命令可用于显示别名的基础定义。 让我们看一下ls的定义:

type ls

输入ls

type ls in a terminal window

The --color=auto parameters are included automatically every time you use the ls command. This is what provides the different colors for the different file types in the listings.

每次使用ls命令时,都会--color=auto包含--color=auto参数。 这就是为清单中的不同文件类型提供不同颜色的原因。

简单的ls清单 (Simple ls Listings)

Everyone who’s spent some time using the Linux terminal knows that, by default, ls lists the files and directories in the current directory.

在Linux终端上花了一些时间的每个人都知道,默认情况下, ls列出当前目录中的文件和目录。

ls
如何在Linux上使用ls命令列出文件和目录_第1张图片

If you want to have your listing produced ina single column, use the -1 (one file per line) option:

如果要在单列中显示清单,请使用-1 (每行一个文件)选项:

ls -1
如何在Linux上使用ls命令列出文件和目录_第2张图片

We’ll discuss that weird-looking filename at the top of the listing in a minute.

一分钟后,我们将在列表顶部讨论该看起来怪异的文件名。

在不同目录上使用ls (Using ls on Different Directories)

To have ls list the files in a directory other than the current directory, pass the path to the directory to ls on the command line. You can also pass more than one directory to ls, and have them listed one after the other. Here, we’re asking ls to list the files in two directories, one called “Help” and the other called “gc_help.”

要使ls列出当前目录以外的目录中的文件,请在命令行中将目录的路径传递给ls 。 您还可以将多个目录传递给ls ,并将它们一个接一个地列出。 在这里,我们要求ls在两个目录中列出文件,一个目录称为“帮助”,另一个目录称为“ gc_help”。

ls Help gc_help

你可能感兴趣的:(linux,java,python,shell,大数据)