‘ls’命令是Linux下用来显示文件目录及其目录列表的。在下方的带有每个命令简短叙述的命令列表中,有几个是有不同用法的。在这次专栏里面,我们列举了对入门学习非常有用的十几个命令。
ls命令格式如下所示:
#ls[选项][文件][目录]
-a |
列举出隐藏文件,如果一个文件或目录的名字以一个’.’开始,那么他就被称为隐藏的 |
-F |
显示文件类型,显示,/用于文件目录,*用于可执行文件,@用于非文本文件的符号链接 |
-lh |
显示长文件列表,其中带有可读格式的文件大小 |
-l |
显示带有文件类型,权限,连接数,所有者,长文件列表,所在的文件簇,文件大小,创建日期以及时间 |
of |
最后一次修改或用于文件.ld的名字中。显示指定路径的长文件列表,但是隐藏其中的内容 |
-R |
显示指定路径内容以及他的所有子目录(递归列举) |
-lt |
以创建的时间和日期的方式排列所有文件,以创建时间由短到长的顺序排列 |
-ltr |
以创建的时间和日期的方式排列所有文件,以创建时间由长到短的顺序排列 |
注意:想知道更详细的ls命令信息,请查阅ls手册
例子1:列举当前工作目录的文件以及路径
[root@localhost /]# ls
bin boot cgroup dev etc home lib lib64 lost+found media mnt opt proc root sbin selinux srv sys tmp usr var
[root@localhost /]#
例子2:列举当前当前路径的详细信息
[root@localhost /]# ls -l
total 94
dr-xr-xr-x. 2 root root 4096 Nov 23 03:13 bin
dr-xr-xr-x. 5 root root 1024 Nov 23 05:29 boot
drwxr-xr-x 10 root root 4096 Nov 23 03:42 cgroup
drwxr-xr-x 18 root root 3680 Dec 12 21:32 dev
drwxr-xr-x. 64 root root 4096 Dec 12 21:32 etc
drwxr-xr-x 8 root root 4096 Jul 19 02:43 home
dr-xr-xr-x. 8 root root 4096 May 3 2014 lib
dr-xr-xr-x. 8 root root 12288 Nov 23 03:13 lib64
-rw-r–r– 1 root root 72 Dec 12 21:40 linux-stuff
drwx——. 2 root root 16384 May 3 2014 lost+found
drwxr-xr-x. 2 root root 4096 Sep 23 2011 media
drwxr-xr-x. 3 root root 4096 Jun 22 01:14 mnt
…………………………………………..
列子3:列举指定路径的内容
[root@localhost /]# ls -l /tmp/
total 8
-rw-r–r– 1 root root 0 Aug 3 14:31 bad-blocks.txt
drwx——. 2 root root 4096 May 3 2014 gpg-auCCFn
-rw——-. 1 root root 0 May 3 2014 yum.log
-rw——- 1 root root 1428 Nov 23 03:58 yum_save_tx-2014-11-23-03-58BHO8Jg.yumtx
如果只是想列举文件路径而不需要其中的内容,用’ld’命令,操作如下
[root@localhost /]# ls -ld /tmp/
drwxrwxrwt. 4 root root 4096 Dec 12 21:34 /tmp/
例子4:显示当前路径的所有文件,以及文件类型
[root@localhost /]# ls -F
bin/ cgroup/ etc/ lib/ linux-stuff media/ opt/ root/ selinux/ sys/ usr/
boot/ dev/ home/ lib64/ lost+found/ mnt/ proc/ sbin/ srv/ tmp/ var/
[root@localhost /]#
如果我们用ls命令中的‘F’操作,在所有的文件目录之后会追加一个/,普通文件之后没有
例子5:列举当前目录下的所有文件,并显示详细并以建立时间由短到长的方式排列
[root@localhost /]# ls -lt
total 94
-rw-r–r– 1 root root 72 Dec 12 21:40 linux-stuff
dr-xr-xr-x 13 root root 0 Dec 12 21:36 sys
drwxrwxrwt. 4 root root 4096 Dec 12 21:34 tmp
drwxr-xr-x 18 root root 3680 Dec 12 21:32 dev
drwxr-xr-x. 64 root root 4096 Dec 12 21:32 etc
dr-xr-xr-x 95 root root 0 Dec 12 21:31 proc
dr-xr-xr-x. 5 root root 1024 Nov 23 05:29 boot
drwxr-xr-x 10 root root 4096 Nov 23 03:42 cgroup
dr-xr-xr-x. 2 root root 4096 Nov 23 03:13 sbin
dr-xr-xr-x. 8 root root 12288 Nov 23 03:13 lib64
dr-xr-xr-x. 2 root root 4096 Nov 23 03:13 bin
dr-xr-x—. 5 root root 4096 Nov 23 02:53 root
drwxr-xr-x. 18 root root 4096 Aug 24 05:14 var
drwxr-xr-x 8 root root 4096 Jul 19 02:43 home
……………………….
例子6:显示当前路径的所有文件的文件大小,以可读形式显示(如:1K,234M,2G)
[root@localhost /]# ls -lh
total 94K
dr-xr-xr-x. 2 root root 4.0K Nov 23 03:13 bin
dr-xr-xr-x. 5 root root 1.0K Nov 23 05:29 boot
drwxr-xr-x 10 root root 4.0K Nov 23 03:42 cgroup
drwxr-xr-x 18 root root 3.6K Dec 12 21:32 dev
drwxr-xr-x. 64 root root 4.0K Dec 12 21:32 etc
drwxr-xr-x 8 root root 4.0K Jul 19 02:43 home
dr-xr-xr-x. 8 root root 4.0K May 3 2014 lib
dr-xr-xr-x. 8 root root 12K Nov 23 03:13 lib64
-rw-r–r– 1 root root 72 Dec 12 21:40 linux-stuff
drwx——. 2 root root 16K May 3 2014 lost+found
drwxr-xr-x. 2 root root 4.0K Sep 23 2011 media
drwxr-xr-x. 3 root root 4.0K Jun 22 01:14 mnt
drwxr-xr-x. 2 root root 4.0K Jun 22 01:25 opt
………………………………..
例子7:显示所有文件,包括隐藏的文件,当前路径的文件显示详细信息
[root@localhost /]# ls -la
total 102
dr-xr-xr-x. 23 root root 4096 Dec 12 21:49 .
dr-xr-xr-x. 23 root root 4096 Dec 12 21:49 ..
-rw-r–r– 1 root root 0 Dec 12 21:32 .autofsck
-rw-r–r– 1 root root 0 Jul 19 02:17 .autorelabel
dr-xr-xr-x. 2 root root 4096 Nov 23 03:13 bin
dr-xr-xr-x. 5 root root 1024 Nov 23 05:29 boot
drwxr-xr-x 10 root root 4096 Nov 23 03:42 cgroup
drwxr-xr-x 18 root root 3680 Dec 12 21:32 dev
drwxr-xr-x. 64 root root 4096 Dec 12 21:32 etc
drwxr-xr-x 8 root root 4096 Jul 19 02:43 home
dr-xr-xr-x. 8 root root 4096 May 3 2014 lib
dr-xr-xr-x. 8 root root 12288 Nov 23 03:13 lib64
-rw-r–r– 1 root root 0 Dec 12 21:48 .linux-rocks
-rw-r–r– 1 root root 72 Dec 12 21:40 linux-stuff
-rw-r–r– 1 root root 0 Dec 12 21:49 .linux-tips
drwx——. 2 root root 16384 May 3 2014 lost+found
drwxr-xr-x. 2 root root 4096 Sep 23 2011 media
…………………………………………………………
例子8:用递归的方式列举/etc路径的内容
[root@localhost /]# ls -R /etc/
/etc/:
adjtime
aliases
aliases.db
alternatives
anacrontab
audisp
audit
bash_completion.d
bashrc
blkid
CentOS-release
……………………..
例子9:以文件大小的方式排列文件
[root@localhost /]# ls -lhS
total 13M
-rw-r–r– 1 root root 12M Dec 12 22:05 linux-stuff
drwx——. 2 root root 16K May 3 2014 lost+found
dr-xr-xr-x. 8 root root 12K Nov 23 03:13 lib64
dr-xr-xr-x. 2 root root 4.0K Nov 23 03:13 bin
drwxr-xr-x 10 root root 4.0K Nov 23 03:42 cgroup
drwxr-xr-x. 64 root root 4.0K Dec 12 21:32 etc
drwxr-xr-x 8 root root 4.0K Jul 19 02:43 home
dr-xr-xr-x. 8 root root 4.0K May 3 2014 lib
drwxr-xr-x. 2 root root 4.0K Sep 23 2011 media
drwxr-xr-x. 3 root root 4.0K Jun 22 01:14 mnt
……………………………………….
上面所有命令显示文件以及路径,都是以文件所占大小 从大到小的方式排列的
例子10:用‘-i’选项列举文件节点以及路径
[root@localhost /]# ls -li
total 12378
786436 dr-xr-xr-x. 2 root root 4096 Nov 23 03:13 bin
2 dr-xr-xr-x. 5 root root 1024 Nov 23 05:29 boot
655361 drwxr-xr-x 10 root root 4096 Nov 23 03:42 cgroup
3 drwxr-xr-x 18 root root 3680 Dec 12 21:32 dev
393217 drwxr-xr-x. 64 root root 4096 Dec 12 21:32 etc
2 drwxr-xr-x 8 root root 4096 Jul 19 02:43 home
917506 dr-xr-xr-x. 8 root root 4096 May 3 2014 lib
262146 dr-xr-xr-x. 8 root root 12288 Nov 23 03:13 lib64
2668 -rw-r–r– 1 root root 12582912 Dec 12 22:05 linux-stuff
………………………………………………………….
例子11:用‘-version’命令显示当前版本信息
[root@localhost /]# ls –version
ls (GNU coreutils) 8.4
Copyright (C) 2010 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Written by Richard M. Stallman and David MacKenzie.
例子12:用‘-n’选项列举文件的uid&gid或者文件路径
[root@localhost /]# ls -n
total 14476
-rw-rw-r– 1 1000 1000 193 Aug 16 16:10 1
-rw-rw-r– 1 1000 1000 262 Nov 28 12:35 Anjali and Aakash help Yohan-B3MzR5cTpd2mLBSZfkPKppip8vCTKUdd.mp4.part
-rw-rw-r– 1 1000 1000 1849048 Jun 7 2014 BINGO from Super Simple Songs-9mmF8zOlh_g.mp4.part
drwxr-xr-x 18 1000 1000 4096 Dec 13 08:59 Desktop
drwxr-xr-x 2 1000 1000 4096 Sep 27 13:09 Documents
drwxr-xr-x 2 1000 1000 4096 Dec 2 10:10 Downloads
drwx—— 3 1000 1000 4096 Nov 30 15:36 Dropbox
-rw-rw-r– 1 1000 1000 94296 Nov 20 02:00 dropbox_2.10.0_amd64.deb
-rw-r–r– 1 1000 1000 8980 Apr 20 2014 examples.desktop
-rw-rw-r– 1 1000 1000 203 Oct 26 16:02 fs.sh
drwxrwxr-x 2 1000 1000 4096 Sep 1 14:31 Google Drive
……………………………………………………….
例子13:ls命令的默认别名
在终端上键入alias命令用来显示ls命令设置默认的别名。
[root@localhost /]# alias
alias cp=’cp -i’
alias l.=’ls -d .* –color=auto’
alias ll=’ls -l –color=auto’
alias ls=’ls –color=auto’
alias mv=’mv -i’
alias rm=’rm -i’
alias which=’alias | /usr/bin/which –tty-only –read-alias –show-dot –show-tilde’
例子14:用‘-time –style ’选项在ls命令输出之后显示时间戳
[root@localhost /]# ls -l –time-style=”+%Y-%m-%d $newline%m-%d %H:%M”
total 12378
dr-xr-xr-x. 2 root root 4096 2014-11-23 11-23 03:13 bin
dr-xr-xr-x. 5 root root 1024 2014-11-23 11-23 05:29 boot
drwxr-xr-x 10 root root 4096 2014-11-23 11-23 03:42 cgroup
drwxr-xr-x 18 root root 3680 2014-12-12 12-12 21:32 dev
drwxr-xr-x. 64 root root 4096 2014-12-12 12-12 21:32 etc
drwxr-xr-x 8 root root 4096 2014-07-19 07-19 02:43 home
dr-xr-xr-x. 8 root root 4096 2014-05-03 05-03 05:24 lib
dr-xr-xr-x. 8 root root 12288 2014-11-23 11-23 03:13 lib64
-rw-r–r– 1 root root 12582912 2014-12-12 12-12 22:05 linux-stuff
drwx——. 2 root root 16384 2014-05-03 05-03 05:19 lost+found
drwxr-xr-x. 2 root root 4096 2011-09-23 09-23 07:50 media
…………………………………………………………….
点击这里查看原文
译者:GuiltyMan
本文由 Linux公社翻译组 原创翻译 Linux公社 诚意奉献
本文永久更新链接地址:http://www.linuxidc.com/Linux/2014-12/110996.htm