每天一个linux命令(1):ls命令

ls命令是linux下最常用的命令。ls命令就是list的缩写缺省下ls用来打印出当前目录的清单如果ls指定其他目录那么就会显示指定目录里的文件及文件夹清单。 通过ls 命令不仅可以查看linux文件夹包含的文件而且可以查看文件权限(包括目录、文件夹、文件权限)查看目录信息等等。ls 命令在日常的linux操作中用的很多!

1. 命令格式:

ls [选项] [目录名]

2. 命令功能:

列出目标目录中所有的子目录和文件。

3. 常用参数:

-a  (–all )  目录下的所有文件,包括以 . 开头的隐含文件

-A  同-a,但不列出“.”(表示当前目录)和“..”(表示当前目录的父目录)。

-c  根据 ctime 排序

   配合 -lt:根据 ctime 排序及显示 ctime (文件状态最后更改的时间)(详细列表)

   配合 -l:显示 ctime 但根据名称排序(详细列表)

   

-t 和 -c 区别

-c 的显示输出文件的 i 节点的修改时间 并排序,

-t  显示的是文件修改时间 并排序。

 1 [root@dream ~]# ls -c -lt
 2 总用量 24
 3 -rw-r--r--. 1 root root   50 12月  8 22:50 test.txt
 4 drwxr-xr-x. 2 root root 4096 12月  8 22:37 testln
 5 -rw-r--r--. 1 root root    0 12月  7 01:16 bashrc
 6 -rw-------. 1 root root 1108 12月  6 23:39 anaconda-ks.cfg
 7 -rw-r--r--. 1 root root 3384 12月  6 23:39 install.log.syslog
 8 -rw-r--r--. 1 root root 8015 12月  6 23:39 install.log
 9 
10 [root@dream ~]# ls -lt
11 总用量 24
12 -rw-r--r--. 1 root root   50 12月  8 22:50 test.txt
13 drwxr-xr-x. 2 root root 4096 12月  8 22:37 testln
14 -rw-r--r--. 1 root root    0 12月  7 01:16 bashrc
15 -rw-------. 1 root root 1108 12月  6 23:39 anaconda-ks.cfg
16 -rw-r--r--. 1 root root 8015 12月  6 23:38 install.log
17 -rw-r--r--. 1 root root 3384 12月  6 23:37 install.log.syslog

同时带 -c 和 -t ,以 -c 为主

1 [root@dream ~]# ls -c -t
2 test.txt  testln  bashrc  anaconda-ks.cfg  install.log.syslog  install.log
3 [root@dream ~]# ls -c
4 test.txt  testln  bashrc  anaconda-ks.cfg  install.log.syslog  install.log
5 [root@dream ~]# ls t
6 ls: 无法访问t: 没有那个文件或目录
7 [root@dream ~]# ls -t
8 test.txt  testln  bashrc  anaconda-ks.cfg  install.log  install.log.syslog
9 [root@dream ~]# 

 

-C   每栏由上至下列出项目,按列输出,纵向排序

 1 [root@dream ~]# ls -lc
 2 总用量 24
 3 -rw-------. 1 root root 1108 12月  6 23:39 anaconda-ks.cfg
 4 -rw-r--r--. 1 root root    0 12月  7 01:16 bashrc
 5 -rw-r--r--. 1 root root 8015 12月  6 23:39 install.log
 6 -rw-r--r--. 1 root root 3384 12月  6 23:39 install.log.syslog
 7 drwxr-xr-x. 2 root root 4096 12月  8 22:37 testln
 8 -rw-r--r--. 1 root root   50 12月  8 22:50 test.txt
 9 [root@dream ~]# ls -lC
10 anaconda-ks.cfg  bashrc  install.log  install.log.syslog  testln  test.txt

 

-d  –directory 将目录象文件一样显示,而不是显示其下的文件。

-D  –dired 产生适合 Emacs 的 dired 模式使用的结果

-f   对输出的文件不进行排序,-aU 选项生效,-lst 选项失效

-g  类似 -l,但不列出所有者

-G  (–no-group)  不列出任何有关组的信息

-h  (–human-readable)   以容易理解的格式列出文件大小 (例如 1K 234M 2G)

单独使用没有效果,需要跟能显示文件大小的命令用在一起,将文件大小的格式进行了转换

 1 [root@dream ~]# ls -h
 2 anaconda-ks.cfg  bashrc  install.log  install.log.syslog  testln  test.txt
 3 [root@dream ~]# ls -lh
 4 总用量 24K
 5 -rw-------. 1 root root 1.1K 12月  6 23:39 anaconda-ks.cfg
 6 -rw-r--r--. 1 root root    0 12月  7 01:16 bashrc
 7 -rw-r--r--. 1 root root 7.9K 12月  6 23:38 install.log
 8 -rw-r--r--. 1 root root 3.4K 12月  6 23:37 install.log.syslog
 9 drwxr-xr-x. 2 root root 4.0K 12月  8 22:37 testln
10 -rw-r--r--. 1 root root   50 12月  8 22:50 test.txt
11 [root@dream ~]# ls -hg
12 总用量 24K
13 -rw-------. 1 root 1.1K 12月  6 23:39 anaconda-ks.cfg
14 -rw-r--r--. 1 root    0 12月  7 01:16 bashrc
15 -rw-r--r--. 1 root 7.9K 12月  6 23:38 install.log
16 -rw-r--r--. 1 root 3.4K 12月  6 23:37 install.log.syslog
17 drwxr-xr-x. 2 root 4.0K 12月  8 22:37 testln
18 -rw-r--r--. 1 root   50 12月  8 22:50 test.txt

 

-H  (–dereference-command-line)  使用命令列中的符号链接指示的真正目的地

-i   (–inode)  印出每个文件的 inode 号(节点号)

-I  (–ignore = 样式 )  不印出任何符合 shell 万用字符<样式>的项目

-k  (–block-size=1K)  以 k 字节的形式表示文件的大小。

-l   除了文件名之外,还将文件的权限、所有者、文件大小等信息详细列出来。

-L  (–dereference)  当显示符号链接的文件信息时,显示符号链接所指示的对象而并非符号链接本身的信息 

 1 [root@dream testln]# ls -lL
 2 总用量 8
 3 -rw-r--r--. 1 root root 50 12月  8 22:50 gege.txt
 4 -rw-r--r--. 1 root root 49 12月  8 22:36 haha.txt
 5 [root@dream testln]# ls -l
 6 总用量 4
 7 lrwxrwxrwx. 1 root root 14 12月  8 22:30 gege.txt -> /root/test.txt
 8 -rw-r--r--. 1 root root 49 12月  8 22:36 haha.txt
 9 [root@dream testln]# ls -L
10 gege.txt  haha.txt
11 [root@dream testln]# ls -l
12 总用量 4
13 lrwxrwxrwx. 1 root root 14 12月  8 22:30 gege.txt -> /root/test.txt
14 -rw-r--r--. 1 root root 49 12月  8 22:36 haha.txt

-m  所有项目以逗号分隔,并填满整行行宽

有了 -m ,就不显示 -l 的效果

1 [root@dream ~]# ls -m
2 anaconda-ks.cfg, bashrc, install.log, install.log.syslog, testln, test.txt
3 [root@dream ~]# ls -lm
4 anaconda-ks.cfg, bashrc, install.log, install.log.syslog, testln, test.txt

-o  类似 -l   显示文件的除组信息外的详细信息。

 1 [root@dream ~]# ls -o
 2 总用量 24
 3 -rw-------. 1 root 1108 12月  6 23:39 anaconda-ks.cfg
 4 -rw-r--r--. 1 root    0 12月  7 01:16 bashrc
 5 -rw-r--r--. 1 root 8015 12月  6 23:38 install.log
 6 -rw-r--r--. 1 root 3384 12月  6 23:37 install.log.syslog
 7 drwxr-xr-x. 2 root 4096 12月  8 22:37 testln
 8 -rw-r--r--. 1 root   50 12月  8 22:50 test.txt
 9 [root@dream ~]# ls -l
10 总用量 24
11 -rw-------. 1 root root 1108 12月  6 23:39 anaconda-ks.cfg
12 -rw-r--r--. 1 root root    0 12月  7 01:16 bashrc
13 -rw-r--r--. 1 root root 8015 12月  6 23:38 install.log
14 -rw-r--r--. 1 root root 3384 12月  6 23:37 install.log.syslog
15 drwxr-xr-x. 2 root root 4096 12月  8 22:37 testln
16 -rw-r--r--. 1 root root   50 12月  8 22:50 test.txt

 

-r   (–reverse)  依相反次序排列

-R  (–recursive)  同时列出所有子目录层

-s  (–size)  以块大小为单位列出所有文件的大小

-S  根据文件大小排序

-t   以文件修改时间排序

-u  根据访问时间排序

   配合 -lt:显示访问时间而且依访问时间排序

   配合 -l:显示访问时间但根据名称排序

-U  不进行排序;依文件系统原有的次序列出项目

-v  根据版本进行排序

-w  (–width=COLS)  自行指定屏幕宽度而不使用目前的数值

-x   逐行列出项目而不是逐栏列出

使用 -x  的时候 -l 不起作用

1 [root@dream ~]# ls -x
2 anaconda-ks.cfg  bashrc  install.log  install.log.syslog  testln  test.txt
3 [root@dream ~]# ls -lx
4 anaconda-ks.cfg  bashrc  install.log  install.log.syslog  testln  test.txt

-X  根据扩展名排序

-1  每行只列出一个文件

只有 -1 没有  -2等等数字

 1 [root@dream ~]# ls -1
 2 anaconda-ks.cfg
 3 bashrc
 4 install.log
 5 install.log.syslog
 6 testln
 7 test.txt
 8 [root@dream ~]# ls -2
 9 ls:无效选项 -- 2
10 请尝试执行"ls --help"来获取更多信息。


 

–color[=WHEN]  控制是否使用色彩分辨文件。WHEN 可以是'never'、'always'或'auto'其中之一

–help  显示此帮助信息并离开

–indicator-style=方式  指定在每个项目名称后加上指示符号<方式>:none (默认),classify (-F),file-type (-p)

–si  类似 -h,但文件大小取 1000 的次方而不是 1024

–sort=WORD  以下是可选用的 WORD 和它们代表的相应选项:

extension -X status -c

none -U time -t

size -S atime -u

time -t access -u

version -v use -u

–version  显示版本信息并离开

 

4. 常用范例:

例一:列出/home/peidachang文件夹下的所有文件和目录的详细资料

命令:ls -l -R /home/peidachang

在使用 ls 命令时要注意命令的格式:在命令提示符后,首先是命令的关键字,接下来是命令参数,在命令参数之前要有一短横线“-”,所有的命令参数都有特定的作用,自己可以根据需要选用一个或者多个参数,在命令参数的后面是命令的操作对象。在以上这条命令“ ls -l -R /home/peidachang”中,“ls” 是命令关键字,“-l -R”是参数,“ /home/peidachang”是命令的操作对象。在这条命令中,使用到了两个参数,分别为“l”和“R”,当然,你也可以把他们放在一起使用,如下所示:

命令:ls -lR /home/peidachang

这种形式和上面的命令形式执行的结果是完全一样的。另外,如果命令的操作对象位于当前目录中,可以直接对操作对象进行操作;如果不在当前目录则需要给出操作对象的完整路径,例如上面的例子中,我的当前文件夹是peidachang文件夹,我想对home文件夹下的peidachang文件进行操作,我可以直接输入 ls -lR peidachang,也可以用 ls -lR /home/peidachang。 

例二:列出当前目录中所有以“t”开头的目录的详细内容,可以使用如下命令:

命令:ls -l t*   

可以查看当前目录下文件名以“t”开头的所有文件的信息。其实,在命令格式中,方括号内的内容都是可以省略的,对于命令ls而言,如果省略命令参数和操作对象,直接输入“ ls ”,则将会列出当前工作目录的内容清单。

例三:只列出文件下的子目录

命令:ls -F /opt/soft |grep /$  

列出 /opt/soft 文件下面的子目录

输出

 

1 [root@localhost opt]# ls -F /opt/soft |grep /$
2 jdk1.6.0_16/
3 subversion-1.6.1/
4 tomcat6.0.32/

命令:ls -l /opt/soft | grep "^d"

列出 /opt/soft 文件下面的子目录详细情况

输出

 

1 [root@localhost opt]#  ls -l /opt/soft | grep "^d"
2 drwxr-xr-x 10 root root      4096 09-17 18:17 jdk1.6.0_16
3 drwxr-xr-x 16 1016 1016      4096 10-11 03:25 subversion-1.6.1
4 drwxr-xr-x  9 root root      4096 2011-11-01 tomcat6.0.32

 

例四列出目前工作目录下所有名称是s 开头的档案,愈新的排愈后面,可以使用如下命令:

命令ls -ltr s*

输出

 

 1 [root@localhost opt]# ls -ltr s*
 2 src:
 3 总计 0
 4 script:
 5 总计 0
 6 soft:
 7 总计 350644
 8 
 9 drwxr-xr-x  9 root root      4096 2011-11-01 tomcat6.0.32
10 -rwxr-xr-x  1 root root  81871260 09-17 18:15 jdk-6u16-linux-x64.bin
11 drwxr-xr-x 10 root root      4096 09-17 18:17 jdk1.6.0_16
12 -rw-r--r--  1 root root 205831281 09-17 18:33 apache-tomcat-6.0.32.tar.gz
13 -rw-r--r--  1 root root   5457684 09-21 00:23 tomcat6.0.32.tar.gz
14 -rw-r--r--  1 root root   4726179 10-10 11:08 subversion-deps-1.6.1.tar.gz
15 -rw-r--r--  1 root root   7501026 10-10 11:08 subversion-1.6.1.tar.gz
16 drwxr-xr-x 16 1016 1016      4096 10-11 03:25 subversion-1.6.1

 

例五:列出目前工作目录下所有档案及目录;目录于名称后加"/", 可执行档于名称后加"*" 

命令ls -AF

输出

 

1 [root@localhost opt]# ls -AF
2 log/  script/  soft/  src/  svndata/  web/

 

例六:计算当前目录下的文件数和目录数

命令:

ls -l * |grep "^-"|wc -l ---文件个数  

ls -l * |grep "^d"|wc -l    ---目录个数

例七: 在ls中列出文件的绝对路径

命令:ls | sed "s:^:`pwd`/:"

输出

    

1 [root@localhost opt]# ls | sed "s:^:`pwd`/:" 
2 /opt/log
3 /opt/script
4 /opt/soft
5 /opt/src
6 /opt/svndata
7 /opt/web

 

例九:列出当前目录下的所有文件(包括隐藏文件)的绝对路径, 对目录不做递归

命令:find $PWD -maxdepth 1 | xargs ls -ld

输出

 

1 [root@localhost opt]# find $PWD -maxdepth 1 | xargs ls -ld
2 drwxr-xr-x 8 root root 4096 10-11 03:43 /opt
3 drwxr-xr-x 2 root root 4096 2012-03-08 /opt/log
4 drwxr-xr-x 2 root root 4096 2012-03-08 /opt/script
5 drwxr-xr-x 5 root root 4096 10-11 03:21 /opt/soft
6 drwxr-xr-x 2 root root 4096 2012-03-08 /opt/src
7 drwxr-xr-x 4 root root 4096 10-11 05:22 /opt/svndata
8 drwxr-xr-x 4 root root 4096 10-09 00:45 /opt/web

 

例十:递归列出当前目录下的所有文件(包括隐藏文件)的绝对路径

命令: find $PWD | xargs ls -ld 

 

例十一:指定文件时间输出格式

命令:

 ls -tl --time-style=full-iso

输出:

1 [root@localhost soft]# ls -tl --time-style=full-iso 
2 总计 350644
3 drwxr-xr-x 16 1016 1016 4096 2012-10-11 03:25:58.000000000 +0800 subversion-1.6.1

 

 ls -ctl --time-style=long-iso

输出

 

1 [root@localhost soft]# ls -ctl --time-style=long-iso
2 总计 350644
3 drwxr-xr-x 16 1016 1016      4096 2012-10-11 03:25 subversion-1.6.1

 

扩展:

1. 显示彩色目录列表

    打开/etc/bashrc, 加入如下一行:

    alias ls="ls --color"

    下次启动bash时就可以像在Slackware里那样显示彩色的目录列表了, 其中颜色的含义如下:

    1. 蓝色-->目录

    2. 绿色-->可执行文件

    3. 红色-->压缩文件

    4. 浅蓝色-->链接文件

    5. 灰色-->其他文件

posted @ 2015-12-08 17:15  小兔dream  阅读(204)  评论(0)    收藏  举报