ls 是 Linux 系统中最常用的命令之一,用于列出当前目录下的文件和目录。

概述

ls 命令是一个经常用到的文件管理器,用于显示当前目录中文件和文件夹的列表。 它支持多种参数,可以按照文件的类型,大小,权限,时间戳等属性显示文件信息。

语法

  1. ls [选项] [文件]

选项

以下是 ls 命令的常用选项:

使用方法:
ls [options] [file|dir]

  • -a:显示所有文件,包括以.开头的隐藏文件;
  • -l:以长格式显示文件详细信息;
  • -h:显示文件大小的可读格式;
  • -R:递归显示指定目录的内容;
  • -r:以相反的顺序,将文件列出;
  • -t:按时间顺序列出文件;
  • -S:按文件大小排序;
  • -i:显示文件的 inode 编号;
  • -F:在每个文件名后添加文件类型的标识符。

    实例

    以适当方式显示文件大小:

    1. ls -l
    1. ls -l --block-size=m
    1. ls -l --block-size=G

按照修改时间排序显示

  1. -l use a long listing format 以长列表方式显示(详细信息方式)
  2. -t sort by modification time 按修改时间排序(最新的在最前面)
  3. -r reverse order while sorting (反序)
  1. # 按照时间降序
  2. ls -lt
  3. # 按照时间升序
  4. ls -ltr
  1. root@zabbix-unify:/# # 按照时间降序
  2. root@zabbix-unify:/# ls -lt
  3. total 0
  4. drwxrwxrwt 1 root root 68 Nov 30 17:47 tmp
  5. drwxr-xr-x 1 root root 43 Nov 26 13:39 run
  6. drwxr-xr-x 16 root root 4020 Nov 26 13:39 dev
  7. drwxr-xr-x 1 root root 77 Nov 26 13:39 apps
  8. dr-xr-xr-x 1175 root root 0 Nov 26 13:39 proc
  9. drwxr-xr-x 1 root root 20 Nov 26 13:39 etc
  10. dr-xr-xr-x 13 root root 0 Nov 25 19:53 sys
  11. dr-xr-xr-x 1175 root root 0 Nov 25 19:53 host
  12. drwx------ 2 root root 37 Jun 9 15:31 root
  13. drwxr-xr-x 1 root root 45 Jun 9 15:31 var
  14. drwxr-xr-x 2 root root 6 Jun 9 15:27 media
  15. drwxr-xr-x 2 root root 6 Jun 9 15:27 mnt
  16. drwxr-xr-x 2 root root 6 Jun 9 15:27 opt
  17. drwxr-xr-x 2 root root 6 Jun 9 15:27 srv
  18. drwxr-xr-x 1 root root 19 Jun 9 15:27 usr
  19. lrwxrwxrwx 1 root root 7 Jun 9 15:27 bin -> usr/bin
  20. lrwxrwxrwx 1 root root 7 Jun 9 15:27 lib -> usr/lib
  21. lrwxrwxrwx 1 root root 9 Jun 9 15:27 lib32 -> usr/lib32
  22. lrwxrwxrwx 1 root root 9 Jun 9 15:27 lib64 -> usr/lib64
  23. lrwxrwxrwx 1 root root 10 Jun 9 15:27 libx32 -> usr/libx32
  24. lrwxrwxrwx 1 root root 8 Jun 9 15:27 sbin -> usr/sbin
  25. drwxr-xr-x 2 root root 6 Apr 15 2020 boot
  26. drwxr-xr-x 2 root root 6 Apr 15 2020 home
  27. root@zabbix-unify:/#
  28. root@zabbix-unify:/# # 按照时间升序
  29. root@zabbix-unify:/# ls -ltr
  30. total 0
  31. drwxr-xr-x 2 root root 6 Apr 15 2020 home
  32. drwxr-xr-x 2 root root 6 Apr 15 2020 boot
  33. lrwxrwxrwx 1 root root 8 Jun 9 15:27 sbin -> usr/sbin
  34. lrwxrwxrwx 1 root root 10 Jun 9 15:27 libx32 -> usr/libx32
  35. lrwxrwxrwx 1 root root 9 Jun 9 15:27 lib64 -> usr/lib64
  36. lrwxrwxrwx 1 root root 9 Jun 9 15:27 lib32 -> usr/lib32
  37. lrwxrwxrwx 1 root root 7 Jun 9 15:27 lib -> usr/lib
  38. lrwxrwxrwx 1 root root 7 Jun 9 15:27 bin -> usr/bin
  39. drwxr-xr-x 1 root root 19 Jun 9 15:27 usr
  40. drwxr-xr-x 2 root root 6 Jun 9 15:27 srv
  41. drwxr-xr-x 2 root root 6 Jun 9 15:27 opt
  42. drwxr-xr-x 2 root root 6 Jun 9 15:27 mnt
  43. drwxr-xr-x 2 root root 6 Jun 9 15:27 media
  44. drwxr-xr-x 1 root root 45 Jun 9 15:31 var
  45. drwx------ 2 root root 37 Jun 9 15:31 root
  46. dr-xr-xr-x 1172 root root 0 Nov 25 19:53 host
  47. dr-xr-xr-x 13 root root 0 Nov 25 19:53 sys
  48. drwxr-xr-x 1 root root 20 Nov 26 13:39 etc
  49. dr-xr-xr-x 1172 root root 0 Nov 26 13:39 proc
  50. drwxr-xr-x 1 root root 77 Nov 26 13:39 apps
  51. drwxr-xr-x 16 root root 4020 Nov 26 13:39 dev
  52. drwxr-xr-x 1 root root 43 Nov 26 13:39 run
  53. drwxrwxrwt 1 root root 68 Nov 30 17:47 tmp
  54. root@zabbix-unify:/#
  • 按标准时间格式, 显示时间, 便于 GREP 查找等操作
  • 详细解释:
    1. --time-style=TIME_STYLE time/date format with -l; see TIME_STYLE below

    The TIME_STYLE argument can be full-iso, long-iso, iso, locale, or +FORMAT. FORMAT is interpreted like in date(1). If FORMAT is FORMAT1FORMAT2, 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.

  1. ls -al --time-style=long-iso

image.png