Liunx中 ls -al 释义

ls 选项

[root@node2 ~]# ls --help
Usage: ls [OPTION]... [FILE]...
List information about the FILEs (the current directory by default).
Sort entries alphabetically if none of -cftuvSUX nor --sort is specified.

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      scale sizes by SIZE before printing them; e.g.,
                               '--block-size=M' prints sizes in units of
                               1,048,576 bytes; 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
                               modification of file status information);
                               with -l: show ctime and sort by name;
                               otherwise: sort by ctime, newest first
  -C                         list entries by columns
      --color[=WHEN]         colorize the output; WHEN can be 'never', 'auto',
                               or 'always' (the default); more info below
  -d, --directory            list directories themselves, not their contents       ===> 列出目录本身,而不是它们的内容
  -D, --dired                generate output designed for Emacs' dired mode
  -f                         do not sort, enable -aU, disable -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
      --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, print sizes in human readable format
                               (e.g., 1K 234M 2G)
      --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 directory
      --hide=PATTERN         do not list implied entries matching shell PATTERN
                               (overridden by -a or -A)
      --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 ===> 不列出与 shell PATTERN 匹配的隐含条目
  -k, --kibibytes            default to 1024-byte blocks for disk usage         ===> 默认为 1024 字节块用于磁盘使用
  -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
  -N, --literal              print raw entry names (don't treat e.g. control
                               characters specially)
  -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, c, escape
  -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
      --sort=WORD            sort by WORD instead of name: none (-U), size (-S),
                               time (-t), version (-v), extension (-X)
      --time=WORD            with -l, show time as WORD instead of default
                               modification time: atime or access or use (-u)
                               ctime or status (-c); also use specified time
                               as sort key if --sort=time
      --time-style=STYLE     with -l, show times using style STYLE:
                               full-iso, long-iso, iso, locale, or +FORMAT;
                               FORMAT is interpreted like in 'date'; if FORMAT
                               is FORMAT1<newline>FORMAT2, then FORMAT1 applies
                               to non-recent files and FORMAT2 to recent files;
                               if STYLE is prefixed with 'posix-', STYLE
                               takes effect only outside the POSIX locale
  -t                         sort by modification time, newest first
  -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
  -U                         do not sort; list entries in directory order
  -v                         natural sort of (version) numbers within text
  -w, --width=COLS           assume screen width instead of current value
  -x                         list entries by lines instead of by columns
  -X                         sort alphabetically by entry extension
  -1                         list one file per line

SELinux options:

  --lcontext                 Display security context.   Enable -l. Lines
                             will probably be too wide for most displays.
  -Z, --context              Display security context so it fits on most
                             displays.  Displays only mode, user, group,
                             security context and file name.
  --scontext                 Display only security context and file name.
      --help     display this help and exit
      --version  output version information and exit

SIZE is an integer and optional unit (example: 10M is 10*1024*1024).  Units
are K, M, G, T, P, E, Z, Y (powers of 1024) or KB, MB, ... (powers of 1000).

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.

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: <http://www.gnu.org/software/coreutils/>
For complete documentation, run: info coreutils 'ls invocation'

示例

这里主要讲解回显中第三列的数值意思,即 3 18 1 1 1...这些数值的意思

[root@node2 ~]# ls -al
total 32                                                ===> 第一行,下面会有单独讲的部分
dr-xr-x---.  3 root root  163 Apr 24 15:59 .            ===> 第二行
dr-xr-xr-x. 18 root root  236 Apr 13 14:21 ..           ===> 第三行
-rw-------.  1 root root 1325 Apr 11 10:24 anaconda-ks.cfg
-rw-------.  1 root root 2248 Apr 24 13:47 .bash_history
-rw-r--r--.  1 root root   18 Dec 29  2013 .bash_logout
-rw-r--r--.  1 root root  176 Dec 29  2013 .bash_profile
-rw-r--r--.  1 root root  176 Dec 29  2013 .bashrc
-rw-r--r--.  1 root root  100 Dec 29  2013 .cshrc
drwx------.  2 root root   29 Apr 13 10:54 .ssh           ===> 示例说明目录
-rw-r--r--.  1 root root  129 Dec 29  2013 .tcshrc
-rw-------.  1 root root 3004 Apr 13 14:22 .viminfo

注: 
对于目录文件,回显中第三列表示它的第一级子目录的个数。此处看到目录的第三列的值要减2才等于该目录下的子目录的实际个数。
比如这里的.ssh目录下其实是没有子目录,所以应该是0,但它这里却显示2是因为要加上.目录和..目录。
在linux下,.目录表示当前目录,..目录表示上一级目录

下面的回显也可以解释上图中第二行第三列的 . 目录下的3和第二行 .. 目录下的18的意思
因为当前目录下有一个.ssh目录,所以加上.目录和..目录这2个目录就等于3,所以回显中第二行第三列会显示3。
而第三行第三列则对于上一级目录共有16个目录,加上上一级目录的 . 目录和 .. 目录这2个目录,所以这里的第二行显示的是18(如下所示)

对于其他文件,表示指向它的链接文件的个数             ===> 注意要理解这句话!!!


[root@node2 ~]# ls -al / | grep ^d|wc -l
18
[root@node2 ~]# ls -al /
total 16
dr-xr-xr-x.  18 root root  236 Apr 13 14:21 .
dr-xr-xr-x.  18 root root  236 Apr 13 14:21 ..
lrwxrwxrwx.   1 root root    7 Apr 11 10:21 bin -> usr/bin
dr-xr-xr-x.   5 root root 4096 Apr 14 08:22 boot
drwxr-xr-x.  21 root root 3420 Apr 24 13:55 dev
drwxr-xr-x.  76 root root 8192 Apr 24 13:55 etc
drwxr-xr-x.   2 root root    6 Apr 11  2018 home
lrwxrwxrwx.   1 root root    7 Apr 11 10:21 lib -> usr/lib
lrwxrwxrwx.   1 root root    9 Apr 11 10:21 lib64 -> usr/lib64
drwxr-xr-x.   2 root root    6 Apr 11  2018 media
drwxr-xr-x.   2 root root    6 Apr 11  2018 mnt
drwxr-xr-x.   3 root root   19 Apr 13 15:44 opop
drwxr-xr-x.   2 root root    6 Apr 11  2018 opt
dr-xr-xr-x. 156 root root    0 Apr 24 13:55 proc
dr-xr-x---.   3 root root  163 Apr 24 15:59 root
drwxr-xr-x.  26 root root  840 Apr 24 15:35 run
lrwxrwxrwx.   1 root root    8 Apr 11 10:21 sbin -> usr/sbin
drwxr-xr-x.   2 root root    6 Apr 11  2018 srv
dr-xr-xr-x.  13 root root    0 Apr 24 13:55 sys
drwxrwxrwt.  10 root root  195 Apr 24 15:59 tmp
drwxr-xr-x.  13 root root  155 Apr 11 10:21 usr
drwxr-xr-x.  19 root root  267 Apr 11 10:25 var

total 32由来

把上面的图拿下来,方便查看和讲解
[root@node2 ~]# ls -als
total 32
0 dr-xr-x---.  3 root root  163 Apr 24 17:59 .
0 dr-xr-xr-x. 18 root root  236 Apr 13 14:21 ..
4 -rw-------.  1 root root 1325 Apr 11 10:24 anaconda-ks.cfg
4 -rw-------.  1 root root 2248 Apr 24 13:47 .bash_history
4 -rw-r--r--.  1 root root   18 Dec 29  2013 .bash_logout
4 -rw-r--r--.  1 root root  176 Dec 29  2013 .bash_profile
4 -rw-r--r--.  1 root root  176 Dec 29  2013 .bashrc
4 -rw-r--r--.  1 root root  100 Dec 29  2013 .cshrc
0 drwx------.  2 root root   29 Apr 13 10:54 .ssh
4 -rw-r--r--.  1 root root  129 Dec 29  2013 .tcshrc
4 -rw-------.  1 root root 3416 Apr 24 16:42 .viminfo

注: 要明白该项是什么意思,首先要了解一个数据块(Block)的概念,linux的数据存储是以block(块)为单位的。
-s以块大小为单位列出所有文件的大小
文件系统在格式化时就规范好了block的大小,在ext2/ext3的环境下这个block不变
每一个 block 仅能容纳一个文件/目录,所以如果block大小为4K ,而单一个文件/目录只有1K则会有3K的空间被浪费。而那个total会记录的则是4K的容量

[root@node2 ~]# ll -h
total 4.0K
-rw-------. 1 root root 1.3K Apr 11 10:24 anaconda-ks.cfg

注: 使用上述命令查询即只查询的是一个块,所以是4K


计算一共浪费了多少空间的计算方法:
4*8=32K         # 上述使用-s选项出来的是每个文件所占的块大小,乘8是总共占的block大小

1325占用1个block,所以总共耗去4Kbytes
2248占用1个block,所以总共耗去4Kbytes
18占用1个block,所以总共耗去4Kbytes
176占用1个block,所以总共耗去4Kbytes         ===> 低于4096都是占一个block
176占用1个block,所以总共耗去4Kbytes
100占用1个block,所以总共耗去4Kbytes
129占用1个block,所以总共耗去4Kbytes
3416占用1个block,所以总共耗去4Kbytes

(1325+2248+18+176+176+100+29+129+3416)/1024=7K
32K-7K=25K
一共浪费了25K左右的空间

其他

在大多数的linux shell窗口中,还能用颜色来区分不同文件的属性:(指回显中最后一列,文件名/目录名)
灰白色表示普通文件;
亮绿色表示可执行文件;
亮红色表示压缩文件;
灰蓝色表示目录;
亮蓝色表示链接文件;
亮黄色表示设备文件;


~]# ll -t 是降序, ll -t | tac 是升序


欢迎加入QQ群一起讨论Linux、开源等技术

posted @ 2022-04-24 18:14  e-8bit  阅读(217)  评论(0编辑  收藏  举报