ls命令用于列出文件和目录。默认上,会列出当前目录的内容。带上参数后,我们可以用ls做更多的事情。

1.不带参数运行ls会只列出文件或者目录

[root@one ~]# ls
anaconda-ks.cfg  install.log  install.log.syslog  shell

2. ls -l (小写L) 列出文件详细信息

[root@one ~]# ls -l
总用量 40
-rw-------. 1 root root  1019 1月  20 22:44 anaconda-ks.cfg
-rw-r--r--. 1 root root  20412 1月  20 22:43 install.log
-rw-r--r--. 1 root root  6082 1月  20 22:42 install.log.syslog
drwxr-xr-x. 2 root root  4096 1月  26 19:29 shell
[root@one ~]#

第一列:drwxr-xr-x的第一个字符:“d”表示目录、“-”表示普通文件、“l”表示链接文件,“b”表示设置文件。后面的九个字符每三个为一组,分别表示文件所有者、文件所有者所在用户组、其他用户对文件的权限。rwx表示可读、可写、可执行。

第二列:表示有多少链接指向这个文件。

第三列:文件的所有者(属主)

第四列:文件的所有组(属组)

第五列:该文件的大小,以字节为单位。

第六列:该文件最后的修改时间。

第七列:文件名或目录名

3. ls -a 查看该目录下所有的文件,包括隐藏文件。

[root@one ~]# ls -a
.                .bash_history  .bashrc      install.log.syslog  .tcshrc
..               .bash_logout   .cshrc        shell           .viminfo
anaconda-ks.cfg      .bash_profile  install.log    .ssh
[root@one ~]#

4. ls -d 只查看目录。

通常和和-l搭配使用,ls -ld查看该目录本身的信息。

[root@one ~]# ls -ld .ssh/
drwx------. 2 root root 4096 1月  23 00:29 .ssh/

5.ls -t 按修改时间排序,最新的文件在最上面

通常和和-l搭配使。也可查看指定目录。

[root@one ~]# ls -lt /tmp/
总用量 8
srwxrwxrwx  1 mysql mysql    0 2月  22 21:28 mysql.sock
srw-rw-rw-  1 root  root     0 2月  22 21:28 php-fcgi.sock
drwxr-xr-x. 3 root  root  4096 1月  24 02:29 pear
-rw-r--r--. 1 root  root     5 1月  23 00:48 12.txt
-rw-------. 1 root  root     0 1月  20 22:34 yum.log

6.ls -i  可以查看文件的inode号。

[root@one ~]# ls -i
787663 anaconda-ks.cfg  783364 install.log.syslog
783363 install.log      787665 shell

7.ls -lh 用“K、M、G”显示文件或目录的大小

[root@one ~]# ls -lh /usr/
总用量 108K
dr-xr-xr-x.  2 root root  28K 1月  29 23:53 bin
drwxr-xr-x.  2 root root 4.0K 9月  23 2011 etc
drwxr-xr-x.  2 root root 4.0K 9月  23 2011 games
drwxr-xr-x. 49 root root 4.0K 1月  24 02:06 include
dr-xr-xr-x. 52 root root  32K 1月  29 23:08 lib
drwxr-xr-x. 14 root root 4.0K 1月  24 01:11 libexec
drwxr-xr-x. 16 root root 4.0K 1月  24 03:01 local
dr-xr-xr-x.  2 root root  12K 1月  24 02:05 sbin
drwxr-xr-x. 97 root root 4.0K 1月  24 02:06 share
drwxr-xr-x.  4 root root 4.0K 1月  20 22:36 src
lrwxrwxrwx.  1 root root   10 1月  20 22:36 tmp -> ../var/tmp

8.ls -R  列出目录以及其子目录

[root@one ~]# ls -R
.:
anaconda-ks.cfg  install.log  install.log.syslog  shell
./shell:
1212.txt  2.expect  4.expect  lanmp.sh
1.expect  3.expect  5.expect  test.expect

9. ls -r 排序时反转顺序

[root@one ~]# ls
anaconda-ks.cfg  install.log  install.log.syslog  shell
[root@one ~]# ls -r
shell  install.log.syslog  install.log  anaconda-ks.cfg