• ls
    • 补充说明
      • 语法
      • 选项
      • 参数
      • 实例

    ls

    显示目录内容列表

    补充说明

    ls命令 用来显示目标列表,在Linux中是使用率较高的命令。ls命令的输出信息可以进行彩色加亮显示,以分区不同类型的文件。

    语法

    1. ls(选项)(参数)

    选项

    1. -a, --all 不隐藏任何以. 开始的项目
    2. -A, --almost-all 列出除. 及.. 以外的任何项目
    3. --author 与-l 同时使用时列出每个文件的作者
    4. -b, --escape 以八进制溢出序列表示不可打印的字符
    5. --block-size=SIZE scale sizes by SIZE before printing them; e.g.,
    6. '--block-size=M' prints sizes in units of
    7. 1,048,576 bytes; see SIZE format below
    8. -B, --ignore-backups do not list implied entries ending with ~
    9. -c with -lt: sort by, and show, ctime (time of last
    10. modification of file status information);
    11. with -l: show ctime and sort by name;
    12. otherwise: sort by ctime, newest first
    13. -C list entries by columns
    14. --color[=WHEN] colorize the output; WHEN can be 'never', 'auto',
    15. or 'always' (the default); more info below
    16. -d, --directory list directories themselves, not their contents
    17. -D, --dired generate output designed for Emacs' dired mode
    18. -f do not sort, enable -aU, disable -ls --color
    19. -F, --classify append indicator (one of */=>@|) to entries
    20. --file-type likewise, except do not append '*'
    21. --format=WORD across -x, commas -m, horizontal -x, long -l,
    22. single-column -1, verbose -l, vertical -C
    23. --full-time like -l --time-style=full-iso
    24. -g 类似-l,但不列出所有者
    25. --group-directories-first
    26. group directories before files;
    27. can be augmented with a --sort option, but any
    28. use of --sort=none (-U) disables grouping
    29. -G, --no-group 以一个长列表的形式,不输出组名
    30. -h, --human-readable 与-l 一起,以易于阅读的格式输出文件大小
    31. (例如 1K 234M 2G)
    32. --si 同上面类似,但是使用1000 为基底而非1024
    33. -H, --dereference-command-line
    34. follow symbolic links listed on the command line
    35. --dereference-command-line-symlink-to-dir
    36. follow each command line symbolic link
    37. that points to a directory
    38. --hide=PATTERN do not list implied entries matching shell PATTERN
    39. (overridden by -a or -A)
    40. --indicator-style=WORD append indicator with style WORD to entry names:
    41. none (default), slash (-p),
    42. file-type (--file-type), classify (-F)
    43. -i, --inode print the index number of each file
    44. -I, --ignore=PATTERN do not list implied entries matching shell PATTERN
    45. -k, --kibibytes default to 1024-byte blocks for disk usage
    46. -l 使用较长格式列出信息
    47. -L, --dereference 当显示符号链接的文件信息时,显示符号链接所指示
    48. 的对象而并非符号链接本身的信息
    49. -m 所有项目以逗号分隔,并填满整行行宽
    50. -n, --numeric-uid-gid 类似 -l,但列出UID 及GID 号
    51. -N, --literal 输出未经处理的项目名称 (如不特别处理控制字符)
    52. -o 类似 -l,但不列出有关组的信息
    53. -p, --indicator-style=slash 对目录加上表示符号"/"
    54. -q, --hide-control-chars print ? instead of nongraphic characters
    55. --show-control-chars show nongraphic characters as-is (the default,
    56. unless program is 'ls' and output is a terminal)
    57. -Q, --quote-name enclose entry names in double quotes
    58. --quoting-style=WORD use quoting style WORD for entry names:
    59. literal, locale, shell, shell-always, c, escape
    60. -r, --reverse 逆序排列
    61. -R, --recursive 递归显示子目录
    62. -s, --size 以块数形式显示每个文件分配的尺寸
    63. -S sort by file size
    64. --sort=WORD sort by WORD instead of name: none (-U), size (-S),
    65. time (-t), version (-v), extension (-X)
    66. --time=WORD with -l, show time as WORD instead of default
    67. modification time: atime or access or use (-u)
    68. ctime or status (-c); also use specified time
    69. as sort key if --sort=time
    70. --time-style=STYLE with -l, show times using style STYLE:
    71. full-iso, long-iso, iso, locale, or +FORMAT;
    72. FORMAT is interpreted like in 'date'; if FORMAT
    73. is FORMAT1<newline>FORMAT2, then FORMAT1 applies
    74. to non-recent files and FORMAT2 to recent files;
    75. if STYLE is prefixed with 'posix-', STYLE
    76. takes effect only outside the POSIX locale
    77. -t sort by modification time, newest first
    78. -T, --tabsize=COLS assume tab stops at each COLS instead of 8
    79. -u with -lt: sort by, and show, access time;
    80. with -l: show access time and sort by name;
    81. otherwise: sort by access time
    82. -U do not sort; list entries in directory order
    83. -v natural sort of (version) numbers within text
    84. -w, --width=COLS assume screen width instead of current value
    85. -x list entries by lines instead of by columns
    86. -X sort alphabetically by entry extension
    87. -1 list one file per line
    88. SELinux options:
    89. --lcontext Display security context. Enable -l. Lines
    90. will probably be too wide for most displays.
    91. -Z, --context Display security context so it fits on most
    92. displays. Displays only mode, user, group,
    93. security context and file name.
    94. --scontext Display only security context and file name.
    95. --help 显示此帮助信息并退出
    96. --version 显示版本信息并退出

    参数

    目录:指定要显示列表的目录,也可以是具体的文件。

    实例

    1. $ ls # 仅列出当前目录可见文件
    2. $ ls -l # 列出当前目录可见文件详细信息
    3. $ ls -hl # 列出详细信息并以可读大小显示文件大小
    4. $ ls -al # 列出所有文件(包括隐藏)的详细信息

    显示当前目录下包括影藏文件在内的所有文件列表

    1. [root@localhost ~]# ls -a
    2. . anaconda-ks.cfg .bash_logout .bashrc install.log .mysql_history satools .tcshrc .vimrc
    3. .. .bash_history .bash_profile .cshrc install.log.syslog .rnd .ssh .viminfo

    输出长格式列表

    1. [root@localhost ~]# ls -1
    2. anaconda-ks.cfg
    3. install.log
    4. install.log.syslog
    5. satools

    显示文件的inode信息

    索引节点(index inode简称为“inode”)是Linux中一个特殊的概念,具有相同的索引节点号的两个文本本质上是同一个文件(除文件名不同外)。

    1. [root@localhost ~]# ls -i -l anaconda-ks.cfg install.log
    2. 2345481 -rw------- 1 root root 859 Jun 11 22:49 anaconda-ks.cfg
    3. 2345474 -rw-r--r-- 1 root root 13837 Jun 11 22:49 install.log

    水平输出文件列表

    1. [root@localhost /]# ls -m
    2. bin, boot, data, dev, etc, home, lib, lost+found, media, misc, mnt, opt, proc, root, sbin, selinux, srv, sys, tmp, usr, var

    修改最后一次编辑的文件

    最近修改的文件显示在最上面。

    1. [root@localhost /]# ls -t
    2. tmp root etc dev lib boot sys proc data home bin sbin usr var lost+found media mnt opt selinux srv misc

    显示递归文件

    1. [root@localhost ~]# ls -R
    2. .:
    3. anaconda-ks.cfg install.log install.log.syslog satools
    4. ./satools:
    5. black.txt freemem.sh iptables.sh lnmp.sh mysql php502_check.sh ssh_safe.sh

    打印文件的UID和GID

    1. [root@localhost /]# ls -n
    2. total 254
    3. drwxr-xr-x 2 0 0 4096 Jun 12 04:03 bin
    4. drwxr-xr-x 4 0 0 1024 Jun 15 14:45 boot
    5. drwxr-xr-x 6 0 0 4096 Jun 12 10:26 data
    6. drwxr-xr-x 10 0 0 3520 Sep 26 15:38 dev
    7. drwxr-xr-x 75 0 0 4096 Oct 16 04:02 etc
    8. drwxr-xr-x 4 0 0 4096 Jun 12 10:26 home
    9. drwxr-xr-x 14 0 0 12288 Jun 16 04:02 lib
    10. drwx------ 2 0 0 16384 Jun 11 22:46 lost+found
    11. drwxr-xr-x 2 0 0 4096 May 11 2011 media
    12. drwxr-xr-x 2 0 0 4096 Nov 8 2010 misc
    13. drwxr-xr-x 2 0 0 4096 May 11 2011 mnt
    14. drwxr-xr-x 2 0 0 4096 May 11 2011 opt
    15. dr-xr-xr-x 232 0 0 0 Jun 15 11:04 proc
    16. drwxr-x--- 4 0 0 4096 Oct 15 14:43 root
    17. drwxr-xr-x 2 0 0 12288 Jun 12 04:03 sbin
    18. drwxr-xr-x 2 0 0 4096 May 11 2011 selinux
    19. drwxr-xr-x 2 0 0 4096 May 11 2011 srv
    20. drwxr-xr-x 11 0 0 0 Jun 15 11:04 sys
    21. drwxrwxrwt 3 0 0 98304 Oct 16 08:45 tmp
    22. drwxr-xr-x 13 0 0 4096 Jun 11 23:38 usr
    23. drwxr-xr-x 19 0 0 4096 Jun 11 23:38 var

    列出文件和文件夹的详细信息

    1. [root@localhost /]# ls -l
    2. total 254
    3. drwxr-xr-x 2 root root 4096 Jun 12 04:03 bin
    4. drwxr-xr-x 4 root root 1024 Jun 15 14:45 boot
    5. drwxr-xr-x 6 root root 4096 Jun 12 10:26 data
    6. drwxr-xr-x 10 root root 3520 Sep 26 15:38 dev
    7. drwxr-xr-x 75 root root 4096 Oct 16 04:02 etc
    8. drwxr-xr-x 4 root root 4096 Jun 12 10:26 home
    9. drwxr-xr-x 14 root root 12288 Jun 16 04:02 lib
    10. drwx------ 2 root root 16384 Jun 11 22:46 lost+found
    11. drwxr-xr-x 2 root root 4096 May 11 2011 media
    12. drwxr-xr-x 2 root root 4096 Nov 8 2010 misc
    13. drwxr-xr-x 2 root root 4096 May 11 2011 mnt
    14. drwxr-xr-x 2 root root 4096 May 11 2011 opt
    15. dr-xr-xr-x 232 root root 0 Jun 15 11:04 proc
    16. drwxr-x--- 4 root root 4096 Oct 15 14:43 root
    17. drwxr-xr-x 2 root root 12288 Jun 12 04:03 sbin
    18. drwxr-xr-x 2 root root 4096 May 11 2011 selinux
    19. drwxr-xr-x 2 root root 4096 May 11 2011 srv
    20. drwxr-xr-x 11 root root 0 Jun 15 11:04 sys
    21. drwxrwxrwt 3 root root 98304 Oct 16 08:48 tmp
    22. drwxr-xr-x 13 root root 4096 Jun 11 23:38 usr
    23. drwxr-xr-x 19 root root 4096 Jun 11 23:38 var

    列出可读文件和文件夹详细信息

    1. [root@localhost /]# ls -lh
    2. total 254K
    3. drwxr-xr-x 2 root root 4.0K Jun 12 04:03 bin
    4. drwxr-xr-x 4 root root 1.0K Jun 15 14:45 boot
    5. drwxr-xr-x 6 root root 4.0K Jun 12 10:26 data
    6. drwxr-xr-x 10 root root 3.5K Sep 26 15:38 dev
    7. drwxr-xr-x 75 root root 4.0K Oct 16 04:02 etc
    8. drwxr-xr-x 4 root root 4.0K Jun 12 10:26 home
    9. drwxr-xr-x 14 root root 12K Jun 16 04:02 lib
    10. drwx------ 2 root root 16K Jun 11 22:46 lost+found
    11. drwxr-xr-x 2 root root 4.0K May 11 2011 media
    12. drwxr-xr-x 2 root root 4.0K Nov 8 2010 misc
    13. drwxr-xr-x 2 root root 4.0K May 11 2011 mnt
    14. drwxr-xr-x 2 root root 4.0K May 11 2011 opt
    15. dr-xr-xr-x 235 root root 0 Jun 15 11:04 proc
    16. drwxr-x--- 4 root root 4.0K Oct 15 14:43 root
    17. drwxr-xr-x 2 root root 12K Jun 12 04:03 sbin
    18. drwxr-xr-x 2 root root 4.0K May 11 2011 selinux
    19. drwxr-xr-x 2 root root 4.0K May 11 2011 srv
    20. drwxr-xr-x 11 root root 0 Jun 15 11:04 sys
    21. drwxrwxrwt 3 root root 96K Oct 16 08:49 tmp
    22. drwxr-xr-x 13 root root 4.0K Jun 11 23:38 usr
    23. drwxr-xr-x 19 root root 4.0K Jun 11 23:38 var

    显示文件夹信息

    1. [root@localhost /]# ls -ld /etc/
    2. drwxr-xr-x 75 root root 4096 Oct 16 04:02 /etc/

    按时间列出文件和文件夹详细信息

    1. [root@localhost /]# ls -lt
    2. total 254
    3. drwxrwxrwt 3 root root 98304 Oct 16 08:53 tmp
    4. drwxr-xr-x 75 root root 4096 Oct 16 04:02 etc
    5. drwxr-x--- 4 root root 4096 Oct 15 14:43 root
    6. drwxr-xr-x 10 root root 3520 Sep 26 15:38 dev
    7. drwxr-xr-x 14 root root 12288 Jun 16 04:02 lib
    8. drwxr-xr-x 4 root root 1024 Jun 15 14:45 boot
    9. drwxr-xr-x 11 root root 0 Jun 15 11:04 sys
    10. dr-xr-xr-x 232 root root 0 Jun 15 11:04 proc
    11. drwxr-xr-x 6 root root 4096 Jun 12 10:26 data
    12. drwxr-xr-x 4 root root 4096 Jun 12 10:26 home
    13. drwxr-xr-x 2 root root 4096 Jun 12 04:03 bin
    14. drwxr-xr-x 2 root root 12288 Jun 12 04:03 sbin
    15. drwxr-xr-x 13 root root 4096 Jun 11 23:38 usr
    16. drwxr-xr-x 19 root root 4096 Jun 11 23:38 var
    17. drwx------ 2 root root 16384 Jun 11 22:46 lost+found
    18. drwxr-xr-x 2 root root 4096 May 11 2011 media
    19. drwxr-xr-x 2 root root 4096 May 11 2011 mnt
    20. drwxr-xr-x 2 root root 4096 May 11 2011 opt
    21. drwxr-xr-x 2 root root 4096 May 11 2011 selinux
    22. drwxr-xr-x 2 root root 4096 May 11 2011 srv
    23. drwxr-xr-x 2 root root 4096 Nov 8 2010 misc

    按修改时间列出文件和文件夹详细信息

    1. [root@localhost /]# ls -ltr
    2. total 254
    3. drwxr-xr-x 2 root root 4096 Nov 8 2010 misc
    4. drwxr-xr-x 2 root root 4096 May 11 2011 srv
    5. drwxr-xr-x 2 root root 4096 May 11 2011 selinux
    6. drwxr-xr-x 2 root root 4096 May 11 2011 opt
    7. drwxr-xr-x 2 root root 4096 May 11 2011 mnt
    8. drwxr-xr-x 2 root root 4096 May 11 2011 media
    9. drwx------ 2 root root 16384 Jun 11 22:46 lost+found
    10. drwxr-xr-x 19 root root 4096 Jun 11 23:38 var
    11. drwxr-xr-x 13 root root 4096 Jun 11 23:38 usr
    12. drwxr-xr-x 2 root root 12288 Jun 12 04:03 sbin
    13. drwxr-xr-x 2 root root 4096 Jun 12 04:03 bin
    14. drwxr-xr-x 4 root root 4096 Jun 12 10:26 home
    15. drwxr-xr-x 6 root root 4096 Jun 12 10:26 data
    16. dr-xr-xr-x 232 root root 0 Jun 15 11:04 proc
    17. drwxr-xr-x 11 root root 0 Jun 15 11:04 sys
    18. drwxr-xr-x 4 root root 1024 Jun 15 14:45 boot
    19. drwxr-xr-x 14 root root 12288 Jun 16 04:02 lib
    20. drwxr-xr-x 10 root root 3520 Sep 26 15:38 dev
    21. drwxr-x--- 4 root root 4096 Oct 15 14:43 root
    22. drwxr-xr-x 75 root root 4096 Oct 16 04:02 etc
    23. drwxrwxrwt 3 root root 98304 Oct 16 08:54 tmp

    按照特殊字符对文件进行分类

    1. [root@localhost nginx-1.2.1]# ls -F
    2. auto/ CHANGES CHANGES.ru conf/ configure* contrib/ html/ LICENSE Makefile man/ objs/ README src/

    列出文件并标记颜色分类

    1. [root@localhost nginx-1.2.1]# ls --color=auto
    2. auto CHANGES CHANGES.ru conf configure contrib html LICENSE Makefile man objs README src