Linux/UNIX中的ls命令

ls 命令是日常 Linux/UNIX 操作中最常用的命令之一. 命令用于列出目录中的内容,是初学者从开始学习的少数命令之一. 在本指南中,我们将讨论 Linux 中的 Common ls 命令以及可能与命令一起使用的其他参数。

用 ls 命令列出文件,没有任何参数

ls命令没有任何选项列出文件和目录在一个简单的格式,而不显示很多信息,如文件类型,权限,更改的日期和时间,只是几个。

1$ ls

ls command in Linux/UNIX

以反向顺序列出文件

若要以反向顺序列出文件,请按 Syntax 显示的 -r 标志。

1$ ls -r

common ls commands in linux As you can see above, the order of the listing has changed from the last to the first in comparison to the previous image.

使用 -l 选项列出文件和目录权限

使用 -l 旗,可以列出文件和目录的权限以及其他属性,如文件名、文件和目录大小以及修改日期和时间。

1$ ls -l

ls commands in Linux

在人类可读格式中查看文件

正如您可能已经注意到的,显示的文件和文件夹大小并不容易解读,并在第一眼中有意义。 为了轻松地识别文件大小为千兆字节(kB),千兆字节(MB)或千兆字节(GB),请附上显示的 -lh 旗帜 Syntax

1$ ls -lh

common ls commands in Linux

查看隐藏的文件

您可以通过附加 -a 标签来查看隐藏的文件. 隐藏的文件通常是以完全停止或期开始的系统文件。

1$ ls -a

重复性列出文件

要显示文件和文件夹的目录树,请使用 ls -R 命令,如显示 Syntax

1$ ls -R

common ls commands in Linux

列出文件和目录,结尾有/字符

如果您想继续进一步区分文件和文件夹,请使用 -F 标志,以便文件夹末尾出现前进的/字符。

1$ ls -F

common ls commands in Linux

显示 inode 文件和目录的数量

要显示 inode 文件和目录的数量,请在 ls 命令的末尾附上-i旗,如 Syntax所示。

1$ ls -i

common ls commands in Linux

显示文件和目录的 UID 和 GID

如果您想显示文件和目录的 UID 以及 GId,请附加显示的 -n 参数 ** Syntax**

1$ ls -n

common ls commands in Linux

ls 命令在 aliases 中定义

姓名是Linux壳中的自定义或修改的命令,这些命令被用来代替原始命令。

1$ alias="ls -l"

What this does is that it tells the system to execute the ls -l command instead of the ls command. Be sure to observe that the output you get when running the ls command thereafter, will be as though you run the ls -l command. To remove the added alias, run

1unalias ls

彩色 ls 指令输出

要根据文件类型为输出显示器添加一些风格,您可能需要颜色化输出,以便轻松区分文件,文件夹和其他属性,如文件和目录权限。

1ls --color

显示 ls 命令版本

如果你对你正在运行哪个版本的 ls 有点好奇,请执行下面的命令

1# ls --v
2ls (GNU coreutils) 8.22
3Copyright (C) 2013 Free Software Foundation, Inc.
4License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>.
5This is free software: you are free to change and redistribute it.
6There is NO WARRANTY, to the extent permitted by law.
7
8Written by Richard M. Stallman and David MacKenzie.
9#

您还可以执行命令 ls --version 来打印 ls 命令版本。

ls 命令帮助页面

要查看更多选项和你可以用 ls 做的事情,只需运行]

1ls --help

common ls commands in Linux

访问男人的页面

或者,您可以查看 manpages 以通过运行来了解其使用的更多信息。

1man ls

common ls commands in Linux That's all we had for you today. We hope at this point, you will be more comfortable using the ls command in your day to day operations. Feel free to weigh in your feedback. Thanks!

Published At
Categories with 技术
Tagged with
comments powered by Disqus