我使用过的Linux命令之ls - 显示文件目录列表

用途说明

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--1rootroot006-2817:20install.log.syslog

-rw-r--r--1rootroot3553306-2817:37install.log

-rw-------1rootroot146906-2817:37anaconda-ks.cfg

drwxr-xr-x2rootroot409606-2821:31Desktop

-rwxr-Sr-x1rootroot189806-3009:29ntp.conf

-rw-------1rootroot7894907-0115:13mbox

drwxr-xr-x5rootroot409607-0317:17work11

-rw-r--r--1rootroot1021707-0613:10data.txt

drwxr-xr-x13rootroot409610-1815:15work190

drwxr-xr-x3rootroot409610-2814:47setup

[root@web ~]#

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

示例二 显示隐藏文件

[root@web ~]# ls -a

..bash_profiledata.txt.gconf.gstreamer-0.10.lesshstntp.conf.tcshrc

...bashrcDesktop.gconfd.gtkrc-1.2-gnome2mbox.redhat.Trash

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

.bash_history.cshrc.eggcups.gnome2install.log.mysql_historysetupwork11

.bash_logout.cvspass.elinks.gnome2_privateinstall.log.syslog.nautilus.sshwork190

[root@web~]#ls-A

anaconda-ks.cfg.bashrcdata.txt.elinks.gnome2.ICEauthoritymboxntp.conf.sshwork11

.bash_history.chewingDesktop.gconf.gnome2_privateinstall.log.metacity.redhat.tcshrcwork190

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

.bash_profile.cvspass.eggcups.gnome.gtkrc-1.2-gnome2.lesshst.nautilussetup.viminfo

[root@web ~]#

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

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

[root@web ~]# type -a ls

lsisaliasedto`ls--color=tty'

lsis/bin/ls

[root@web~]#ls

anaconda-ks.cfgdata.txtDesktopinstall.loginstall.log.syslogmboxntp.confsetupwork11work190

[root@web~]#/bin/ls

anaconda-ks.cfgdata.txtDesktopinstall.loginstall.log.syslogmboxntp.confsetupwork11work190

[root@web ~]#

问题思考

相关资料

【1】Linux宝库 LS

【2】时光漂流瓶 linux系统中ls命令用法详解

返回 我使用过的Linux命令系列总目录