Linux下ls命令中的time-style参数
·
2 min read
在做WebShell时为了实现文件文件管理需要确定ls命令下如何获取文件列表及相关信息,这里mark下
实现命令
ls -Al --time-style=full-iso
通过上述命令执行可以得到如下格式结果
total 190600
-rw-r--r-- 1 root root 1781 2023-07-24 09:56:56.520113036 +0800 cosfs.sh
drwxrwxrw- 3 root root 4096 2023-06-05 16:32:39.466006640 +0800 dir1
drwxrwxrw- 2 root root 4096 2023-05-27 20:46:06.219707419 +0800 dir2
lrwxrwxrwx 1 root root 4 2023-09-05 21:45:49.401943527 +0800 dir3 -> dir1
drwxr-xr-x 3 root root 4096 2023-05-06 06:57:38.000000000 +0800 docs
drwxr-xr-x 484 root root 20480 2023-05-06 15:42:58.494411265 +0800 node_modules
drwxr-xr-x 4 root root 4096 2023-08-29 11:07:33.162370076 +0800 .orca_term
-rw-r--r-- 1 root root 195132152 2023-06-14 19:04:12.167000570 +0800 test 2.zip
根据结果,再通过正则可以获取到
- 权限信息
- 所属用户
- 所属组
- 文件大小
- ISO时间,包含时区信息
- 文件名,->即软链接
局限性/兼容性
如上time-style参数并非所有Linux均支持,另外类Unix的系统也并不支持。
这里举几个例子
- Alpine Linux即不支持,同时并没有内置的命令可以等效time-style。
- 类Unix如FreeBSD也并不支持
解决办法
- 安装
coreutils
,FreeBSD下执行pkg install coreutils
,Alpine Linux下执行apk add coreutils
- 放弃使用time-style参数,组合使用stat命令解决,当然坏处就是性能会差些,毕竟需要多命令执行
写在最后
- Linux系统众多,因此在平时使用的一些命令也需要注意兼容性,兼容性来自于两方面,1是命令版本,2是Linux/Unix遵从的规范不同,命令参数也会有所差异。