ls是英文单词list的缩写,表示列举出指定目录下的文件名称及其属性。
不加参数的情况下,ls命令会列出当前工作目录中的文件和子目录。
带选项时,其语法格式如下:
ls [选项] [文件]
常用选项如下表所示:
选项 | 作用 |
-a | 显示所有文件及目录 (包括以“.”开头的隐藏文件) |
-l | 使用长格式列出文件及目录的详细信息 |
-r | 将文件按英文字母次序反向显示。默认为正向 |
-t | 根据最后的修改时间排序 |
-A | 同 -a ,但不列出 “.” (当前目录) 及 “..” (父目录) |
-S | 根据文件大小排序 |
-R | 递归列出所有子目录 |
-d | 查看目录的信息,但不包括其中子目录的信息 |
-i | 输出文件的inode节点信息 |
-m | 水平列出文件,以逗号间隔 |
-X | 按文件扩展名排序 |
-h | 将文件大小以人类易读的方式显示 |
-Z | 显示文件的SELinux安全上下文 |
--color | 输出信息中带有着色效果,默认带有该选项 |
实例演示:
1、列表显示当前目录中的所有文件(但不包含隐藏文件)
[root@myEuler ~]# ls
anaconda-ks.cfg data file1
2、列表显示当前目录中的报有文件(含隐藏文件)
[root@myEuler ~]# ls -a
. anaconda-ks.cfg .bash_logout .bashrc data .lesshst .viminfo
.. .bash_history .bash_profile .cshrc file1 .tcshrc
文件名前加小数点的就是隐藏文件
单独一个小数点(.)表示当前目录
两个小数点(..)表示上一层目录,也就是父目录。
3、列表显示指定文件的详细属性信息
扫描二维码关注公众号,回复:
15223247 查看本文章
[root@myEuler ~]# ls -l anaconda-ks.cfg
-rw-------. 1 root root 1055 Feb 19 23:31 anaconda-ks.cfg
4、显示指定目录的详细信息
[root@myEuler ~]# ls -l /etc/
total 1716
-rw-r--r--. 1 root root 44 Feb 22 23:54 adjtime
-rw-r--r--. 1 root root 1529 Jul 16 2021 aliases
drwxr-xr-x. 2 root root 4096 Feb 19 23:28 alternatives
-rw-r--r--. 1 root root 541 Dec 27 21:02 anacrontab
……此处省略部分输出信息……
5、显示当前目录下所有文件的详细信息,包括其inode号
[root@myEuler ~]# ls -li
total 8
786439 -rw-------. 1 root root 1055 Feb 19 23:31 anaconda-ks.cfg
786440 drwxr-xr-x. 2 root root 4096 Feb 26 23:51 data
786444 -rw-r--r--. 1 root root 0 Feb 26 23:51 file1
6、显示指定目录下的所有文件,要求按文件大小进行排序
[root@myEuler ~]# ls -hlS /etc
total 1.7M
-rw-r--r--. 1 root root 686K Jul 16 2021 services
-rw-r--r--. 1 root root 69K Apr 27 2021 mime.types
-rw-r--r--. 1 root root 48K Feb 19 23:36 ld.so.cache
-rw-r--r--. 1 root dnsmasq 28K Dec 27 20:49 dnsmasq.conf
-rw-r--r--. 1 root root 8.6K Feb 19 23:36 login.defs
……此处省略部分输出信息……
7、查看指定文件/目录的SELinux安全上下文
[root@myEuler ~]# ls -Zld /etc
drwxr-xr-x. 88 root root system_u:object_r:etc_t:s0 4096 3月 6 17:09 /etc