linux命令详解之ls命令

linux命令详解之ls命令ls 命令概述 ls 命令用于显示文件目录列表 和 Windows 系统下 DOS 命令 dir 类似 当执行 ls 命令时 默认显示的只有非隐藏文件的文件名 以文件名进行排序及文件名代表的颜色显示 当不加参数时 默认列出当前目录的列表信息 ls 命令使用频率非常高 参数也非常多 多达四五十个 本文只介绍一部分常用的参数 其他参数可以通过 man 命令查看帮助手册 说明 在 linux 下 文件名以点 开头表示该文件为隐藏文件 如

ls命令概述

ls命令用于显示文件目录列表,和Windows系统下DOS命令dir类似。当执行ls命令时,默认显示的只有非隐藏文件的文件名、以文件名进行排序及文件名代表的颜色显示。当不加参数时,默认列出当前目录的列表信息。ls命令使用频率非常高,参数也非常多,多达四五十个,本文只介绍一部分常用的参数,其他参数可以通过man命令查看帮助手册。

说明:在linux下,文件名以点.开头表示该文件为隐藏文件,如.bashrc

ls命令语法

ls [选项] [目录或文件名]
参数:
-a--all的缩写,显示所有的文件,包括隐藏文件(以.开头的文件),参考示例1。(常用)
-A--almost-all的缩写,显示所有的文件,包括隐藏文件,但不包括表示当前目录.和上级目录..这两个文件,参考示例2
-c:和-lt一起使用:显示列表并且以ctime(文件状态最后改变时间)排序。和-l一起使用:显示ctime并且以文件名排序。其他情况,以ctime排序。参考示例3
-d--directory的缩写,仅列出目录本身,而不是列出目录里的内容列表,参考示例4。(常用)
-f:直接列出结果,而不进行排序(ls默认会以文件名排序)
--color[=WHEN]:是否根据文件类型显示颜色,WHEN可以为neveralways或者auto
--full-time:以完整的实际模式显示(包含年月日时分),类似与ls -l --time-style=full-iso参考示例5
-g:列表显示结果,和-l类似,但是不显示文件所属者。
-h:将文件内容大小以GB、KB等易读的方式显示,参考示例6
-i:结合-l参数,列出每个文件的inode,参考示例7
-l:列出长数据串,显示出文件的属性与权限等数据信息(常用)
-n:和-l类似,只是显示的所属用户和组不是名称而是对应的id,参考示例8
-r--reverse,将排序结果以倒序方式显示,参考示例9
-S:以文件大小排序,参考示例9
-t:以修改时间排序
--help:显示帮助信息


































ls命令示例

示例1:列出所有文件(注意和-A参数的区别,结果里面包括表示当前目录.和上级目录..这两个文件)。

[root@localhost ~]# ls -a #列出所有文件 . .. .bash_history .bash_logout .bash_profile .bashrc install.log install.log.syslog [root@localhost ~]# ls --all . .. .bash_history .bash_logout .bash_profile .bashrc install.log install.log.syslog

示例2:列出所有的文件,但不包括表示当前目录.和上级目录..这两个文件。

[root@localhost ~]# ls -A .bash_history .bash_logout .bash_profile .bashrc install.log install.log.syslog [root@localhost ~]# ls --almost-all .bash_history .bash_logout .bash_profile .bashrc install.log install.log.syslog

示例3:显示列表并且以ctime排序

[root@localhost /]# ls -clt #和 -lt参数一起使用,以时间排序 total 146 drwxrwxrwt 7 root root 4096 Apr 13 06:23 tmp drwxr-xr-x 12 root root 4440 Apr 13 05:18 dev drwxr-xr-x 93 root root 12288 Apr 13 05:18 etc drwxr-xr-x 2 root root 0 Apr 13 05:18 net drwxr-xr-x 2 root root 0 Apr 13 05:18 misc drwxr-xr-x 4 root root 0 Apr 13 05:16 selinux drwxr-xr-x 11 root root 0 Apr 13 05:16 sys dr-xr-xr-x 150 root root 0 Apr 13 05:16 proc drwxr-xr-x 2 root root 12288 Mar 28 06:34 sbin drwxr-xr-x 2 root root 4096 Mar 28 06:33 bin drwxr-xr-x 8 root root 4096 Mar 28 06:33 lib64 drwxr-x--- 4 root root 4096 Nov 19 06:52 root drwxr-xr-x 4 root root 4096 Nov 18 22:05 home drwxr-xr-x 11 root root 4096 Nov 14 06:13 lib drwxr-xr-x 3 root root 4096 Oct 17 2015 mnt drwxr-xr-x 22 root root 4096 Oct 17 2015 var drwxr-xr-x 4 root root 1024 Oct 17 2015 boot drwxr-xr-x 15 root root 4096 Oct 17 2015 usr drwxr-xr-x 2 root root 4096 Oct 17 2015 media drwxr-xr-x 2 root root 4096 Oct 17 2015 srv drwx------ 2 root root 16384 Oct 17 2015 lost+found drwxr-xr-x 3 root root 4096 Oct 17 2015 opt [root@localhost /]# ls -cl #和-l参数一起使用,以文件名排序并显示时间 total 146 drwxr-xr-x 2 root root 4096 Mar 28 06:33 bin drwxr-xr-x 4 root root 1024 Oct 17 2015 boot drwxr-xr-x 12 root root 4440 Apr 13 05:18 dev drwxr-xr-x 93 root root 12288 Apr 13 05:18 etc drwxr-xr-x 4 root root 4096 Nov 18 22:05 home drwxr-xr-x 11 root root 4096 Nov 14 06:13 lib drwxr-xr-x 8 root root 4096 Mar 28 06:33 lib64 drwx------ 2 root root 16384 Oct 17 2015 lost+found drwxr-xr-x 2 root root 4096 Oct 17 2015 media drwxr-xr-x 2 root root 0 Apr 13 05:18 misc drwxr-xr-x 3 root root 4096 Oct 17 2015 mnt drwxr-xr-x 2 root root 0 Apr 13 05:18 net drwxr-xr-x 3 root root 4096 Oct 17 2015 opt dr-xr-xr-x 150 root root 0 Apr 13 05:16 proc drwxr-x--- 4 root root 4096 Nov 19 06:52 root drwxr-xr-x 2 root root 12288 Mar 28 06:34 sbin drwxr-xr-x 4 root root 0 Apr 13 05:16 selinux drwxr-xr-x 2 root root 4096 Oct 17 2015 srv drwxr-xr-x 11 root root 0 Apr 13 05:16 sys drwxrwxrwt 7 root root 4096 Apr 13 06:23 tmp drwxr-xr-x 15 root root 4096 Oct 17 2015 usr drwxr-xr-x 22 root root 4096 Oct 17 2015 var [root@localhost /]# ls -c #单独使用,以时间排序,但不显示时间 tmp dev etc net misc selinux sys proc sbin bin lib64 root home lib mnt var boot usr media srv lost+found opt

示例4:仅仅列出目录本身,不需要列出目录里的内容

[root@localhost /]# ls -d /home #仅列出/home目录本身 /home [root@localhost /]# ls /home #列出/home目录里的内容 sgl software # 加上-l参数,比较的更清楚一些: [root@localhost /]# ls -ld /home drwxr-xr-x 4 root root 4096 Nov 18 22:05 /home [root@localhost /]# ls -l /home total 16 drwx------ 16 sgl sgl 4096 Oct 17 2015 sgl drwxr-xr-x 3 root root 4096 Nov 14 05:13 software 

示例5:显示完整时间

[root@localhost ~]# ls --full-time / total 146 drwxr-xr-x 2 root root 4096 2017-03-28 06:33:59.000000000 -0700 bin drwxr-xr-x 4 root root 1024 2015-10-17 08:08:24.000000000 -0700 boot drwxr-xr-x 12 root root 4440 2017-04-14 21:22:27. -0700 dev drwxr-xr-x 93 root root 12288 2017-04-14 21:22:24.000000000 -0700 etc drwxr-xr-x 4 root root 4096 2016-11-18 22:05:32.000000000 -0800 home drwxr-xr-x 11 root root 4096 2016-11-14 06:13:35.000000000 -0800 lib drwxr-xr-x 8 root root 4096 2017-03-28 06:33:33.000000000 -0700 lib64 drwx------ 2 root root 16384 2015-10-17 08:01:52.000000000 -0700 lost+found drwxr-xr-x 2 root root 4096 2011-05-11 04:58:23.000000000 -0700 media drwxr-xr-x 2 root root 0 2017-04-14 21:22:23. -0700 misc drwxr-xr-x 3 root root 4096 2015-10-17 08:16:39.000000000 -0700 mnt drwxr-xr-x 2 root root 0 2017-04-14 21:22:23. -0700 net drwxr-xr-x 3 root root 4096 2015-10-17 00:17:25.000000000 -0700 opt dr-xr-xr-x 147 root root 0 2017-04-14 21:21:02.00 -0700 proc drwxr-x--- 4 root root 4096 2016-11-19 06:52:09.000000000 -0800 root drwxr-xr-x 2 root root 12288 2017-03-28 06:34:07.000000000 -0700 sbin drwxr-xr-x 4 root root 0 2017-04-14 21:21:02. -0700 selinux drwxr-xr-x 2 root root 4096 2011-05-11 04:58:23.000000000 -0700 srv drwxr-xr-x 11 root root 0 2017-04-14 21:21:02. -0700 sys drwxrwxrwt 7 root root 4096 2017-04-14 21:24:24.000000000 -0700 tmp drwxr-xr-x 15 root root 4096 2015-10-17 08:04:13.000000000 -0700 usr drwxr-xr-x 22 root root 4096 2015-10-17 08:11:05.000000000 -0700 var

示例6:以易读方式显示列表

[root@localhost ~]# ls -lh / #注意列表容量大小列的单位 total 146K drwxr-xr-x 2 root root 4.0K Mar 28 06:33 bin drwxr-xr-x 4 root root 1.0K Oct 17 2015 boot drwxr-xr-x 12 root root 4.4K Apr 14 21:22 dev drwxr-xr-x 93 root root 12K Apr 14 21:22 etc drwxr-xr-x 4 root root 4.0K Nov 18 22:05 home drwxr-xr-x 11 root root 4.0K Nov 14 06:13 lib drwxr-xr-x 8 root root 4.0K Mar 28 06:33 lib64 drwx------ 2 root root 16K Oct 17 2015 lost+found drwxr-xr-x 2 root root 4.0K May 11 2011 media drwxr-xr-x 2 root root 0 Apr 14 21:22 misc drwxr-xr-x 3 root root 4.0K Oct 17 2015 mnt drwxr-xr-x 2 root root 0 Apr 14 21:22 net drwxr-xr-x 3 root root 4.0K Oct 17 2015 opt dr-xr-xr-x 147 root root 0 Apr 14 21:21 proc drwxr-x--- 4 root root 4.0K Nov 19 06:52 root drwxr-xr-x 2 root root 12K Mar 28 06:34 sbin drwxr-xr-x 4 root root 0 Apr 14 21:21 selinux drwxr-xr-x 2 root root 4.0K May 11 2011 srv drwxr-xr-x 11 root root 0 Apr 14 21:21 sys drwxrwxrwt 7 root root 4.0K Apr 14 21:24 tmp drwxr-xr-x 15 root root 4.0K Oct 17 2015 usr drwxr-xr-x 22 root root 4.0K Oct 17 2015 var [root@localhost ~]# ls -l / #默认方式,以字节为单位显示 total 146 drwxr-xr-x 2 root root 4096 Mar 28 06:33 bin drwxr-xr-x 4 root root 1024 Oct 17 2015 boot drwxr-xr-x 12 root root 4440 Apr 14 21:22 dev drwxr-xr-x 93 root root 12288 Apr 14 21:22 etc drwxr-xr-x 4 root root 4096 Nov 18 22:05 home drwxr-xr-x 11 root root 4096 Nov 14 06:13 lib drwxr-xr-x 8 root root 4096 Mar 28 06:33 lib64 drwx------ 2 root root 16384 Oct 17 2015 lost+found drwxr-xr-x 2 root root 4096 May 11 2011 media drwxr-xr-x 2 root root 0 Apr 14 21:22 misc drwxr-xr-x 3 root root 4096 Oct 17 2015 mnt drwxr-xr-x 2 root root 0 Apr 14 21:22 net drwxr-xr-x 3 root root 4096 Oct 17 2015 opt dr-xr-xr-x 147 root root 0 Apr 14 21:21 proc drwxr-x--- 4 root root 4096 Nov 19 06:52 root drwxr-xr-x 2 root root 12288 Mar 28 06:34 sbin drwxr-xr-x 4 root root 0 Apr 14 21:21 selinux drwxr-xr-x 2 root root 4096 May 11 2011 srv drwxr-xr-x 11 root root 0 Apr 14 21:21 sys drwxrwxrwt 7 root root 4096 Apr 14 21:24 tmp drwxr-xr-x 15 root root 4096 Oct 17 2015 usr drwxr-xr-x 22 root root 4096 Oct 17 2015 var

示例7:显示inode

[root@localhost ~]# ls -li / total 146  drwxr-xr-x 2 root root 4096 Mar 28 06:33 bin 2 drwxr-xr-x 4 root root 1024 Oct 17 2015 boot 1071 drwxr-xr-x 12 root root 4440 Apr 14 21:22 dev  drwxr-xr-x 93 root root 12288 Apr 14 21:22 etc  drwxr-xr-x 4 root root 4096 Nov 18 22:05 home  drwxr-xr-x 11 root root 4096 Nov 14 06:13 lib  drwxr-xr-x 8 root root 4096 Mar 28 06:33 lib64 11 drwx------ 2 root root 16384 Oct 17 2015 lost+found  drwxr-xr-x 2 root root 4096 May 11 2011 media 14244 drwxr-xr-x 2 root root 0 Apr 14 21:22 misc  drwxr-xr-x 3 root root 4096 Oct 17 2015 mnt 14249 drwxr-xr-x 2 root root 0 Apr 14 21:22 net  drwxr-xr-x 3 root root 4096 Oct 17 2015 opt 1 dr-xr-xr-x 147 root root 0 Apr 14 21:21 proc 65217 drwxr-x--- 4 root root 4096 Nov 19 06:52 root  drwxr-xr-x 2 root root 12288 Mar 28 06:34 sbin 591 drwxr-xr-x 4 root root 0 Apr 14 21:21 selinux  drwxr-xr-x 2 root root 4096 May 11 2011 srv 1 drwxr-xr-x 11 root root 0 Apr 14 21:21 sys  drwxrwxrwt 7 root root 4096 Apr 14 21:24 tmp  drwxr-xr-x 15 root root 4096 Oct 17 2015 usr  drwxr-xr-x 22 root root 4096 Oct 17 2015 var

示例8:列出文件夹内容,并显示出文件所属用户和组的id

[root@localhost ~]# ls -ln / total 146 drwxr-xr-x 2 0 0 4096 Mar 28 06:33 bin drwxr-xr-x 4 0 0 1024 Oct 17 2015 boot drwxr-xr-x 12 0 0 4440 Apr 14 21:22 dev drwxr-xr-x 93 0 0 12288 Apr 14 21:22 etc drwxr-xr-x 4 0 0 4096 Nov 18 22:05 home drwxr-xr-x 11 0 0 4096 Nov 14 06:13 lib drwxr-xr-x 8 0 0 4096 Mar 28 06:33 lib64 drwx------ 2 0 0 16384 Oct 17 2015 lost+found drwxr-xr-x 2 0 0 4096 May 11 2011 media drwxr-xr-x 2 0 0 0 Apr 14 21:22 misc drwxr-xr-x 3 0 0 4096 Oct 17 2015 mnt drwxr-xr-x 2 0 0 0 Apr 14 21:22 net drwxr-xr-x 3 0 0 4096 Oct 17 2015 opt dr-xr-xr-x 147 0 0 0 Apr 14 21:21 proc drwxr-x--- 4 0 0 4096 Nov 19 06:52 root drwxr-xr-x 2 0 0 12288 Mar 28 06:34 sbin drwxr-xr-x 4 0 0 0 Apr 14 21:21 selinux drwxr-xr-x 2 0 0 4096 May 11 2011 srv drwxr-xr-x 11 0 0 0 Apr 14 21:21 sys drwxrwxrwt 7 0 0 4096 Apr 14 21:24 tmp drwxr-xr-x 15 0 0 4096 Oct 17 2015 usr drwxr-xr-x 22 0 0 4096 Oct 17 2015 var

示例9:以文件大小排序(升序和降序)

[root@localhost ~]# ls -lS / #默认降序排序 total 146 drwx------ 2 root root 16384 Oct 17 2015 lost+found drwxr-xr-x 93 root root 12288 Apr 14 21:22 etc drwxr-xr-x 2 root root 12288 Mar 28 06:34 sbin drwxr-xr-x 12 root root 4440 Apr 14 21:22 dev drwxr-xr-x 2 root root 4096 Mar 28 06:33 bin drwxr-xr-x 4 root root 4096 Nov 18 22:05 home drwxr-xr-x 11 root root 4096 Nov 14 06:13 lib drwxr-xr-x 8 root root 4096 Mar 28 06:33 lib64 drwxr-xr-x 2 root root 4096 May 11 2011 media drwxr-xr-x 3 root root 4096 Oct 17 2015 mnt drwxr-xr-x 3 root root 4096 Oct 17 2015 opt drwxr-x--- 4 root root 4096 Nov 19 06:52 root drwxr-xr-x 2 root root 4096 May 11 2011 srv drwxrwxrwt 7 root root 4096 Apr 14 21:24 tmp drwxr-xr-x 15 root root 4096 Oct 17 2015 usr drwxr-xr-x 22 root root 4096 Oct 17 2015 var drwxr-xr-x 4 root root 1024 Oct 17 2015 boot drwxr-xr-x 2 root root 0 Apr 14 21:22 misc drwxr-xr-x 2 root root 0 Apr 14 21:22 net dr-xr-xr-x 147 root root 0 Apr 14 21:21 proc drwxr-xr-x 4 root root 0 Apr 14 21:21 selinux drwxr-xr-x 11 root root 0 Apr 14 21:21 sys [root@localhost ~]# ls -lSr / #通过-r参数实现升序排列 total 146 drwxr-xr-x 11 root root 0 Apr 14 21:21 sys drwxr-xr-x 4 root root 0 Apr 14 21:21 selinux dr-xr-xr-x 147 root root 0 Apr 14 21:21 proc drwxr-xr-x 2 root root 0 Apr 14 21:22 net drwxr-xr-x 2 root root 0 Apr 14 21:22 misc drwxr-xr-x 4 root root 1024 Oct 17 2015 boot drwxr-xr-x 22 root root 4096 Oct 17 2015 var drwxr-xr-x 15 root root 4096 Oct 17 2015 usr drwxrwxrwt 7 root root 4096 Apr 14 21:24 tmp drwxr-xr-x 2 root root 4096 May 11 2011 srv drwxr-x--- 4 root root 4096 Nov 19 06:52 root drwxr-xr-x 3 root root 4096 Oct 17 2015 opt drwxr-xr-x 3 root root 4096 Oct 17 2015 mnt drwxr-xr-x 2 root root 4096 May 11 2011 media drwxr-xr-x 8 root root 4096 Mar 28 06:33 lib64 drwxr-xr-x 11 root root 4096 Nov 14 06:13 lib drwxr-xr-x 4 root root 4096 Nov 18 22:05 home drwxr-xr-x 2 root root 4096 Mar 28 06:33 bin drwxr-xr-x 12 root root 4440 Apr 14 21:22 dev drwxr-xr-x 2 root root 12288 Mar 28 06:34 sbin drwxr-xr-x 93 root root 12288 Apr 14 21:22 etc drwx------ 2 root root 16384 Oct 17 2015 lost+found
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/213700.html原文链接:https://javaforall.net

(0)
上一篇 2026年3月18日 下午5:50
下一篇 2026年3月18日 下午5:51


相关推荐

  • C++中成员变量 父类 子类构造析构顺序

    C++中成员变量 父类 子类构造析构顺序c 中对象都是先构造成员变量 再调用自身的构造函数 故父类成员变量首先被构造 然后调用父类的构造函数 再构造子类的成员变量 最后调用子类的构造函数 对象析构时与构造顺序相反 子类先调用自身的析构函数 再调用子类成员变量的析构函数 再调用父类的析构函数 再析构父类的成员变量 测试代码如下 includeusing classTest

    2026年3月16日
    2
  • webstorm格式化代码快捷键[通俗易懂]

    webstorm格式化代码快捷键[通俗易懂]ctrl+alt+L把网易云音乐的快捷键关了就可以了转载于:https://www.cnblogs.com/foreverlin/p/10103283.html

    2022年6月14日
    33
  • SpringBoot+JWT+Shiro+MybatisPlus实现Restful快速开发后端脚手架

    SpringBoot+JWT+Shiro+MybatisPlus实现Restful快速开发后端脚手架

    2020年11月14日
    208
  • linux 查看java的pid,linux 查看java进程pid「建议收藏」

    linux 查看java的pid,linux 查看java进程pid「建议收藏」linux查看java进程pid[2021-01-3021:05:24]简介:建站服务器这篇文章主要介绍了linux中如何查看系统进程,具有一定借鉴价值,需要的朋友可以参考下。希望大家阅读完这篇文章后大有收获。下linux查看端口被哪个进程占用的方法:1、使用“lsof-i:端口号”来查看;2、使用“netstat-tunlp|grep端口号”来查看。linux查看端口被哪个进程占…

    2022年8月24日
    11
  • 二传感器尺寸「建议收藏」

    二传感器尺寸「建议收藏」说到传感器的尺寸,其实是说感光器件的面积大小,这里就包括了CCD和CMOS。感光器件的面积越大,CCD/CMOS面积越大,捕捉的光子越多,感光性能越好,信噪比越高。下面是家用小DC图像传感器尺寸换算为公制的大小(单位为毫米):2/3英寸的画幅面积为(8.8×6.6mm)1/1.8英寸的画幅面积为(7.178×5.319mm)1/2英寸的画幅面积为(6.4×4.8mm)1/2.7英寸的画幅面…

    2022年6月12日
    50
  • 最全排列组合算法详解以及套路总结一文突破

    最全排列组合算法详解以及套路总结一文突破1 排列组合问题排列组合是经典的算法问题 相关的内容中学阶段就学习过 在讲算法实现之前 我们先简单复习一下排列组合的相关定义 排列 英文名称为 Permutation 简称 P 假设有一个数组 1 2 3 4 5 我们需要将数组中的所有元素进行排序 那么第一个位置 我们可以选择五个数字的任何一个 共有 5 种选择 第二个位置 可以选择剩余四个数字的任何一个 共有 4 种选择 第三个位置 可以选择剩余三个数字中的任何一个 共有 3 种选择 第四个位置 可以选择剩余两个数字中的任何一个 共有 2 种选择 最后一个位置

    2026年3月26日
    2

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号