ls命令了,带你重新认识linux查看文件信息的ls【内涵长文,非命令参数罗列!】
linux极简小知识:40、文件管理中最重要的ls命令,及文件类型、文件权限标识【附带了解pwd命令】
本篇是 linux极简小知识 系列的第40篇,介绍真实有用的linux知识,欢迎阅读查看!
ls 用于查看linux中的文件信息。之所以说它是最重要的命令,原因在于在整个 Unix(或linux)的哲学中,核心就是“一切皆文件!”
驱动、终端、网卡、显示器、硬件设备、软件程序等,在linux中,都是文件。知道了如何查看文件,就知道了如何深入了解linux。
比如,打印或查看当前的工作目录:
[root_test@VM_0_15_centos test]$ pwd /home/root_test/test 复制代码
pwd 命令
pwd (print work directory
),直译就是打印工作目录,即显示当前的目录、显示当前命令行操作的位置!
ls 命令
ls (list files
)列出当前目录下的文件,用于查看指定目录下都有哪些文件及文件信息。
可以查看到文件的名称、类型、权限、所属用户和组、修改时间等各种信息。
ls 查看当前目录下的文件
[root_test@VM_0_15_centos ~]$ ls axel-2.4-9.el7.x86_64.rpm test Y6kkd1d 复制代码
ls 查看指定目录下的文件
ls 后跟着一个目录,即表示查看这个目录下的文件。
比如,查看根目录下的文件 ls /
。
查看 /etc/my.cnf.d
目录中的文件。
[root_test@VM_0_15_centos ~]$ ls /etc/my.cnf.d hostname xzdiff xzgrep 复制代码
注,
ls
列出来的文件,不同的颜色,表示不同的权限!
ls查看多个目录或文件
ls后面可以通过空格跟着多个文件或目录。
如下,查看当前目录、根目录、root用户目录、my.txt文件。
[root_test@VM_0_15_centos ~]$ ls ./ / /root test/my.txt test/my.txt /: bin data etc lib lost+found mnt proc run srv sys usr boot dev home lib64 media opt root sbin swap_file tmp var ./: axel-2.4-9.el7.x86_64.rpm test Y6kkd1d ls: cannot open directory /root: Permission denied 复制代码
ls -l
查看长列表形式的文件
ls -l
中 -l
表示长列表格式(long listing format
)。
使用该命令,可以以列表且内容更详细的形式查看文件。
$ ls -l total 64 -rw-rw-r-- 1 root_test root_test 54908 Apr 16 2014 axel-2.4-9.el7.x86_64.rpm drwxrwxr-x 2 root_test root_test 4096 Oct 26 21:52 test -rw-rw-r-- 1 root_test root_test 0 Sep 3 17:40 Y6kkd1d 复制代码
ls -l 长列表格式详解
通过执行命令 ls -l
显示的长列表格式的文件信息,包含着文件有关的更多内容。
我们以下面两行为例:
-rw-rw-r-- 1 root_test root_test 54908 Apr 16 2014 axel-2.4-9.el7.x86_64.rpm drwxrwxr-x 2 root_test root_test 4096 Oct 26 21:52 test 复制代码
格式详解
长列表格式的输出在每一行中列出了单个文件或目录。不仅有文件名,还包括文件的类型(属性)、大小、权限等详细信息:
第一行显示了在目录中文件占用的总块数(文件系统块数
the number of file system blocks
)。表示的是目录下所有文件的大小。之后的每一行显示该文件的长格式信息。
长格式信息一个分为7大列,10小项。
file mode【文件模式】 | number of links【链接数】 | owner name【所属用户名】 | group name【所属组名】 | number of bytes in the file【文件字节数】 | The date of last modification【组后修改的日期】 | filename【文件名】 | |||
---|---|---|---|---|---|---|---|---|---|
File Type[文件类型] | 所有者权限 | 所属组的权限 | 其他用户权限 | 链接数 | 所有者 | 所属群组 | 文件的字节大小 | 最后修改时间 | 文件名 |
- | rw- | rw- | r-- | 1 | root_test | root_test | 54908 | Apr 16 2014 | axel-2.4-9.el7.x86_64.rpm |
d | rwx | rwx | r-x | 2 | root_test | root_test | 4096 | Oct 26 21:52 | test |
第一列共10位,第1位表示文档类型,d表示目录,-表示普通文件。后9位,依次对应三种身份所拥有的权限,身份顺序为:owner、group、others,权限顺序为:readable、writable、excutable,即可读、可写、可执行权限。如:-rwxr-xr--的含义为,当前文档是一个普通文件,所属用户可读、可写、可执行,同一个群组下的用户,可读、可执行,其他人只有可读取的权限。
第二列表示链接数,表示有多少个文件链接到inode号码。它表示的是硬链接数。
硬链接数并不表示实际的文件数。
硬链接(
hard link
, 也称链接)是指通过索引节点来进行的链接。在Linux的文件系统中,保存在磁盘分区中的文件不管是什么类型都会给它分配一个编号,这个编号被称为索引节点编号(
inode number
)。【关于inode参见最后一部分附:关于 inode 和 ls -li的介绍】
第三列表示拥有者,即文件所属用户
第四列表示所属用户组。
第五列表示文档容量大小,单位字节。
第六列表示文档最后修改时间,注意不是文档的创建时间!包括缩写的月(
abbreviated month
)、日、小时、分钟等。第七列表示文档名称。以点 . 开头的是隐藏文件
关于第一行的文件块数和-k
选项的问题
第一行显示的文件大小,默认的单位是 512 byte,除非指定 -k
,指定选项 -k
将以 1024 byte(1kB blocks) 为单位。
该内容参考自 What does the first line "Total" indicate while executing the ls -l command in Linux? [closed],并且 POSIX文档 中也是这么说的。
可实际测试,在加 -k
和不加 -k
选项的情况下,Total显示的数字是一样的,具体原因未找到相关资料,不知是否是用法不对。
不加 -k
选项为 64:
$ ls -l total 64 -rw-rw-r-- 1 root_test root_test 54908 Apr 16 2014 axel-2.4-9.el7.x86_64.rpm drwxrwxr-x 2 root_test root_test 4096 Oct 26 21:52 test -rw-rw-r-- 1 root_test root_test 0 Sep 3 17:40 Y6kkd1d 复制代码
加 -k
选项仍为 64:
$ ls -lk total 64 -rw-rw-r-- 1 root_test root_test 54908 Apr 16 2014 axel-2.4-9.el7.x86_64.rpm drwxrwxr-x 2 root_test root_test 4096 Oct 26 21:52 test -rw-rw-r-- 1 root_test root_test 0 Sep 3 17:40 Y6kkd1d 复制代码
使用 --block-size
选项设置第一行显示的块大小
要查看不同单位的块大小的显示,可以使用 --block-size
选项。
ls -l --block-size=M
将会以近似 MiB 的文件大小显示。如果先要使用 MB (10^6 bytes) 而不是 MiB (2^20 bytes) 单位,可以使用 --block-size=MB
代替。
实际测试
--block-size=B
和--block-size=MB
看不出什么区别。
$ ls -l --block-size=M total 1M -rw-rw-r-- 1 root_test root_test 1M Apr 16 2014 axel-2.4-9.el7.x86_64.rpm drwxrwxr-x 2 root_test root_test 1M Oct 26 21:52 test -rw-rw-r-- 1 root_test root_test 0M Sep 3 17:40 Y6kkd1d $ ls -l --block-size=k total 64K -rw-rw-r-- 1 root_test root_test 54K Apr 16 2014 axel-2.4-9.el7.x86_64.rpm drwxrwxr-x 2 root_test root_test 4K Oct 26 21:52 test -rw-rw-r-- 1 root_test root_test 0K Sep 3 17:40 Y6kkd1d 复制代码
而且,还可以使用任何你想要的给定的数字单位,如 --block-size=1M
、--block-size=2k
。
$ ls -l --block-size=2k total 32 -rw-rw-r-- 1 root_test root_test 27 Apr 16 2014 axel-2.4-9.el7.x86_64.rpm drwxrwxr-x 2 root_test root_test 2 Oct 26 21:52 test -rw-rw-r-- 1 root_test root_test 0 Sep 3 17:40 Y6kkd1d 复制代码
关于 -h
选项(human readable)
ls -lh
可以获得长格式列表和 人类可读(human readable
) 的文件大小。
$ ls -lh total 64K -rw-rw-r-- 1 root_test root_test 54K Apr 16 2014 axel-2.4-9.el7.x86_64.rpm drwxrwxr-x 2 root_test root_test 4.0K Oct 26 21:52 test -rw-rw-r-- 1 root_test root_test 0 Sep 3 17:40 Y6kkd1d 复制代码
ls -s 查看磁盘使用情况
$ ls -s total 64 60 axel-2.4-9.el7.x86_64.rpm 4 test 0 Y6kkd1d 复制代码
ls -a 显示隐藏文件和文件夹,即指定目录下的所有文件
Linux中,以 .
开头的文件或文件名就是隐藏文件。
默认 ls 查看文件时,并不会显示隐藏文件。可通过 -a
(--all
)选项查看所有文件。
$ ls --all . axel-2.4-9.el7.x86_64.rpm .bash_logout .bashrc .config .pki .viminfo .. .bash_history .bash_profile .cache .mozilla test Y6kkd1d $ ls axel-2.4-9.el7.x86_64.rpm test Y6kkd1d 复制代码
结合 -l
查看,ls -al
:
$ ls -al total 116 drwx------ 7 root_test root_test 4096 Oct 26 21:52 . drwxr-xr-x. 7 root root 4096 Apr 11 2018 .. -rw-rw-r-- 1 root_test root_test 54908 Apr 16 2014 axel-2.4-9.el7.x86_64.rpm -rw------- 1 root_test root_test 10633 Oct 28 14:04 .bash_history -rw-r--r-- 1 root_test root_test 18 Dec 7 2016 .bash_logout -rw-r--r-- 1 root_test root_test 193 Dec 7 2016 .bash_profile -rw-r--r-- 1 root_test root_test 231 Dec 7 2016 .bashrc drwxrwxr-x 3 root_test root_test 4096 Sep 2 11:36 .cache drwxrwxr-x 3 root_test root_test 4096 Sep 2 11:36 .config drwxr-xr-x 4 root_test root_test 4096 Jun 21 2019 .mozilla drwxrw---- 3 root_test root_test 4096 Sep 2 11:41 .pki drwxrwxr-x 2 root_test root_test 4096 Oct 26 21:52 test -rw------- 1 root_test root_test 1985 Sep 3 09:44 .viminfo -rw-rw-r-- 1 root_test root_test 0 Sep 3 17:40 Y6kkd1d 复制代码
ls -lr
通常不会直接使用 ls -r
而是要结合 -l
使用。
-r
表示逆向排序显示;默认是以文件名逆序显示的。
$ ls -r Y6kkd1d test axel-2.4-9.el7.x86_64.rpm $ ls axel-2.4-9.el7.x86_64.rpm test Y6kkd1d 复制代码
结合 -l
:
$ ls -l -r total 64 -rw-rw-r-- 1 root_test root_test 0 Sep 3 17:40 Y6kkd1d drwxrwxr-x 2 root_test root_test 4096 Oct 26 21:52 test -rw-rw-r-- 1 root_test root_test 54908 Apr 16 2014 axel-2.4-9.el7.x86_64.rpm 复制代码
-r -t
可以实现按时间进行逆序显示(ls -l -r -t
):
$ ls -lrt total 64 -rw-rw-r-- 1 root_test root_test 54908 Apr 16 2014 axel-2.4-9.el7.x86_64.rpm -rw-rw-r-- 1 root_test root_test 0 Sep 3 17:40 Y6kkd1d drwxrwxr-x 2 root_test root_test 4096 Oct 26 21:52 test 复制代码
以上所有命令都可以合并简写,如:ls -lr
,ls -lrt
。
ls -R 递归显示文件(所有的文件)
-R
用于递归显示文件夹中的文件。
$ ls -R .: axel-2.4-9.el7.x86_64.rpm test Y6kkd1d ./test: my.txt 复制代码
命令组合使用:
$ ls -alrtR .: total 116 -rw-rw-r-- 1 root_test root_test 54908 Apr 16 2014 axel-2.4-9.el7.x86_64.rpm -rw-r--r-- 1 root_test root_test 231 Dec 7 2016 .bashrc -rw-r--r-- 1 root_test root_test 193 Dec 7 2016 .bash_profile -rw-r--r-- 1 root_test root_test 18 Dec 7 2016 .bash_logout drwxr-xr-x. 7 root root 4096 Apr 11 2018 .. drwxr-xr-x 4 root_test root_test 4096 Jun 21 2019 .mozilla drwxrwxr-x 3 root_test root_test 4096 Sep 2 11:36 .cache drwxrwxr-x 3 root_test root_test 4096 Sep 2 11:36 .config drwxrw---- 3 root_test root_test 4096 Sep 2 11:41 .pki -rw------- 1 root_test root_test 1985 Sep 3 09:44 .viminfo -rw-rw-r-- 1 root_test root_test 0 Sep 3 17:40 Y6kkd1d drwx------ 7 root_test root_test 4096 Oct 26 21:52 . drwxrwxr-x 2 root_test root_test 4096 Oct 26 21:52 test -rw------- 1 root_test root_test 10777 Oct 28 14:10 .bash_history ./.mozilla: total 16 drwxr-xr-x 2 root_test root_test 4096 Jun 10 2014 plugins drwxr-xr-x 2 root_test root_test 4096 Jun 10 2014 extensions drwxr-xr-x 4 root_test root_test 4096 Jun 21 2019 . drwx------ 7 root_test root_test 4096 Oct 26 21:52 .. ./.mozilla/plugins: total 8 drwxr-xr-x 2 root_test root_test 4096 Jun 10 2014 . drwxr-xr-x 4 root_test root_test 4096 Jun 21 2019 .. ./.mozilla/extensions: total 8 drwxr-xr-x 2 root_test root_test 4096 Jun 10 2014 . drwxr-xr-x 4 root_test root_test 4096 Jun 21 2019 .. ./.cache: total 12 drwxrwxr-x 3 root_test root_test 4096 Sep 2 11:36 . drwx------ 7 root_test root_test 4096 Oct 26 21:52 .. drwxrwxr-x 2 root_test root_test 4096 Oct 28 10:17 abrt ./.cache/abrt: total 12 drwxrwxr-x 3 root_test root_test 4096 Sep 2 11:36 .. -rw------- 1 root_test root_test 11 Oct 28 10:17 lastnotification drwxrwxr-x 2 root_test root_test 4096 Oct 28 10:17 . ./.config: total 12 drwxrwxr-x 2 root_test root_test 4096 Sep 2 11:36 abrt drwxrwxr-x 3 root_test root_test 4096 Sep 2 11:36 . drwx------ 7 root_test root_test 4096 Oct 26 21:52 .. ./.config/abrt: total 8 drwxrwxr-x 3 root_test root_test 4096 Sep 2 11:36 .. drwxrwxr-x 2 root_test root_test 4096 Sep 2 11:36 . ./.pki: total 12 drwxrw---- 2 root_test root_test 4096 Sep 2 11:41 nssdb drwxrw---- 3 root_test root_test 4096 Sep 2 11:41 . drwx------ 7 root_test root_test 4096 Oct 26 21:52 .. ./.pki/nssdb: total 8 drwxrw---- 3 root_test root_test 4096 Sep 2 11:41 .. drwxrw---- 2 root_test root_test 4096 Sep 2 11:41 . ./test: total 8 drwx------ 7 root_test root_test 4096 Oct 26 21:52 .. -rw-rw-r-- 1 root_test root_test 0 Oct 26 21:52 my.txt drwxrwxr-x 2 root_test root_test 4096 Oct 26 21:52 . 复制代码
文件类型标志(File type flag
)
-(regular file) 普通文件
d(directory) 目录文件
b(block) 块特殊⽂件,块设备文件。最常见的块设备是硬盘,但也存在许多其他块设备,如软盘驱动器、蓝光阅读器和闪存。
c(character) 字符特殊⽂件,字符设备文件,多表示一次性读取设备,典型的字符设备是终端(终端分多种,有物理的也有虚拟的)和键盘。
l(linkage) 链接文件,符号链接文件
f(FIFO-pipe) 管道文件,命名管道p,也简称 p
s(socket) 套接字⽂件,本地套接字
区分块设备和字符设备最简单的方法是看数据访问的方式。
能随机访问获取数据的是块设备,必须按字节顺序访问的是字符设备。
推荐参考 搞懂Linux下的几种文件类型。
文件权限的表示
r = 4,读。
w = 2,写。
x = 1,执行。
对于目录的可执行权限(x
),表示的是可进入目录。
rwx权限的组合,可以通过其对应的数字相加的值来表示。
比如,可读可执行(rx=5=4+1)、可读可写(rw=6=4+2)、可读可写可执行(rwx=7=4+2+1)。
这也是常常看到的 chmod 命令中 chmod 755
、chmod 774
、chmod 775
、chmod 777
数字所表示的含义。
有时会看到
chmod 4755
四个数字表示的形式。与chmod 755
的区别在于开头多了一位,此处为4,表示其他用户执行文件时,具有与所有者相同的权限。(未进一步确认)
linux命令chmod命令设置权限的777,775,774
附:关于 inode 和 ls -li
inode 中文意思是“索引节点”。
每个存储设备或存储设备的分区(存储设备是硬盘、软盘、U盘等等)被格式化为文件系统后,应该有两部份,一部份是inode,另一部份是Block,Block是用来存储数据用的。而inode,则是用来存储这些数据的信息,这些信息包括文件大小、属主、归属的用户组、读写权限等。
inode为每个文件进行信息索引,所以就有了inode的数值。操作系统根据指令,能通过inode值最快的找到相对应的文件。
当用 ls 查看目录或文件时,如果加上 -i
参数,就会显示出 inode 节点号码。
$ ls -li total 64 492775 -rw-rw-r-- 1 root_test root_test 54908 Apr 16 2014 axel-2.4-9.el7.x86_64.rpm 499731 drwxrwxr-x 2 root_test root_test 4096 Oct 26 21:52 test 492981 -rw-rw-r-- 1 root_test root_test 0 Sep 3 17:40 Y6kkd1d 复制代码
test 的 inode 值为 499731;Y6kkd1d 的 inode 值是 492981。
此部分参考自 linux每日命令(26):Linux文件属性详解。
作者:代码迷途
链接:https://juejin.cn/post/7024290464954073102