The time-style Parameter in Linux ls

Sep 5, 2023 · 1 min read · 212 Words · -Views -Comments

While building WebShell, I needed a reliable way to get file lists and metadata from ls. Here’s a note.

Command

ls -Al --time-style=full-iso

The output looks like:

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

From this output, you can parse:

  1. Permissions
  2. Owner
  3. Group
  4. File size
  5. ISO time with timezone
  6. File name (and symlink target if ->)

Limitations / compatibility

time-style is not supported on all Linux distributions, and Unix-like systems may not support it either.

Examples:

  1. Alpine Linux does not support it and has no built-in equivalent.
  2. Unix-like systems such as FreeBSD do not support it.

Workarounds

  1. Install coreutils. On FreeBSD: pkg install coreutils. On Alpine: apk add coreutils.
  2. Avoid time-style and combine stat instead. The downside is worse performance due to extra commands.

Final Thoughts

Linux has many distributions. Be careful about compatibility, which depends on command versions and Linux/Unix standards.

Authors
Developer, digital product enthusiast, tinkerer, sharer, open source lover