ls命令
ls命令用来显示目标列表,是在使用linux时使用率较高的命令。
语法
ls [op] 参数
op
-a:显示所有文件和目录
root@f52b90085a14:/d/go/src/webapi/go/bin# ls -a
. .. go godoc gofmt
-A:显示除了隐藏文件“.”和“..”以外的所有文件列表
root@f52b90085a14:/d/go/src/webapi/go/bin# ls -A
go godoc gofmt
-C:多列显示输出结果。(默认选项)
root@f52b90085a14:/d/go/src/webapi/go/bin# ls -C
go godoc gofmt
-l:和“-C”选项功能相反,所有输出信息用单列格式输出,不输出为多列
root@f52b90085a14:/d/go/src/webapi/go/bin# ls -l
total 28288
-rwxr-xr-x 1 root root 10377652 Oct 4 18:47 go
-rwxr-xr-x 1 root root 15325248 Oct 4 18:54 godoc
-rwxr-xr-x 1 root root 3257829 Oct 4 18:47 gofmt
-F:在每个输出项后追加文件的类型标识符,具体含义:“*”表示具有可执行权限的普通文件,“/”表示目录,“@”表示符号链接,“|”表示命令管道FIFO,“=”表示sockets套接字。当文件为普通文件时,不输出如何标识符
root@f52b90085a14:/d/go/src/webapi/go/bin# ls -F
go* godoc* gofmt*
-b:将文件中不可输出的字符以反斜线‘\’加字符编码的方式输出。
root@f52b90085a14:/d/go/src/webapi/go/bin# ls -b
go godoc gofmt
-c:与“-lt”选项连用是,按照时间状态时间排序输出目录内容,排序的依据是文件的索引节点的ctime字段。与“-l”选项连用时,则排序的依据是文件的状态改变时间。
root@f52b90085a14:/d/go/src/webapi/go/bin# ls -ltc
total 28288
-rwxr-xr-x 1 root root 3257829 Oct 16 03:13 gofmt
-rwxr-xr-x 1 root root 15325248 Oct 16 03:13 godoc
-rwxr-xr-x 1 root root 10377652 Oct 16 03:13 go
root@f52b90085a14:/d/go/src/webapi/go/bin# ls -lc
total 28288
-rwxr-xr-x 1 root root 10377652 Oct 16 03:13 go
-rwxr-xr-x 1 root root 15325248 Oct 16 03:13 godoc
-rwxr-xr-x 1 root root 3257829 Oct 16 03:13 gofmt
-d:仅显示目录名,而不显示目录下内容列表。显示符号链接本身,而不显示其所指向其所指向的目录列表。
root@f52b90085a14:/d/go/src/webapi/go/bin# ls -d
.
-f:此参数的效果和同时指定“aU”参数相同,并关闭“lst”参数的效果
root@f52b90085a14:/d/go/src/webapi/go/bin# ls -f
godoc .. . gofmt go
-i:显示文件索引节点号(inode)。一个索引节点代表一个文件。
root@f52b90085a14:/d/go/src/webapi/go/bin# ls -i
545846 go 545848 godoc 545852 gofmt
–file-type:和“-F”选项功能相同,但是不显示“*”
root@f52b90085a14:/d/go/src/webapi/go/bin# ls --file-type
go godoc gofmt
-k:以kb(千字节)为单位显示文件大小
-l:以长格式显示目录下的内容列表。输出的信息从左到右依次包括文件名、文件类型、权限模式、硬链接数、所有者、组、文件大小和文件最后修改时间等。
root@f52b90085a14:/d/go/src/webapi/go# ls -l
total 176
-rw-r--r-- 1 root root 41258 Oct 4 18:46 AUTHORS
-rw-r--r-- 1 root root 1576 Oct 4 18:46 CONTRIBUTING.md
-rw-r--r-- 1 root root 55577 Oct 4 18:46 CONTRIBUTORS
-rw-r--r-- 1 root root 1479 Oct 4 18:46 LICENSE
-rw-r--r-- 1 root root 1303 Oct 4 18:46 PATENTS
-rw-r--r-- 1 root root 1601 Oct 4 18:46 README.md
-rw-r--r-- 1 root root 7 Oct 4 18:46 VERSION
drwxr-xr-x 2 root root 4096 Oct 4 18:46 api
drwxr-xr-x 2 root root 4096 Oct 4 18:54 bin
drwxr-xr-x 4 root root 4096 Oct 4 18:54 blog
drwxr-xr-x 9 root root 4096 Oct 4 18:46 doc
-rw-r--r-- 1 root root 5686 Oct 4 18:46 favicon.ico
drwxr-xr-x 3 root root 4096 Oct 4 18:46 lib
drwxr-xr-x 14 root root 4096 Oct 4 18:54 misc
drwxr-xr-x 8 root root 4096 Oct 4 18:54 pkg
-rw-r--r-- 1 root root 26 Oct 4 18:46 robots.txt
drwxr-xr-x 46 root root 4096 Oct 4 18:46 src
drwxr-xr-x 19 root root 12288 Oct 4 18:54 test
-m:用“,”号隔开每个文件和目录的名称
root@f52b90085a14:/d/go/src/webapi/go# ls -m
AUTHORS, CONTRIBUTING.md, CONTRIBUTORS, LICENSE, PATENTS, README.md, VERSION, api, bin, blog, doc, favicon.ico, lib,
misc, pkg, robots.txt, src, test
-n:以用户设别码和群组识别码替代其名称
root@f52b90085a14:/d/go/src/webapi/go# ls -n
total 176
-rw-r--r-- 1 0 0 41258 Oct 4 18:46 AUTHORS
-rw-r--r-- 1 0 0 1576 Oct 4 18:46 CONTRIBUTING.md
-rw-r--r-- 1 0 0 55577 Oct 4 18:46 CONTRIBUTORS
-rw-r--r-- 1 0 0 1479 Oct 4 18:46 LICENSE
-rw-r--r-- 1 0 0 1303 Oct 4 18:46 PATENTS
-rw-r--r-- 1 0 0 1601 Oct 4 18:46 README.md
-rw-r--r-- 1 0 0 7 Oct 4 18:46 VERSION
drwxr-xr-x 2 0 0 4096 Oct 4 18:46 api
drwxr-xr-x 2 0 0 4096 Oct 4 18:54 bin
drwxr-xr-x 4 0 0 4096 Oct 4 18:54 blog
drwxr-xr-x 9 0 0 4096 Oct 4 18:46 doc
-rw-r--r-- 1 0 0 5686 Oct 4 18:46 favicon.ico
drwxr-xr-x 3 0 0 4096 Oct 4 18:46 lib
drwxr-xr-x 14 0 0 4096 Oct 4 18:54 misc
drwxr-xr-x 8 0 0 4096 Oct 4 18:54 pkg
-rw-r--r-- 1 0 0 26 Oct 4 18:46 robots.txt
drwxr-xr-x 46 0 0 4096 Oct 4 18:46 src
drwxr-xr-x 19 0 0 12288 Oct 4 18:54 test
可以看出root都变成了0
-r:以文件名反序排列并输出目录内容列表
root@f52b90085a14:/d/go/src/webapi/go# ls -r
test robots.txt misc favicon.ico blog api README.md LICENSE CONTRIBUTING.md
src pkg lib doc bin VERSION PATENTS CONTRIBUTORS AUTHORS
-s:显示文件和目录的大小,以区块为单位
root@f52b90085a14:/d/go/src/webapi/go# ls -s
total 176
44 AUTHORS 56 CONTRIBUTORS 4 PATENTS 4 VERSION 4 bin 4 doc 4 lib 4 pkg 4 src
4 CONTRIBUTING.md 4 LICENSE 4 README.md 4 api 4 blog 8 favicon.ico 4 misc 4 robots.txt 12 test
-t:用文件和目录的更改时间排序
root@f52b90085a14:/d/go/src/webapi/go# ls -t
bin misc test src lib CONTRIBUTING.md LICENSE README.md favicon.ico
blog pkg VERSION doc AUTHORS CONTRIBUTORS PATENTS api robots.txt
-L:如果遇到为符号链接的文件或目录,直接列出链接指向的原始文件或目录
-R:递归处理,将制定目录下的所有文件及子目录一并处理
–full-time:列出完整的日期与时间
root@f52b90085a14:/d/go/src/webapi/go# ls --full-time
total 176
-rw-r--r-- 1 root root 41258 2017-10-04 18:46:09.000000000 +0000 AUTHORS
-rw-r--r-- 1 root root 1576 2017-10-04 18:46:09.000000000 +0000 CONTRIBUTING.md
-rw-r--r-- 1 root root 55577 2017-10-04 18:46:09.000000000 +0000 CONTRIBUTORS
-rw-r--r-- 1 root root 1479 2017-10-04 18:46:09.000000000 +0000 LICENSE
-rw-r--r-- 1 root root 1303 2017-10-04 18:46:09.000000000 +0000 PATENTS
-rw-r--r-- 1 root root 1601 2017-10-04 18:46:09.000000000 +0000 README.md
-rw-r--r-- 1 root root 7 2017-10-04 18:46:18.000000000 +0000 VERSION
drwxr-xr-x 2 root root 4096 2017-10-04 18:46:09.000000000 +0000 api
drwxr-xr-x 2 root root 4096 2017-10-16 05:42:12.663396279 +0000 bin
drwxr-xr-x 4 root root 4096 2017-10-04 18:54:15.000000000 +0000 blog
drwxr-xr-x 9 root root 4096 2017-10-04 18:46:10.000000000 +0000 doc
-rw-r--r-- 1 root root 5686 2017-10-04 18:46:09.000000000 +0000 favicon.ico
drwxr-xr-x 3 root root 4096 2017-10-04 18:46:10.000000000 +0000 lib
drwxr-xr-x 14 root root 4096 2017-10-04 18:54:15.000000000 +0000 misc
drwxr-xr-x 8 root root 4096 2017-10-04 18:54:14.000000000 +0000 pkg
-rw-r--r-- 1 root root 26 2017-10-04 18:46:09.000000000 +0000 robots.txt
drwxr-xr-x 46 root root 4096 2017-10-04 18:46:13.000000000 +0000 src
drwxr-xr-x 19 root root 12288 2017-10-04 18:54:06.000000000 +0000 test
–color[=auto/never]:使用不同的颜色高亮显示不同类型
参数
制定要显示列表的目录,也可以是具体的文件
命令shell返回值
Exit status:
0 if OK,
1 if minor problems (e.g., cannot access subdirectory),
2 if serious trouble (e.g., cannot access command-line argument).
0:表示执行成功
1、表示参数错误(比如目录不存在)
2、表示op不存在