ls是最常被使用到的Linux命令之一,它来自英语单词list的缩写,就像list单词的英语意思一样。它的功能是列出指定目录下的文件名称和特征。

默认情况下不添加参数,ls指令将列出各种工作目录中的文件信息,通常与cd和pwd配合使用指令非常方便。带上参数后,我们可以做更多的事情。

作为最基本、最常用的命令,有必要仔细了解其常用功能。


本次测试环境:Centos7.6系统-服务器来自: 蓝易云

定位建站服务器,特点是 香港五网CN2网络 ,国内速度优秀,支持VPC内网互联、快照、备份等功能。

移动+联通+电信+教育网+广电-五网CN2-延迟超低!

蓝易云持有 IDCISPCDN许可证,为正规商家。

详细测评文章:点我查看


语法格式: ls [参数] [文件]

常用参数:

-a显示所有文件及目录 (包括以“.”开头的隐藏文件)
-l使用长格式列出文件及目录的详细信息
-r将文件以相反次序显示(默认依英文字母次序)
-t根据最后的修改时间排序
-A同 -a ,但不列出 “.” (当前目录) 及 “..” (父目录)
-S根据文件大小排序
-R递归列出所有子目录
-d查看目录的信息,而不是里面子文件的信息
-i输出文件的inode节点信息
-m水平列出文件,以逗号间隔
-X按文件扩展名排序
--color输出信息中带有着色效果

参考实例

输出当前目录中的文件(默认不含隐藏文件):

[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  2018 services
-rw-r--r--.  1 root root     66482 Dec 14 08:34 ld.so.cache
-rw-r--r--.  1 root root     60352 May 11  2017 mime.types
-rw-r--r--.  1 root dnsmasq  26843 Aug 12  2018 dnsmasq.conf
-rw-r--r--.  1 root root     25696 Dec 12  2018 brltty.conf
-rw-r--r--.  1 root root      9450 Aug 12  2018 nanorc
-rw-r--r--.  1 root root      7265 Dec 14 08:03 kdump.conf
-rw-------.  1 tss  tss       7046 Aug 13  2018 tcsd.conf
………………省略部分输出信息………………
最后修改:2023 年 02 月 02 日
如果觉得我的文章对你有用,请随意赞赏