ls 是 Linux 系统中最常用的命令之一,用于列出当前目录下的文件和目录。
概述
ls 命令是一个经常用到的文件管理器,用于显示当前目录中文件和文件夹的列表。 它支持多种参数,可以按照文件的类型,大小,权限,时间戳等属性显示文件信息。
语法
ls [选项] [文件]
选项
以下是 ls 命令的常用选项:
使用方法:ls [options] [file|dir]
-a
:显示所有文件,包括以.
开头的隐藏文件;-l
:以长格式显示文件详细信息;-h
:显示文件大小的可读格式;-R
:递归显示指定目录的内容;-r
:以相反的顺序,将文件列出;-t
:按时间顺序列出文件;-S
:按文件大小排序;-i
:显示文件的 inode 编号;-F
:在每个文件名后添加文件类型的标识符。实例
以适当方式显示文件大小:
ls -l
ls -l --block-size=m
ls -l --block-size=G
按照修改时间排序显示
-l use a long listing format 以长列表方式显示(详细信息方式)
-t sort by modification time 按修改时间排序(最新的在最前面)
-r reverse order while sorting (反序)
# 按照时间降序
ls -lt
# 按照时间升序
ls -ltr
root@zabbix-unify:/# # 按照时间降序
root@zabbix-unify:/# ls -lt
total 0
drwxrwxrwt 1 root root 68 Nov 30 17:47 tmp
drwxr-xr-x 1 root root 43 Nov 26 13:39 run
drwxr-xr-x 16 root root 4020 Nov 26 13:39 dev
drwxr-xr-x 1 root root 77 Nov 26 13:39 apps
dr-xr-xr-x 1175 root root 0 Nov 26 13:39 proc
drwxr-xr-x 1 root root 20 Nov 26 13:39 etc
dr-xr-xr-x 13 root root 0 Nov 25 19:53 sys
dr-xr-xr-x 1175 root root 0 Nov 25 19:53 host
drwx------ 2 root root 37 Jun 9 15:31 root
drwxr-xr-x 1 root root 45 Jun 9 15:31 var
drwxr-xr-x 2 root root 6 Jun 9 15:27 media
drwxr-xr-x 2 root root 6 Jun 9 15:27 mnt
drwxr-xr-x 2 root root 6 Jun 9 15:27 opt
drwxr-xr-x 2 root root 6 Jun 9 15:27 srv
drwxr-xr-x 1 root root 19 Jun 9 15:27 usr
lrwxrwxrwx 1 root root 7 Jun 9 15:27 bin -> usr/bin
lrwxrwxrwx 1 root root 7 Jun 9 15:27 lib -> usr/lib
lrwxrwxrwx 1 root root 9 Jun 9 15:27 lib32 -> usr/lib32
lrwxrwxrwx 1 root root 9 Jun 9 15:27 lib64 -> usr/lib64
lrwxrwxrwx 1 root root 10 Jun 9 15:27 libx32 -> usr/libx32
lrwxrwxrwx 1 root root 8 Jun 9 15:27 sbin -> usr/sbin
drwxr-xr-x 2 root root 6 Apr 15 2020 boot
drwxr-xr-x 2 root root 6 Apr 15 2020 home
root@zabbix-unify:/#
root@zabbix-unify:/# # 按照时间升序
root@zabbix-unify:/# ls -ltr
total 0
drwxr-xr-x 2 root root 6 Apr 15 2020 home
drwxr-xr-x 2 root root 6 Apr 15 2020 boot
lrwxrwxrwx 1 root root 8 Jun 9 15:27 sbin -> usr/sbin
lrwxrwxrwx 1 root root 10 Jun 9 15:27 libx32 -> usr/libx32
lrwxrwxrwx 1 root root 9 Jun 9 15:27 lib64 -> usr/lib64
lrwxrwxrwx 1 root root 9 Jun 9 15:27 lib32 -> usr/lib32
lrwxrwxrwx 1 root root 7 Jun 9 15:27 lib -> usr/lib
lrwxrwxrwx 1 root root 7 Jun 9 15:27 bin -> usr/bin
drwxr-xr-x 1 root root 19 Jun 9 15:27 usr
drwxr-xr-x 2 root root 6 Jun 9 15:27 srv
drwxr-xr-x 2 root root 6 Jun 9 15:27 opt
drwxr-xr-x 2 root root 6 Jun 9 15:27 mnt
drwxr-xr-x 2 root root 6 Jun 9 15:27 media
drwxr-xr-x 1 root root 45 Jun 9 15:31 var
drwx------ 2 root root 37 Jun 9 15:31 root
dr-xr-xr-x 1172 root root 0 Nov 25 19:53 host
dr-xr-xr-x 13 root root 0 Nov 25 19:53 sys
drwxr-xr-x 1 root root 20 Nov 26 13:39 etc
dr-xr-xr-x 1172 root root 0 Nov 26 13:39 proc
drwxr-xr-x 1 root root 77 Nov 26 13:39 apps
drwxr-xr-x 16 root root 4020 Nov 26 13:39 dev
drwxr-xr-x 1 root root 43 Nov 26 13:39 run
drwxrwxrwt 1 root root 68 Nov 30 17:47 tmp
root@zabbix-unify:/#
- 按标准时间格式, 显示时间, 便于 GREP 查找等操作
- 详细解释:
--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 FORMAT1
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.
ls -al --time-style=long-iso