如何在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
ls in a terminal window

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

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

ls -1
ls -1 in a terminal window

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
ls Help gc_help in a terminal window

When ls has listed the contents of the first directory it lists the contents of the second. It prints the name of each directory as it processes them:

ls列出了第一个目录的内容时,它列出了第二个目录的内容。 在处理每个目录时,它会打印每个目录的名称:

Name of the directory being displayed by ls before the contents are listed.

使用文件模式 (Using File Patterns)

To selectively list a set of files, use pattern matching. The question mark “?” will represent any single character and the asterisk “*” will represent any string of characters. To list any files or directories that have names starting with “ip_” use this format:

要有选择地列出一组文件,请使用模式匹配。 问号“ ? ”将代表任何单个字符,而星号“ * ”将代表任何字符串。 要列出名称以“ ip_”开头的任何文件或目录,请使用以下格式:

ls ip_*
ls ip_* in a terminal window

To list files that have “.c” extensions, use this format:

要列出具有“ .c”扩展名的文件,请使用以下格式:

ls *.c
ls *.c in a terminal window

You can also use ls with grep , and use grep‘s pattern matching capabilities. Let’s look for any files that have the string “_pin_” in their name:

您还可以将lsgrep使用,并使用grep模式匹配功能。 让我们查找名称中包含字符串“ _pin_”的所有文件:

ls | grep _pin_
ls | grep _pin_ in a terminal window

This is almost the same as using ls on its own, with two wildcards:

这几乎与ls使用ls (带有两个通配符)相同:

ls | grep _pin_
ls *_pin_*
ls | grep _pin_ in a terminal window

Why almost the same? Note the different layouts. grep forces the output to a single filename per line format.

为什么几乎一样? 注意不同的布局。 grep强制输出为每行格式一个文件名。

非印刷字符 (Non-Printing Characters)

It is possible to find yourself with a filename that has a non-printing or control-character in its filename. Typically this can happen when you expand an archive you’ve downloaded from the web or retrieved a git repository, and the original author made a mistake creating a file but didn’t spot it.

可能会发现自己的文件名中包含非打印字符或控制字符。 通常,当您扩展从网络上下载的档案或检索git存储库时,原始作者创建文件时出错却没有发现它,这可能会发生。

Our weird file is one of these:

我们的怪异文件是其中之一:

Filename with control character in it in a terminal window

If we look at it in the file browser and press “F2” to rename it, the non-printing characters are represented by a strange symbol.

如果我们在文件浏览器中查看它,然后按“ F2”将其重命名,则非打印字符将由一个奇怪的符号表示。

Filename with a control characater in it, in the renaming dialog window

You can use the -b (escape) option to allow you to see what the file name actually contains. This option causes ls to use the escape sequences of the C programming language to represent the control-characters.

您可以使用-b (转义)选项来查看文件名实际包含的内容。 此选项使ls使用C编程语言的转义序列表示控制字符。

ls -b a*
ls -b a* in a terminal window

The mysterious character is revealed to be a newline character, represented in C as “\n.”

该神秘字符显示为换行符,用C表示为“ \ n”。

忽略文件 (Ignoring Files)

To have certain files omitted from a listing, use the --hide option. Suppose you don’t want to see the backup “.bak” files in the listing. You could use this command:

要使列表中省略某些文件,请使用--hide选项。 假设您不想在清单中看到备份的“ .bak”文件。 您可以使用以下命令:

ls
ls --hide=*.bak
ls --hide=*.bak in a terminal window

The “.bak” files are not included in the second listing.

“ .bak”文件不包含在第二个列表中。

长格式清单 (The Long Format Listing)

The -l (long listing) option causes ls to provide detailed information about each file.

-l (长列表)选项使ls提供有关每个文件的详细信息。

ls -l
ls -l in a terminal window

There’s a lot of information here, so let’s step through it.

这里有很多信息,所以让我们逐步进行。

The first thing ls displays is the total size of all the files in the listing. Then each file or directory is displayed on a line by itself.

ls显示的第一件事是清单中所有文件的总大小。 然后,每个文件或目录将单独显示在一行上。

The first set of ten letters and dashes are the file type and the owner, group and other file permissions.

第一组十个字母和破折号是文件类型以及所有者,组和其他文件权限。

The very first character represents the file type. It will be one of:

第一个字符代表文件类型。 它将是以下之一:

  • :  A regular file.

    :常规文件。

  • b: A block special file.

    b :块特殊文件。

  • c: A character special file.

    c :字符特殊文件。

  • d: A directory.

    d :目录。

  • l: A symbolic link.

    l :符号链接。

  • n: A network file.

    n :网络文件。

  • p: A named pipe.

    p :命名管道。

  • s: A socket.

    s :一个套接字。

The next nine characters are three groups of three characters displayed contiguously. Each group of three represent the read, write, and execute permissions, in that order. If the permission is granted, there will be an r, w, or x present. If the permission is not granted, a hyphen - is shown.

接下来的九个字符是连续显示的三组,每组三个字符。 每三个一组,分别代表读取,写入和执行权限。 如果授予该权限,则将出现rwx 。 如果未授予许可,则显示连字符-

The first set of three characters are the permissions for the file owner. The second set of three permissions are for group members, and the last set of three permissions is for others.

第一组三个字符是文件所有者的权限。 第二组三个权限适用于组成员,最后一组三个权限适用于其他权限。

Sometimes the execution permission for the owner is represented by an s. This is the setuid bit. If it is present, it means that the file is executed with the privileges of the file owner, not the user executing the file.

有时,所有者的执行许可用s表示。 这是setuid位。 如果存在,则表示该文件是使用文件所有者(而不是执行文件的用户)的特权执行的。

The execution permission for the group can also be an s.  This is the setgid bit. When this is applied to a file, it means the file will be executed with the privileges of the ower’s group. When used with a directory, any files created inside it will take their group permissions from the directory they’re being created in, not from the user who is creating the file.

的执行许可也可以是s 。 这是setgid位。 将其应用于文件时,这意味着将使用所有者组的特权来执行文件。 当一个目录使用,它里面创建的任何文件都将采取他们的组权限从他们在被创建的目录,而不是谁在创建该文件的用户。

The execution permission for the others can sometimes be represented by a t. This is the sticky bit. It is usually applied to directories. If this is set, regardless of the write and executable privileges that are set on the files in the directory, only the file owner, the directory owner, or the root user can rename or delete files in the directory.

其他权限的执行权限有时可以用t表示。 这是粘性的位。 它通常应用于目录。 如果设置了此选项,则无论在目录中的文件上设置了写和可执行特权,只有文件所有者,目录所有者或root用户都可以重命名或删除目录中的文件。

A common use for the sticky bit is on folders such as “/tmp”. This is writable by all users on the computer. The sticky bit on the directory ensures that users—and processes launched by the users—can only rename or delete their own temporary files.

粘性位的常见用法是在文件夹“ / tmp”上。 这是计算机上的所有用户均可写的。 目录中的粘性位可确保用户以及用户启动的进程只能重命名或删除自己的临时文件。

We can see the sticky bit on the “/tmp” directory. Note the use of the -d (directory) option. This causes ls to report on the details of the directory. Without this option, ls will report on the files inside the directory.

我们可以在“ / tmp”目录中看到粘性位。 注意-d (目录)选项的使用。 这将导致ls报告目录的详细信息。 如果没有此选项, ls将报告目录中的文件。

ls -l -d /tmp
ls -l -d /tmp in a terminal window

The number following the permissions is the number of hard links to the file or directory. For a file, this is usually one, but if other hard links are created, this number will increase. A directory typically has at least two hard links. One is a link to itself, and the other is its entry in its parent directory.

权限后的数字是指向文件或目录的硬链接的数量。 对于文件,通常是一个,但是如果创建了其他硬链接,则此数目将增加。 一个目录通常至少具有两个硬链接。 一个是指向自身的链接,另一个是其在父目录中的条目。

The name of the owner and group are displayed next. They are followed by the file size and the date of the last modification of the file. Finally, the filename is given.

接下来显示所有者和组的名称。 它们后面是文件大小和文件的最后修改日期。 最后,给出文件名。

人类可读的文件大小 (Human Readable File Sizes)

Having the file sizes in bytes is not always convenient. To see the file sizes in the most appropriate units (Kilobytes, Megabytes, etc.) use the -h (human-readable) option:

以字节为单位的文件大小并不总是很方便。 要以最合适的单位(千字节,兆字节等)查看文件大小,请使用-h (人类可读)选项:

ls -l -h
ls -l -h in a terminal window

显示隐藏文件 (Showing Hidden Files)

To see hidden files, use the -a (all) option:

要查看隐藏的文件,请使用-a (所有)选项:

ls -l -a
ls -l -a in a terminal window

The two entries “.” and “..” represent the current directory and the parent directory, respectively. A file called “.base_settings” is now visible for the first time.

两个条目“。” “ ..”和“ ..”分别代表当前目录和父目录。 现在首次显示名为“ .base_settings”的文件。

省略。 和..来自列表 (Omitting . and .. from Listings)

If you don’t want your listing cluttered up with the “.” and “..” entries, but you do want to see hidden files, use the -A (almost all) option:

如果您不希望列表中出现“。” 和“ ..”条目,但是您确实想查看隐藏的文件,请使用-A (几乎所有)选项:

ls -l -A
ls -l -A in a terminal window

The hidden file is still listed, but the “.” and “..” entries are suppressed.

隐藏文件仍然列出,但“。” 和“ ..”条目被禁止。

递归列出目录 (Listing Directories Recursively)

To have ls list the files in all subdirectories use the -R (recursive) option

要使ls列出所有子目录中的文件,请使用-R (递归)选项

ls -l -R
ls -l -R in a terminal window

ls works its way through the entire directory tree below the starting directory, and lists the files in each subdirectory.

ls遍历起始目录下的整个目录树,并列出每个子目录中的文件。

output from ls recursively listing directories

显示UID和GID (Displaying the UID and GID)

To have the user ID and group ID displayed instead of the user name and group name, use the -n (numeric uid and gid) option.

要显示用户ID和组ID而不是用户名和组名,请使用-n (数字uid和gid)选项。

ls -n
ls -n in a terminal window

排序列表 (Sorting The Listings)

You can sort the listing by extension, file size, or modification time. These options don’t have to be used with the long listing format, but it usually makes sense to do so. If you’re sorting by file size, it makes sense to see the file sizes in the listing. When you’re sorting by extension type, the long listing format isn’t so important.

您可以按扩展名,文件大小或修改时间对列表进行排序。 这些选项不必与长列表格式一起使用,但是这样做通常是有意义的。 如果您按文件大小排序,则在列表中查看文件大小是有意义的。 当按扩展名类型排序时,长列表格式不是很重要。

To sort by extension, use the -X (sort by extension) option.

要按扩展名排序,请使用-X (按扩展名排序)选项。

ls -X -1
ls -X -1 in a terminal window

The directories are listed first (no extensions at all) then the rest follow in alphabetical order, according to the extensions.

根据扩展名,目录首先列出(根本没有扩展名),然后其余按照字母顺序列出。

To sort by file size, use the -S (sort by file size) option.

要按文件大小排序,请使用-S (按文件大小排序)选项。

ls -l -h -S
ls -l -h -S in a terminal window

The sort order is largest to smallest.

排序顺序从大到小。

To sort the listing by modification time, use the -t (sort by modification time) option.

要按修改时间对列表进行排序,请使用-t (按修改时间排序)选项。

ls -l -t
ls -l -t in a terminal window

The listing is sorted by the modification time.

该列表按修改时间排序。

If the file modification time is within the current year, the information displayed is the month, day, and time. If the modification date was not in the current year, the information that is displayed is the month, day, and the year.

如果文件修改时间在当前年份之内,则显示的信息是月份,日期和时间。 如果修改日期不是当前年份,则显示的信息是月份,日期和年份。

A quick way to get the newest and oldest files in a directory is to use ls with the head and tail commands.

获取目录中最新和最旧文件的快速方法是将lsheadtail命令一起使用。

To get the newest file or directory, use this command:

要获取最新的文件或目录,请使用以下命令:

ls -t | head -1

To get the oldest file or directory, use this command:

要获取最早的文件或目录,请使用以下命令:

ls -t | tail -1
ls -t | head -1 in a terminal window

颠倒排序顺序 (To Reverse the Sort Order)

To reverse any of the sort orders, use the -r (reverse) option.

要反转任何排序顺序,请使用-r (反转)选项。

ls -l -h -S -r
ls -l -h -S -r in a terminal window

The listing is now ordered from the smallest file to the largest file.

现在按最小文件到最大文件的顺序排列列表。

还有更多 (And there’s more)

Check out the man page for lsthere are many more options. Some of them satisfy somewhat obscure use cases, but once in a while, you’ll be glad you know about them.

查阅ls的手册页,还有更多选择。 他们中的一些人满足了一些晦涩的用例,但是不时地,您会很高兴了解它们。

Do you need to see the file timestamps with the maximum precision that Linux can provide? Use the full-time option:

您是否需要查看Linux可以提供的最高精度的文件时间戳? 使用全职选项:

ls --full-time

Perhaps you want to see the inode number of the files? Use the inode option:

也许您想查看文件的索引节点号? 使用inode选项:

ls -i

Are you working on a monochrome display and want to remove all risk of confusing files for directories and links? Use the classify option, and ls will append one of these to each listing entry:

您是否正在使用单色显示器,并希望消除混淆目录和链接文件的所有风险? 使用分类选项, ls将其中之一附加到每个列表条目:

  • /: A directory.

    / :目录。

  • @: A symlink.

    @ :符号链接。

  • |: A named pipe.

    | :命名管道。

  • =: A socket.

    = :一个套接字。

  • *: An executable files

    * :可执行文件

ls -F

Do some digging. You’ll find that ls is a rich vein, and you’ll keep turning up gems.

做一些挖掘。 您会发现ls很丰富,并且会不断出现宝石。

翻译自: https://www.howtogeek.com/448446/how-to-use-the-ls-command-on-linux/

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值