#Bash 的 ls 命令
ls [OPTION]... [FILE]...
功能
列出文件信息。
类型
可执行文件(/usr/bin/ls),属于 coreutils。
参数
OPTION选项:-a,--all- 显示隐藏文件(文件名以.开头)-A,--almost-all- 显示隐藏文件,但不显示.和..--author- 显示文件的作者;和-l选项一起使用-b,--escape- 非 图形字符 显示为 C 风格的 转义字符--block-size=SIZE- 文件大小的单位;和-l选项一起使用-B,--ignore-backups不显示备份文件(文件名以~结束)-c- 按 ctime(文件状态信息最后更新的时间)排序;和-lt选项一起使用-C- 竖向排列--color[=WHEN]- 何时使用彩色,有效值:always,auto,never-d,--directory- 查看目录本身,而不是目录的内容-D,--dired- 生成为 Emacs 的 dired 模式设计的输出-f- 显示所有文件且不进行排序(按照目录顺序显示)-F,--classify[=WHEN]- 何时添加 文件类型指示器,有效值:always,auto,never--file-type- 显示文件类型指示器;同--classify但是不添加*--format=WORD- 格式,有效值:across,commas,horizontal,long,single-column,verbose,vertical--full-time- 显示完整的时间;同-l --time-style=full-iso-g- 显示文件详情;同-l但不显示所有者--group-directories-first- 分组时将目录排在文件前面-G,--no-group- 不显示分组名-h,--human-readable- 让详细信息更适合人类阅读;和-l或-s选择一起使用--si- 让详细信息更适合人类阅读;同-h但是文件大小以 1000 为单位,而不是 1024-H,--dereference-command-line- 参数是符号链接时,查看源文件--dereference-command-line-symlink-to-dir- 参数是目录的符号链接时,查看源目录--hide=PATTERN- 隐藏匹配的文件--hyperlink[=WHEN]- 何时将文件名显示为超链接(现代终端上点击可以打开文件管理器),有效值:always,auto,never--indicator-style=WORD- 添加 文件类型指示器 ,有效值:none,slash,file-type,classify-i,--inode- 显示文件的 inode 编号-I,--ignore=PATTERN- 不显示匹配的文件-k,--kibibytes- 文件大小以 1024 字节为单位-l- 显示详细信息-L,--dereference- 符号链接显示源文件的信息,而非符号链接本身的信息-m- 用逗号分隔条目-n,--numeric-uid-gid- 显示详细信息,但是所有者和所属组显示为 ID 而非名称-N,--literal- 含特殊字符的文件名不添加单引号-o- 显示详细信息,但不显示所属组信息-p,--indicator-style=slash- 添加/指示器-q,--hide-control-chars- 非 图形字符 显示为?--show-control-chars- 直接显示非 图形字符-Q,--quote-name- 用双引号包裹文件名--quoting-style=WORD- 引号风格,有效值:literal,locale,shell,shell-always,shell-escape,shell-escape-always,c,escape-r,--reverse- 逆序-R,--recursive- 递归查看子目录-s,--size- 显示文件分配的块数-S- 按文件大小排序,最大的优先--sort=WORD- 排序规则,有效值:none,size,time,version,extension,width--time=WORD- 显示或排序的时间,有效值:atime,ctime,mtime,btime--time-style=TIME_STYLE- 日期和时间的格式-t- 按时间排序-T,--tabsize=COLS- 制表符的宽度-u- 显示 atime 或按 atime 排序-U- 不排序-v- 自然排序-w,--width=COLS- 一行的宽度,0 表示无限-x- 横向排列-X- 按扩展名排序-Z,--context- 显示安全上下文--zero- 以空字符(\0)作为行的结尾,而不是换行符(\n)-1- 每行只显示一个文件--help- 显示当前帮助--version- 显示版本
FILE文件,可以省略,默认为当前工作目录
返回值
0:成功1:失败,小问题2:失败,严重问题
#文件类型指示器
| 符号 | 含义 | 示例 |
|---|---|---|
/ | 目录(directory) | src/ |
@ | 符号链接(symbolic link) | readme@ |
* | 可执行文件(executable file) | run.sh* |
= | UNIX 域套接字(socket) | mysock= |
| | 命名管道(FIFO) | myfifo| |
> | Solaris Door File | door> |
#示例
基本列出文件
ls
- 列出当前目录下的文件(不包括隐藏文件)。
- 输出按字母顺序排序。
ls -a
- 包括隐藏文件(以
.开头的文件)。
ls -1
- 每行只显示一个文件名,方便脚本处理。
彩色与类型指示
ls --color=auto
- 根据文件类型显示颜色(目录、可执行文件、链接等)。
ls -F
-
给文件添加类型符号:
/目录*可执行文件@符号链接|FIFO=socket
ls -lF --color=auto
- 长列表 + 类型符号 + 彩色显示。
多列与排序
ls -C
- 多列竖向显示(默认行为)。
ls -x
- 多列横向显示(行填充)。
ls -lt
- 按 修改时间 排序,最近修改的在前。
ls -ltu
- 按 访问时间 排序(-u 选项),最近访问的在前。
ls -ltc
- 按 状态改变时间 排序(-c 选项)。
文件名原样
ls -N
- 文件名按原样显示,不加引号、不转义。
ls --show-control-chars
- 显示文件名中的控制字符(如换行、制表符等),不替换为
?。
超链接显示(现代终端)
ls --hyperlink=auto
- 支持 OSC 8 超链接,在终端中点击文件名可直接打开。
ls --hyperlink=always
- 强制生成超链接,即使终端不支持也输出控制序列。
组合示例
ls -lF --color=auto --hyperlink=auto
- 长列表 + 类型符号 + 彩色显示 + 可点击链接。
- 最适合现代终端使用。
ls -laN --show-control-chars
- 查看所有文件(包括隐藏)
- 原样显示文件名
- 显示控制字符
ls -ltuF --color=auto
- 按访问时间排序
- 显示类型符号
- 彩色显示
ls -1U
- 每行显示一个文件名
- 不排序(按照目录存储顺序),适合脚本处理。
ls -l --quoting-style=c
- 长列表
- 文件名用 C 风格转义(
\n、\t等),便于分析特殊字符。
#推荐阅读
#手册
LS(1) User Commands LS(1) NAME ls - list directory contents SYNOPSIS ls [OPTION]... [FILE]... DESCRIPTION List information about the FILEs (the current directory by default). Sort entries alphabetically if none of -cftuvSUX nor --sort is speci‐ fied. Mandatory arguments to long options are mandatory for short options too. -a, --all do not ignore entries starting with . -A, --almost-all do not list implied . and .. --author with -l, print the author of each file -b, --escape print C-style escapes for nongraphic characters --block-size=SIZE with -l, scale sizes by SIZE when printing them; e.g., '--block-size=M'; see SIZE format below -B, --ignore-backups do not list implied entries ending with ~ -c with -lt: sort by, and show, ctime (time of last change of file status information); with -l: show ctime and sort by name; oth‐ erwise: sort by ctime, newest first -C list entries by columns --color[=WHEN] color the output WHEN; more info below -d, --directory list directories themselves, not their contents -D, --dired generate output designed for Emacs' dired mode -f list all entries in directory order -F, --classify[=WHEN] append indicator (one of */=>@|) to entries WHEN --file-type likewise, except do not append '*' --format=WORD across -x, commas -m, horizontal -x, long -l, single-column -1, verbose -l, vertical -C --full-time like -l --time-style=full-iso -g like -l, but do not list owner --group-directories-first group directories before files; can be augmented with a --sort option, but any use of --sort=none (-U) disables grouping -G, --no-group in a long listing, don't print group names -h, --human-readable with -l and -s, print sizes like 1K 234M 2G etc. --si likewise, but use powers of 1000 not 1024 -H, --dereference-command-line follow symbolic links listed on the command line --dereference-command-line-symlink-to-dir follow each command line symbolic link that points to a direc‐ tory --hide=PATTERN do not list implied entries matching shell PATTERN (overridden by -a or -A) --hyperlink[=WHEN] hyperlink file names WHEN --indicator-style=WORD append indicator with style WORD to entry names: none (default), slash (-p), file-type (--file-type), classify (-F) -i, --inode print the index number of each file -I, --ignore=PATTERN do not list implied entries matching shell PATTERN -k, --kibibytes default to 1024-byte blocks for file system usage; used only with -s and per directory totals -l use a long listing format -L, --dereference when showing file information for a symbolic link, show informa‐ tion for the file the link references rather than for the link itself -m fill width with a comma separated list of entries -n, --numeric-uid-gid like -l, but list numeric user and group IDs -N, --literal print entry names without quoting -o like -l, but do not list group information -p, --indicator-style=slash append / indicator to directories -q, --hide-control-chars print ? instead of nongraphic characters --show-control-chars show nongraphic characters as-is (the default, unless program is 'ls' and output is a terminal) -Q, --quote-name enclose entry names in double quotes --quoting-style=WORD use quoting style WORD for entry names: literal, locale, shell, shell-always, shell-escape, shell-escape-always, c, escape (overrides QUOTING_STYLE environment variable) -r, --reverse reverse order while sorting -R, --recursive list subdirectories recursively -s, --size print the allocated size of each file, in blocks -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), width --time=WORD select which timestamp used to display or sort; access time (-u): atime, access, use; metadata change time (-c): ctime, sta‐ tus; modified time (default): mtime, modification; birth time: birth, creation; with -l, WORD determines which time to show; with --sort=time, sort by WORD (newest first) --time-style=TIME_STYLE time/date format with -l; see TIME_STYLE below -t sort by time, newest first; see --time -T, --tabsize=COLS assume tab stops at each COLS instead of 8 -u with -lt: sort by, and show, access time; with -l: show access time and sort by name; otherwise: sort by access time, newest first -U do not sort; list entries in directory order -v natural sort of (version) numbers within text -w, --width=COLS set output width to COLS. 0 means no limit -x list entries by lines instead of by columns -X sort alphabetically by entry extension -Z, --context print any security context of each file --zero end each output line with NUL, not newline -1 list one file per line --help display this help and exit --version output version information and exit The SIZE argument is an integer and optional unit (example: 10K is 10*1024). Units are K,M,G,T,P,E,Z,Y,R,Q (powers of 1024) or KB,MB,... (powers of 1000). Binary prefixes can be used, too: KiB=K, MiB=M, and so on. The TIME_STYLE argument can be full-iso, long-iso, iso, locale, or +FORMAT. FORMAT is interpreted like in date(1). If FORMAT is FOR‐ MAT1<newline>FORMAT2, then FORMAT1 applies to non-recent files and FOR‐ MAT2 to recent files. TIME_STYLE prefixed with 'posix-' takes effect only outside the POSIX locale. Also the TIME_STYLE environment vari‐ able sets the default style to use. The WHEN argument defaults to 'always' and can also be 'auto' or 'never'. Using color to distinguish file types is disabled both by default and with --color=never. With --color=auto, ls emits color codes only when standard output is connected to a terminal. The LS_COLORS environment variable can change the settings. Use the dircolors(1) command to set it. 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). AUTHOR Written by Richard M. Stallman and David MacKenzie. REPORTING BUGS GNU coreutils online help: <https://www.gnu.org/software/coreutils/> Report any translation bugs to <https://translationproject.org/team/> COPYRIGHT Copyright © 2023 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>. This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. SEE ALSO dircolors(1) Full documentation <https://www.gnu.org/software/coreutils/ls> or available locally via: info '(coreutils) ls invocation' GNU coreutils 9.4 April 2024 LS(1)