2015-06-26创建
1、概述:
Linux ls 命令 列出目录内容
2、命令格式:
ls [OPTION]... [FILE]... 中文:ls [选项]... 文件...
3、常用示例:
ls 列出所有文件
ls -l 列出所有文件,并显示详细信息。信息内容见请看下面内容
la -a 列出所有文件,并显示隐藏文件。
4、常用命令参数:
-a, --all 列出所有文件,包括以 "." 开头的文件【包括 . .. 目录 和 . 开头的隐藏文件】。
-A, --almost-all 列出所有文件,包括以 "." 开头的隐藏文件,但是 不列出 . 和 .. 目录
-c 通常和 -l 一起使用。内容列表显示改变时间,而不是修改时间
-d, --directory 列出对应目录,而不是列出目录中的内容。并且不会引用符号链接。示例如下:
-bash-4.1$ ls -d
.
-bash-4.1$ ls -dl
drwx------ 8 yuanyl yuanyl 4096 6月 11 14:56 .
-bash-4.1$ ls /mnt
db_ysbak jdk1.7.0_67 lost+found pmcs2 rsync-3.0.9 svndata ys
-bash-4.1$ ls -d /mnt
/mnt
-F, --classify 对列出的文件追加标记。以区分是什么文件 (*/=>@|)
[root@mode-6 ~]# ls -lF /etc/ # 自己查看
[root@mode-6 ~]# ls -F /etc/ # 自己查看
--full-time 显示详细时间。就像 -l --time-style=full-iso
[root@mode-6 ~]# ls --full-time
total 68
-rw-------. 1 root root 1266 2015-10-02 04:06:41.682999208 +0800 anaconda-ks.cfg
drwxr-xr-x 2 root root 4096 2015-10-09 20:24:31.884215974 +0800 day03
drwxr-xr-x. 12 root root 4096 2015-10-03 18:12:47.866268463 +0800 day04
-rw-r--r--. 1 root root 39693 2012-12-11 15:23:50.000000000 +0800 install.log
-rw-r--r--. 1 root root 8468 2015-10-02 04:05:44.895999231 +0800 install.log.syslog
-g 就像 -l, 但是不列出
属主
[root@mode-6 ~]# ll
total 68
-rw-r--r-- 1 oldboy root 0 Oct 9 21:18 test
[root@mode-6 ~]# ls -g
total 68
-rw-r--r-- 1 root 0 Oct 9 21:18 test # 不显示<strong>属主</strong>
-G, --no-group 在详情列表中,不显示
属组
[root@mode-6 ~]# ls -l
total 68
-rw-r--r-- 1 oldboy root 0 Oct 9 21:18 test
[root@mode-6 ~]# ls -lG
total 68
-rw-r--r-- 1 oldboy 0 Oct 9 21:18 test # 不显示<strong>属组</strong>
-h, --human-readable 和 -l 一起使用。在文件长度(大小)一列,会显示为我们熟悉的格式,比如:(2K, 225M, 1.5G)。示例如下:
[root@moban ~]# ls -l
total 76
-rw-r--r--. 1 root root 13 Jun 23 00:39 aaa
-rw-------. 1 root root 1106 Jun 22 22:29 anaconda-ks.cfg
drwxr-xr-x. 9 root root 4096 Jun 26 06:53 data
[root@moban ~]# ls -hl
total 76K
-rw-r--r--. 1 root root 13 Jun 23 00:39 aaa
-rw-------. 1 root root 1.1K Jun 22 22:29 anaconda-ks.cfg
drwxr-xr-x. 9 root root 4.0K Jun 26 06:53 data
--si 使用 1000 而不是 1024 作为转换。
-i, --inode 打印每个文件的索引编号。实例如下:
[root@moban ~]# ls -lhi
total 76K
916130 -rw-r--r--. 1 root root 13 Jun 23 00:39 aaa
916125 -rw-------. 1 root root 1.1K Jun 22 22:29 anaconda-ks.cfg
915722 drwxr-xr-x. 9 root root 4.0K Jun 26 06:53 data
-l
输出文件的详细信息。
[root@moban ~]# ls -l
total 76
-rw-r--r--. 1 root root 13 Jun 23 00:39 aaa
-rw-------. 1 root root 1106 Jun 22 22:29 anaconda-ks.cfg
drwxr-xr-x. 9 root root 4096 Jun 26 06:53 data
上面的7列信息分别为:
第一列:第一个字符代表文件类型
-:普通文件(f)
d:目录文件
b:块设备文件(block)
c:字符设备文件(character)
l:符号链接文件(symbolic link file)
p:命令管道文件(pipe)
s:套接字文件(socket)
后面9位是该文件权限:每三位一组 rwx(读写执行), - 代表没有这个权限。
第二列:文件的硬链接次数。普通文件至少一次链接,目录至少两次链接(分别为 . ..)
第三列:文件的属主
第四列:文件的属组,即用户组
第五列:文件大小,单位是字节
第六列:文件最近修改时间,修改文件意味着对其内文件或子目录的增添和修改
第七列:文件名
对于目录来说 rwx
其中r :代表读取目录中的内容
w:删除、修改 目录里面文件名的权限 【必须配合 x 执行才可以】
x:代表能否进入到目录里面去
-m 使用逗号分隔显示信息。
-n, --numeric-uid-gid 就像 -l ,但是只列出 属主和属组的ID号
[root@mode-6 ~]# ls -n
total 68
drwxr-xr-x. 12 0 0 4096 Oct 9 21:46 day04
-rw-r--r--. 1 0 0 39693 Dec 11 2012 install.log
-rw-r--r-- 1 500 0 0 Oct 9 21:18 test
-o 就像 -l ,但是不会列出组信息
[root@mode-6 ~]# ls -o
total 68
drwxr-xr-x. 12 root 4096 Oct 9 21:46 day04
-rw-r--r--. 1 root 39693 Dec 11 2012 install.log
-rw-r--r-- 1 oldboy 0 Oct 9 21:18 test
-p, --indicator-style=slash 如果是目录,那么就在文件名后面 追加 "/"
[root@mode-6 ~]# ls -p
anaconda-ks.cfg day03/ day04/ install.log install.log.syslog test
-Q, --quote-name 对文件名添加双引号
[root@mode-6 ~]# ls -Q
"anaconda-ks.cfg" "day03" "day04" "install.log" "install.log.syslog" "test"
-F, --classify 根据文件类型的不同追加不同的信息【append indicator (one of */=>@|) to entries】
-r, --reverse 有排序时,那么倒序显示
-R 列出当前目录和子目录文件,使用“递归”显示
-S 根据文件大小排序(由大到小)
-s, --size 打印每个文件的分配大小,在块中
[root@mode-6 ~]# ll -s
total 72
4 -rw-------. 1 root root 1266 Oct 2 04:06 anaconda-ks.cfg
4 drwxr-xr-x 2 root root 4096 Oct 9 20:24 day03
4 drwxr-xr-x. 12 root root 4096 Oct 9 21:46 day04
44 -rw-r--r--. 1 root root 39693 Dec 11 2012 install.log
12 -rw-r--r--. 1 root root 8468 Oct 2 04:05 install.log.syslog
4 -rw-r--r-- 1 oldboy root 49 Oct 9 22:10 test
--sort=WORD 使用 WORD 替代 选项。none -U, extension -X, size -S, time -t, version -v
[root@mode-6 ~]# ll --sort=WORD
ls: invalid argument `WORD' for `--sort'
Valid arguments are:
- `none'
- `time'
- `size'
- `extension'
- `version'
Try `ls --help' for more information.
[root@mode-6 ~]# ll --sort=size
total 72
-rw-r--r--. 1 root root 39693 Dec 11 2012 install.log
-rw-r--r--. 1 root root 8468 Oct 2 04:05 install.log.syslog
drwxr-xr-x 2 root root 4096 Oct 9 20:24 day03
drwxr-xr-x. 12 root root 4096 Oct 9 21:46 day04
-rw-------. 1 root root 1266 Oct 2 04:06 anaconda-ks.cfg
-rw-r--r-- 1 oldboy root 49 Oct 9 22:10 test
[root@mode-6 ~]# ll --sort=time
total 72
-rw-r--r-- 1 oldboy root 49 Oct 9 22:10 test
drwxr-xr-x. 12 root root 4096 Oct 9 21:46 day04
drwxr-xr-x 2 root root 4096 Oct 9 20:24 day03
-rw-------. 1 root root 1266 Oct 2 04:06 anaconda-ks.cfg
-rw-r--r--. 1 root root 8468 Oct 2 04:05 install.log.syslog
-rw-r--r--. 1 root root 39693 Dec 11 2012 install.log
--time-style=STYLE 和 -l 一起使用。STYLE 为: full-iso, long-iso, iso, locale, +FORMAT。其中 +FORMAT 和 'date' 一样。
[root@mode-6 ~]# ll --time-style=full-iso # 合适
total 72
-rw-------. 1 root root 1266 2015-10-02 04:06:41.682999208 +0800 anaconda-ks.cfg
drwxr-xr-x. 12 root root 4096 2015-10-09 21:46:57.130804079 +0800 day04
-rw-r--r--. 1 root root 39693 2012-12-11 15:23:50.000000000 +0800 install.log
-rw-r--r-- 1 oldboy root 49 2015-10-09 22:10:47.311863858 +0800 test
[root@mode-6 ~]#
[root@mode-6 ~]# ll --time-style=long-iso # 合适
total 72
-rw-------. 1 root root 1266 2015-10-02 04:06 anaconda-ks.cfg
drwxr-xr-x. 12 root root 4096 2015-10-09 21:46 day04
-rw-r--r--. 1 root root 39693 2012-12-11 15:23 install.log
-rw-r--r-- 1 oldboy root 49 2015-10-09 22:10 test
[root@mode-6 ~]# ll --time-style=iso
total 72
-rw-------. 1 root root 1266 10-02 04:06 anaconda-ks.cfg
drwxr-xr-x. 12 root root 4096 10-09 21:46 day04
-rw-r--r--. 1 root root 39693 2012-12-11 install.log
-rw-r--r-- 1 oldboy root 49 10-09 22:10 test
[root@mode-6 ~]# ll --time-style=locale
total 72
-rw-------. 1 root root 1266 Oct 2 04:06 anaconda-ks.cfg
drwxr-xr-x. 12 root root 4096 Oct 9 21:46 day04
-rw-r--r--. 1 root root 39693 Dec 11 2012 install.log
-rw-r--r-- 1 oldboy root 49 Oct 9 22:10 test
-t 根据修改时间排序(离当前时间越近,越靠前)
-x 用直线代替按列的列表项
[root@mode-6 ~]# ls -x /etc/ # 自己看区别
[root@mode-6 ~]# ls /etc/ # 自己看区别
-1 (数字 1),一行显示一个文件
[root@mode-6 ~]# ls
anaconda-ks.cfg day03 day04 install.log install.log.syslog test
[root@mode-6 ~]# ls -1
anaconda-ks.cfg
day03
day04
install.log
install.log.syslog
test
。。。。。其他的暂时不列了
5、当前命令所在位置和类型 :
[root@moban ~]# which ls
alias ls='ls --color=auto'
/bin/ls
[root@moban ~]# type ls
ls is aliased to `ls --color=auto'