ls命令来自英文单词list的缩写,中文译为“列出”,其功能是显示目录中的文件及其属性信息,是最常使用的Linux命令之一。
默认不添加任何参数的情况下,ls命令会列出当前工作目录中的文件信息,常与cd或pwd命令搭配使用,十分方便。带上参数后,我们可以做更多的事情。作为最基础、最频繁使用的命令,有必要仔细了解其常用功能。
语法格式:ls [参数] [文件或目录…]
常用参数:
参数 | 说明 |
---|---|
-a | 显示所有文件,包括以. 开头的隐藏文件 |
-A | 显示除. 和.. 外的所有文件 |
-b | 以八进制转义字符显示不可打印字符 |
-c | 根据文件状态更改时间排序,并显示ctime |
-d | 将目录视为普通文件,显示目录自身的信息 |
-f | 不进行排序,直接列出结果,等同于启用-aU 并禁用-lst |
-F | 在每个名称后附加指示符号(例如:/ 表示目录,* 表示可执行文件) |
-h | 以人类可读的格式显示文件大小(例如:1K,234M,2G) |
-i | 显示文件的inode编号 |
-l | 以长格式显示文件的详细信息,包括权限、所有者、大小和修改时间 |
-m | 使用逗号分隔文件名,横向输出 |
-n | 以数字形式显示用户和组ID,而非名称 |
-p | 在目录名后加上斜杠/ 以区分目录 |
-q | 用问号? 替换不可打印的字符 |
-r | 逆序排列输出 |
-R | 递归列出所有子目录及其内容 |
-s | 显示每个文件的块大小 |
-S | 按文件大小排序 |
-t | 按修改时间排序,最新的排在前面 |
-u | 显示文件的访问时间,并根据访问时间排序 |
-x | 按行列顺序排列输出,横向排序 |
-1 | 每行只输出一个文件名 |
--color | 根据文件类型使用不同颜色显示,参数可为never 、always 或auto |
--full-time | 显示完整的时间戳信息 |
--help | 显示此命令的帮助信息 |
--version | 显示此命令的版本信息 |
参考示例
显示当前目录中的文件名(默认不含隐藏文件):
[root@linuxcool ~]# ls
anaconda-ks.cfg Documents initial-setup-ks.cfg Pictures Templates
Desktop Downloads Music Public Videos
显示当前目录中的文件名(含隐藏文件):
[root@linuxcool ~]# ls -a . .bashrc Documents Music Videos .. .cache Downloads Pictures .viminfo anaconda-ks.cfg .config .esd_auth .pki .bash_history .cshrc .ICEauthority Public .bash_logout .dbus initial-setup-ks.cfg .tcshrc .bash_profile Desktop .local Templates
以详细信息模式输出文件名及其属性信息:
[root@linuxcool ~]# ls -l total 8 -rw-------. 1 root root 1430 Dec 14 08:05 anaconda-ks.cfg drwxr-xr-x. 2 root root 6 Dec 14 08:37 Desktop drwxr-xr-x. 2 root root 6 Dec 14 08:37 Documents drwxr-xr-x. 2 root root 6 Dec 14 08:37 Downloads -rw-r--r--. 1 root root 1585 Dec 14 08:34 initial-setup-ks.cfg drwxr-xr-x. 2 root root 6 Dec 14 08:37 Music drwxr-xr-x. 2 root root 6 Dec 14 08:37 Pictures drwxr-xr-x. 2 root root 6 Dec 14 08:37 Public drwxr-xr-x. 2 root root 6 Dec 14 08:37 Templates drwxr-xr-x. 2 root root 6 Dec 14 08:37 Videos
显示指定目录中的文件列表:
[root@linuxcool ~]# ls /etc adjtime hosts pulse aliases hosts.allow qemu-ga alsa hosts.deny qemu-kvm alternatives hp radvd.conf anacrontab idmapd.conf ras asound.conf init.d rc0.d at.deny inittab rc1.d ………………省略部分输出信息………………
显示当前目录中的文件名及inode属性信息:
[root@linuxcool ~]# ls -i 35290115 anaconda-ks.cfg 35290137 initial-setup-ks.cfg 35290164 Templates 1137391 Desktop 17840039 Music 51609597 Videos 1137392 Documents 35290165 Pictures 17840038 Downloads 51609596 Public
结合通配符一起使用,显示指定目录中所有以sd开头的文件列表:
[root@linuxcool ~]# ls /dev/sd* /dev/sda /dev/sda1 /dev/sda2
依据文件内容大小进行排序,显示指定目录中文件名及其属性详情信息:
[root@linuxcool ~]# ls -Sl /etc
total 1348
-rw-r--r--. 1 root root 692241 Sep 10 2023 services
-rw-r--r--. 1 root root 66482 Dec 14 08:34 ld.so.cache
-rw-r--r--. 1 root root 60352 May 11 2023 mime.types
-rw-r--r--. 1 root dnsmasq 26843 Aug 12 2023 dnsmasq.conf
-rw-r--r--. 1 root root 25696 Dec 12 2023 brltty.conf
-rw-r--r--. 1 root root 9450 Aug 12 2023 nanorc
-rw-r--r--. 1 root root 7265 Dec 14 08:03 kdump.conf
-rw-------. 1 tss tss 7046 Aug 13 2023 tcsd.conf
………………省略部分输出信息………………