Linux ls --显示文件目录列表

本文介绍Linux下常用的ls命令及其各种参数的使用方法,包括显示文件目录列表、按时间排序、显示隐藏文件等实用技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

用途说明

ls命令用于显示文件目录列表,类似于DOS/Windows中的dir命令。它是Linux系统下最常用的命令之一,它的参数多达30个之多,也是较复杂的Linux命令。本文不打算列出所有的参数的含义,有兴趣的可以查看手册页,网上也有很多资源进行详细的描述。

常用参数

ls命令最常用的方式是就是不带任何参数。

格式:ls

若要显示某个具体的文件,直接跟上文件名即可。

格式:ls <FILE>

若要显示某个目录下的文件,直接跟上目录名即可。

格式:ls <DIR>

若要显示某个目录本身的信息,需要加上-d参数。

格式:ls -d <DIR>

若要显示目录或文件的详细信息,加上-l参数(l是long的缩写)。

格式:ls -l

若要列表按时间排序,加上-t参数(t是time的缩写),注意是按时间倒序排列的,最新的文件排在前面。

格式:ls -lt

若要使文件按时间顺序排列,在-t参数的基础上再加上-r参数(r是reverse的缩写)。

格式:ls -lrt

在Linux下,以点开头的文件或目录是隐藏的文件,通常有特殊用途,若要全部显示出来,要加上-a参数(a是all的缩写)。

格式:ls -a

在加上-a参数之后,会把所有以点开头的文件名,包括.和..,及当前目录和上级目录,若不想显示这两个,改用-A参数。

格式:ls -A

使用示例

示例一 按时间排序显示文件列表

[root@web ~]# ls -lrt

总计 196

-rw-r--r--  1root root     0 06-28 17:20 install.log.syslog

-rw-r--r--  1root root 35533 06-28 17:37 install.log

-rw-------  1root root  1469 06-28 17:37 anaconda-ks.cfg

drwxr-xr-x  2root root  4096 06-28 21:31 Desktop

-rwxr-Sr-x  1root root  1898 06-30 09:29 ntp.conf

-rw-------  1root root 78949 07-01 15:13 mbox

drwxr-xr-x  5root root  4096 07-03 17:17 work11

-rw-r--r--  1root root 10217 07-06 13:10 data.txt

drwxr-xr-x 13 rootroot  4096 10-18 15:15 work190

drwxr-xr-x  3root root  4096 10-28 14:47 setup

[root@web ~]#

上述列表的信息的含义,参见相关资料【2】。

示例二 显示隐藏文件

[root@web ~]# ls -a

.               .bash_profile  data.txt .gconf          .gstreamer-0.10     .lesshst       ntp.conf  .tcshrc

..              .bashrc        Desktop  .gconfd         .gtkrc-1.2-gnome2   mbox           .redhat   .Trash

anaconda-ks.cfg .chewing       .dmrc    .gnome          .ICEauthority      .metacity       .scim    .viminfo

.bash_history   .cshrc         .eggcups .gnome2         install.log        .mysql_history  setup     work11

.bash_logout    .cvspass       .elinks  .gnome2_private  install.log.syslog .nautilus      .ssh      work190

[root@web ~]# ls -A

anaconda-ks.cfg .bashrc   data.txt  .elinks .gnome2           .ICEauthority      mbox           ntp.conf  .ssh      work11

.bash_history   .chewing  Desktop   .gconf  .gnome2_private   install.log        .metacity       .redhat   .tcshrc  work190

.bash_logout    .cshrc    .dmrc     .gconfd .gstreamer-0.10    install.log.syslog  .mysql_history .scim     .Trash

.bash_profile   .cvspass  .eggcups  .gnome   .gtkrc-1.2-gnome2 .lesshst           .nautilus       setup    .viminfo

[root@web ~]#

 

示例三 ls命令默认显示颜色之谜

我们在bash中使用的ls实际上是个别名。

[root@web ~]# type -a ls

ls is aliased to `ls--color=tty'

ls is /bin/ls

[root@web ~]# ls

anaconda-ks.cfg data.txt  Desktop   install.log install.log.syslog  mbox  ntp.conf  setup   work11   work190

[root@web ~]# /bin/ls

anaconda-ks.cfg data.txt  Desktop  install.log  install.log.syslog mbox  ntp.conf  setup  work11  work190

[root@web ~]#


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值