2. 语法
ls [options] [file|directory]
默认情形下,ls 列出当前路径的内容,按文件和目录名字母排序。排序干系的参数:
t : 按末了修正韶光排序,Sort files and directories by their last modification time, displaying the most recently modified ones first.S : 按大小排序,Sort files and directories by their sizes, listing the largest ones first.r : 反向排序,known as reverse order which is used to reverse the default order of listing.详细的参数和用法如下:
用法:ls [选项] [文件]列出给定文件(默认为当前目录)的信息。如果不指定 -cftuvSUX 中任意一个或--sort 选项,则根据字母排序。必选参数对是非选项同时适用。 -a, --all 列出所有文件和目录,包括 . 和 .. -A, --almost-all 列出除 . 及 .. 以外的所有文件和目录 --author 与 -l 同时利用时,列出每个文件的作者 -b, --escape 以 C 风格的转义序列表示不可打印的字符 --block-size=SIZE 与 -l 同时利用时,将文件以给定的SIZE为单位显示 -B, --ignore-backups 不列出任何以 ~ 字符结束的项目 -c 与 -lt 共同利用时:根据 ctime 排序并显示 ctime(末了变动韶光); 与 -l 共同利用时:显示 ctime 并按照名称排序; 其它情形:按照 ctime 排序,最新的排最前 -C 分栏由上至下列出项目 --color[=WHEN] 是否带颜色输出;WHEN 可以是 always(默认)、auto 或 never; -d, --directory 当碰着目录时列出目录本身而非目录内的文件 -D, --dired 产生适宜 Emacs 的 dired 模式利用的结果 -f 不进行排序,-aU 选项生效,禁用 -ls --color -F, --classify 加上文件类型的指示符号(/=@| 个中一个) --file-type 类似,但不包含‘’后缀的情形 --format=word 交错 -x,逗号分隔 -m,水平 -x,长 -l,单栏 -1,详细 -l,垂直 -C --full-time 类似 -l --time-style=full-iso -g 类似 -l,但不列出所有者 --group-directories-first 在文件前分组目录;此选项可与--sort 一起利用, 但是一旦利用 --sort=none (-U) 将禁用分组 -G, --no-group 以一个长列表的形式,不输出组名 -h, --human-readable 与 -l 和 -s 一起,以易于阅读的格式输出文件大小(如 1K 23M 2G等) --si 同上面类似,但是利用 1000 为基底而非 1024 -H, --dereference-command-line 跟随命令行列出的符号链接 --dereference-command-line-symlink-to-dir 跟随命令行列出的指向目录的符号链接 --hide=PATTERN 隐蔽符合 shell PATTERN 模式的项目(-a 或 -A 将覆盖此选项) --hyperlink[=WHEN] 文件名利用超链接;WHEN 可以是 always(默认)、auto 或 never --indicator-style=办法 指定在每个项目名称后加上指示符号办法:none(默认)、slash(-p)、 file-type(--file-type)以及 classify(-F) -i, --inode 显示每个文件的索引编号(inode 号) -I, --ignore=模式 不显示任何匹配指定 shell <模式>的项目 -k, --kibibytes 默认利用 1024 字节的块显示磁盘利用量;只用于 -s 和每个目录的总计 -l 利用较长格式列出信息 -L, --dereference当显示符号链接的文件信息时,显示符号链接所指示的工具而非符号链接本身的信息 -m 所有项目以逗号分隔,并填满整行行宽 -n, --numeric-uid-gid 类似 -l,但列出用户和组的 ID 号 -N, --literal 输出未经引号括起的项目名称 -o 类似 -l,但不列出有关组的信息 -p, --indicator-style=slash 对目录附加“/”作为指示符号 -q, --hide-control-chars 以“?”字符代替无法打印的字符 --show-control-chars 原样显示无法打印的字符(这是默认行为,除非被调用 本程序的名称是“ls”而且是在终端中进行输出) -Q, --quote-name 将条款名称括上双引号 --quoting-style=办法 利用指定引用的办法显示条款标名称: literal, locale, shell, shell-always, shell-escape, shell-escape-always, c, escape (该选项优先于 QUOTING_STYLE 环境变量的配置) -r, --reverse逆序排列 -R, --recursive递归显示子目录 -s, --size以块数形式显示每个文件分配的尺寸 -S sort by file size, largest first --sort=WORD sort by WORD instead of name: none (-U), size (-S), time (-t), version (-v), extension (-X) --time=WORD change the default of using modification times; access time (-u): atime, access, use; change time (-c): ctime, status; birth time: birth, creation; with -l, WORD determines which time to show; with --sort=time, sort by WORD (newest first) --time-style=TIME_STYLE 利用 -l 时显示的韶光/日期格式; -t 按韶光排序,最新的排最前;拜会 --time -T, --tabsize=COLS 指定制表符(Tab)的宽度,而非默认8字符 -u 同 -lt 一起利用:按照访问韶光排序并显示; 同 -l 一起利用:显示访问韶光并按文件名排序 其它:按照访问韶光排序,最新的最最前 -U 不进行排序;按照目录顺序列出项目 -v 在文本中进行数字(版本)的自然排序 -w, --width=COLS 设置输出宽度为 COLS 对应的值。0 代表无限制 -x 逐行列出项目而不是逐栏列出 -X 根据扩展名按字母顺序排序 -Z, --context 输出每个文件的所有安全高下文信息 -1 每行只列出一个文件。与 -q 或 -b 共用时避免 '\n' --help显示此帮助信息并退出 --version显示版本信息并退出退出状态: 0 正常 1 一样平常问题 (例如:无法访问子文件夹) 2 严重问题 (例如:无法利用命令行参数)

从 /etc 目录中列出最近修正过的 3 个文件或目录。
[root@kylin ~]# ls -t /etc | head -3localtime_tmpresolv.confaudit[root@kylin bin]# ls -ld /etcdrwxr-xr-x 104 root root 8192 2月 1 21:08 /etc[root@kylin bin]# ls -lt /etc总用量 1128-rw------- 1 root root 9 2月 1 23:00 localtime_tmp-rw-r--r-- 1 root root 82 2月 1 21:03 resolv.confdrwxr-x--- 4 root root 81 2月 1 21:03 audit...
ls -t
3.2. ls -1 一行显示一个把稳不是字母 l,而是数字 1。
[root@centos data]# ls -11.txt2.mddemo1demo2My songs
ls -1
3.3. ls -l 长格式显示文件/目录信息`ls -l` 简写为 `ll`。
[root@centos data]# ls -ltotal 0-rw-r--r--. 1 root root 10 Jan 30 21:46 1.txt-rw-r--r--. 1 root root 20 Jan 30 21:46 2.mddrwxr-xr-x. 2 root root 44 Jan 31 16:39 demo1drwxr-xr-x. 2 root root 40 Jan 30 21:57 demo2drwxr-xr-x. 2 root root 6 Jan 31 21:52 My songs
ls -l
3.4. ls -lh 符合人类阅读习气办法显示大小h 表示 human readable,会以 K,M,G 为单位来显示文件大小。
[root@kylin bin]# ls -lh总用量 309M-rwxr-xr-x 1 root root 2.7K 1月 2 2019 anaconda-cleanup-rwxr-xr-x 1 root root 102 6月 8 2018 anaconda-disable-nm-ibft-plugin-rwxr-xr-x 1 root root 439K 12月 7 15:43 atril-rwxr-xr-x 1 root root 1.2M 11月 3 2022 bash-rwxr-xr-x 1 root root 201K 7月 28 2023 bccmd-rwxr-xr-x 1 root root 65M 11月 23 09:42 docker
[root@kylin bin]# ls -ld /etcdrwxr-xr-x 104 root root 8192 2月 1 21:08 /etc
[root@kylin ~]# ls -a
ls -a
3.7. ls -R 递归显示各个目录包含的文件和目录
[root@centos /]# ls -R /data
ls -R
3.8. ls -i 显示 file inode number[root@centos /]# ls -i /data[root@centos /]# ls -li /data
ls -i
3.9. ls -n 显示 UID 和 GID
默认显示的是 user name 和 group name。
[root@kylin /]# ls -n
ls -n
3.10. ls -F 用特定字符显示不同类型的文件或目录
[root@kylin /]# ls -F
ls -F
/ 表示 directory.@ 表示 link file. 表示 Executable filenothing 表示 normal file.