Linux 知:ls

前言

ls - list directory contents 列出目录内容

官方手册 | 在线中文手册 | 菜鸟教程

帮助

在这里插入图片描述
详情如下:

Usage: ls [OPTION]... [FILE]...
List information about the FILEs (the current directory by default).
列出有关FILEs的信息(默认为当前目录)。
Sort entries alphabetically if none of -cftuvSUX nor --sort is specified.
如果未指定-cftuvSUX或--sort,则按字母顺序对条目进行排序。

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
      						 加-l,可以列出每个文件的作者
  -b, --escape               print C-style escapes for nongraphic characters
  							 打印非图形字符的C样式转义符
      --block-size=SIZE      with -l, scale sizes by SIZE when printing them;
                               e.g., '--block-size=M'; see SIZE format below
                             加-l,在打印时,按SIZE缩放大小
  -B, --ignore-backups       do not list implied entries ending with ~
  						     不列出以〜结尾的隐含条目(备份条目)
  -c                         with -lt: sort by, and show, ctime (time of last
                               modification of file status information);
                             with -l: show ctime and sort by name;
                             otherwise: sort by ctime, newest first
                             加-lt:按ctime(文件状态信息的最后修改时间)排序和显示;
							 加-l:显示ctime并按名称排序;
							 否则:按ctime排序,最新的优先
  -C                         list entries by columns 按列列出条目
      --color[=WHEN]         colorize the output; WHEN can be 'always' (default
                               if omitted), 'auto', or 'never'; more info below
                             着色输出; WHEN可以是“always”(如果省略则默认为always),
                             “auto”或“never”; 更多信息在下面
  -d, --directory            list directories themselves, not their contents
  							 列出目录本身,而不是目录内容
  -D, --dired                generate output designed for Emacs' dired mode
  							 生成为Emacs的转向模式设计的输出
  -f                         do not sort, enable -aU, disable -ls --color
  							 不排序,启用-aU,禁用-ls --color
  -F, --classify             append indicator (one of */=>@|) to entries
  							 将指示符(* / => @ |之一)附加到条目
      --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
  							 像-l,但不列出拥有者
      --group-directories-first
                             group directories before files;
                               can be augmented with a --sort option, but any
                               use of --sort=none (-U) disables grouping
                             在文件之前对目录进行分组; 可以使用--sort选项进行扩充,
                             但是对--sort = none (-U) 的任何使用都会禁用分组
  -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.
  							 加-l和-s,打印大小如1K 234M 2G等。
      --si                   likewise, but use powers of 1000 not 1024
      						 同上,但使用1000而不是1024作为幂(底数为2)
  -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 directory
                             跟随每个指向目录的命令行符号链接
      --hide=PATTERN         do not list implied entries matching shell PATTERN
                               (overridden by -a or -A)
                             不列出与shell PATTERN匹配的隐式条目(会被-a或-A覆盖)
      --hyperlink[=WHEN]     hyperlink file names; WHEN can be 'always'
                               (default if omitted), 'auto', or 'never'
                             超链接文件名; WHEN可以是“always”(如果省略则默认为always),
                             “auto”或“never”
      --indicator-style=WORD  append indicator with style WORD to entry names:
                               none (default), slash (-p),
                               file-type (--file-type), classify (-F)
                             将带有WORD样式的指示符追加到条目名称:
  -i, --inode                print the index number of each file
  							 打印每个文件的索引号
  -I, --ignore=PATTERN       do not list implied entries matching shell PATTERN
  						     不列出与shell PATTERN匹配的隐式条目
  -k, --kibibytes            default to 1024-byte blocks for disk usage;
                               used only with -s and per directory totals
                             磁盘使用情况默认为1024字节块;仅与-s一起使用,并且每个目录总计
  -l                         use a long listing format
  							 使用长列表格式
  -L, --dereference          when showing file information for a symbolic
                               link, show information 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
  							 类似于-l,但列出数字化用户名和组ID
  -N, --literal              print entry names without quoting
  							 打印条目名称,不带引号
  -o                         like -l, but do not list group information
  							 类似于-l,但不列出组信息
  -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)
                             按原样显示非图形字符(默认值,除非程序为'ls'且输出为终端)
  -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)
                             为条目名称使用引号WORD:literal,local,shell,
                             shell-always,shell-escape,shell-escape-always,c,escape
                             (覆盖QUOTING_STYLE环境变量)
  -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)
      --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  time/date format with -l; see TIME_STYLE below
      					     使用-l时的时间/日期格式; 请参阅下面的TIME_STYLE 
  -t                         sort by time, newest first; see --time
  							 按时间排序,最新的优先;见 --time
  -T, --tabsize=COLS         assume tab stops at each COLS instead of 8
  							 指定制表符(Tab)的宽度,而非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
                             加-lt:按访问时间排序并显示;
                             加-l:显示访问时间并按名称排序;
                             否则:按访问时间排序,最新的优先
  -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
  							 将输出宽度设置为COLS。 0表示没有限制
  -x                         list entries by lines instead of by columns
  							 按行而不是按列列出条目
  -X                         sort alphabetically by entry extension
  							 按条目扩展名的字母顺序排序
  -Z, --context              print any security context of each file
  							 打印每个文件的任何安全上下文
  -1                         list one file per line.  Avoid '\n' with -q or -b
  							 每行列出一个文件。 使用-q或-b避免使用'\n'
      --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 (powers of 1024) or KB,MB,... (powers of 1000).
Binary prefixes can be used, too: KiB=K, MiB=M, and so on.
SIZE参数是整数和可选单位(例如:10K是10 * 1024)。
单位为K,M,G,T,P,E,Z,Y(1024的幂)或KB,MB,...(1000的幂)。
也可以使用二进制前缀:KiB = K,MiB = M,依此类推。

The TIME_STYLE argument can be full-iso, long-iso, iso, locale, or +FORMAT.
FORMAT is interpreted like in date(1).  If FORMAT is FORMAT1<newline>FORMAT2,
then FORMAT1 applies to non-recent files and FORMAT2 to recent files.
TIME_STYLE prefixed with 'posix-' takes effect only outside the POSIX locale.
Also the TIME_STYLE environment variable sets the default style to use.
TIME_STYLE参数可以是full-iso,long-iso,iso,locale或+ FORMAT。
FORMAT的解释方式与date(1)中的一样。 如果FORMAT是FORMAT1<newline>FORMAT2,
那么FORMAT1适用于非最新文件,FORMAT2适用于最近的文件。
带有“posix-”前缀的TIME_STYLE仅在POSIX语言环境外生效。
TIME_STYLE环境变量还设置要使用的默认样式。 

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 command to set it.
默认情况下和--color=never都禁用使用颜色来区分文件类型。 
使用--color=auto,仅当标准输出连接到终端时,ls才会发出颜色代码。 
LS_COLORS环境变量可以更改设置。 使用dircolors命令进行设置。

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).
 	如果遇到严重麻烦(例如,无法访问命令行参数)。

GNU coreutils online help: <https://www.gnu.org/software/coreutils/>
Full documentation <https://www.gnu.org/software/coreutils/ls>
or available locally via: info '(coreutils) ls invocation'

详解

–format=WORD 格式化显示

across -x, commas -m, horizontal -x, long -l, single-column -1, verbose -l, vertical -C

WORD 可以取上面每一段的单词,比如:across,commas,horizontal,……
-x,-m,……等表示简写,比如:-x 等价于 --format=across

单列显示

$ ls
1  2  a  b
$ ls --format=single-column
1
2
a
b
$ ls -1
1
2
a
b

以逗号分隔显示

$ ls -m
1, 2, a, b

拓展

获取当前目录下的目录

假设当前目录下有两个文件1和2,以及两个目录a和b,如下所示:

$ ls
1  2  a  b

我们有时只想批量操作目录,下面给出几种获取目录的方法。

ls

通过ls列出当前目录下所有文件,然后遍历所有文件,通过-d判断是否是目录,如下所示:

ls | for f in $(xargs); do [ -d "$f" ] && echo $f; done
a
b

ls -F

ls加-F选项列出当前目录下所有文件,但会将指示符(* / => @ |之一)附加到每个条码后面,附加/的条目表示为目录,然后通过grep和sed来提取目录,如下所示:

$ ls -F | grep '/$' | sed 's|/$||g'
a
b
  • grep ‘/$’:检索出后缀是/的条目
  • sed ‘s|/$||g’:删除最后的/

ls -l

ls加-l选项列出当前目录下所有文件的详情,第一列是d表示该条目是目录,然后通过grep和awk来提取目录,如下所示:

$ ls -l | grep '^d' | awk '{print $NF}'
a
b
  • grep ‘^d’:检索出行首是d的条目
  • awk ‘{print $NF}’:打印最后一列字段

后语

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值