前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Linux / MacOS 修改 ls 显示年月日的时间格式

Linux / MacOS 修改 ls 显示年月日的时间格式

作者头像
阳光岛主
发布2020-02-25 12:35:14
4.3K0
发布2020-02-25 12:35:14
举报
文章被收录于专栏:米扑专栏米扑专栏

Linux 修改时间显示格式

本文参考转自米扑博客:Linux / MacOS 修改 ls 显示年月日的时间格式

Linux 系统的 ls 默认支持 --full-time,但是 MacOS 系统下的 ls 不支持 --full-time

1、Linux 系统下的 ls --full-time

1 2 3 4 5 6 7 8 9 10 11 12 13

# ll /var/log/ | head -5 total 10024 drwxr-x---. 2 root   root      4096 May 28  2014 audit -rw-r--r--  1 root   root      9835 May 30  2018 boot.log -rw-------  1 root   utmp     62208 Dec  8 19:53 btmp -rw-------  1 root   utmp    235392 Dec  1 04:14 btmp-20191201 [root@mimvp-bj ~]# [root@mimvp-bj ~]# ll --full-time /var/log/ | head -5  total 10024 drwxr-x---. 2 root   root      4096 2014-05-28 22:37:06.000000000 +0800 audit -rw-r--r--  1 root   root      9835 2018-05-30 09:13:01.455403717 +0800 boot.log -rw-------  1 root   utmp     62208 2019-12-08 19:53:53.303521055 +0800 btmp -rw-------  1 root   utmp    235392 2019-12-01 04:14:21.953041715 +0800 btmp-20191201

2、Linux 自定义日期格式 (推荐)

# Linux 下修改 .bashrc 或者 .profile , 添加 export TIME_STYLE='+%Y-%m-%d %H:%M:%S' 即可

1 2 3 4 5

vim ~/.bashrc 在最后一行添加:export TIME_STYLE='+%Y-%m-%d__%H:%M:%S'   接着,使修改的配置生效 source ~/.bashrc

最后,验证

1 2 3 4 5 6 7 8 9 10 11 12

[root@mimvp-bj ~]# ll /var/log/ | head -5 total 10024 drwxr-x---. 2 root   root      4096 2014-05-28__22:37:06 audit -rw-r--r--  1 root   root      9835 2018-05-30__09:13:01 boot.log -rw-------  1 root   utmp     62208 2019-12-08__19:53:53 btmp -rw-------  1 root   utmp    235392 2019-12-01__04:14:21 btmp-20191201 [root@mimvp-bj ~]# ll --full-time /var/log/ | head -5 total 10024 drwxr-x---. 2 root   root      4096 2014-05-28 22:37:06.000000000 +0800 audit -rw-r--r--  1 root   root      9835 2018-05-30 09:13:01.455403717 +0800 boot.log -rw-------  1 root   utmp     62208 2019-12-08 19:53:53.303521055 +0800 btmp -rw-------  1 root   utmp    235392 2019-12-01 04:14:21.953041715 +0800 btmp-20191201

注意:

若想跟 --full-time 格式一样,显示出纳秒(秒 - 毫秒 - 微妙 - 纳秒),纳秒日期格式为:%N,详见米扑博客:Linux date日期格式及加减运算

即日志格式化:export TIME_STYLE='+%Y-%m-%d__%H:%M:%S.%N'

1 2 3 4 5

vim ~/.bashrc 在最后一行添加:export TIME_STYLE='+%Y-%m-%d__%H:%M:%S.%N'   接着,使修改的配置生效 source ~/.bashrc

最后,验证

1 2 3 4 5 6 7 8 9 10 11 12 13

[root@mimvp-bj ~]# ll /var/log/ | head -5            total 10028 drwxr-x---. 2 root   root      4096 2014-05-28__22:37:06.000000000 audit -rw-r--r--  1 root   root      9835 2018-05-30__09:13:01.455403717 boot.log -rw-------  1 root   utmp     62208 2019-12-08__19:53:53.303521055 btmp -rw-------  1 root   utmp    235392 2019-12-01__04:14:21.953041715 btmp-20191201 [root@mimvp-bj ~]# [root@mimvp-bj ~]# ll --full-time /var/log/ | head -5 total 10028 drwxr-x---. 2 root   root      4096 2014-05-28 22:37:06.000000000 +0800 audit -rw-r--r--  1 root   root      9835 2018-05-30 09:13:01.455403717 +0800 boot.log -rw-------  1 root   utmp     62208 2019-12-08 19:53:53.303521055 +0800 btmp -rw-------  1 root   utmp    235392 2019-12-01 04:14:21.953041715 +0800 btmp-20191201

Macbook  修改时间显示格式

1、Macbook(Unix)可以使用 ls -lT (增加了时分秒,但年月日格式不变)

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18

yg-mac:~ homer$ ll /var/log/ | head total 243160 drwxr-xr-x   3 root             wheel                 102 12 19  2017 Bluetooth -rw-r--r--@  1 root             wheel                  12  4 13  2018 CDIS.custom drwxr-xr-x   2 root             wheel                  68 10 19  2017 CoreDuet -rw-r--r--   1 root             wheel                   0  4 21  2018 alf.log drwxr-xr-x   2 root             wheel                  68 10 10  2017 apache2 -rw-r--r--   1 root             wheel                   0  4 21  2018 appfirewall.log yg-mac:~ homer$ yg-mac:~ homer$ yg-mac:~ homer$ ll -lT /var/log/ | head total 243160 drwxr-xr-x   3 root             wheel                 102 12 19 20:27:33 2017 Bluetooth -rw-r--r--@  1 root             wheel                  12  4 13 23:05:38 2018 CDIS.custom drwxr-xr-x   2 root             wheel                  68 10 19 11:39:25 2017 CoreDuet -rw-r--r--   1 root             wheel                   0  4 21 11:10:17 2018 alf.log drwxr-xr-x   2 root             wheel                  68 10 10 10:58:11 2017 apache2 -rw-r--r--   1 root             wheel                   0  4 21 11:10:48 2018 appfirewall.log

2、Macbook(Unix)自定义日期格式(没生效,失败)

1 2 3 4 5

vim ~/.bashrc 在最后一行添加:export TIME_STYLE='+%Y-%m-%d__%H:%M'   接着,使修改的配置生效 source ~/.bashrc

打印定义的变量 TIME_STYLE,并验证日期格式,没生效!失败了!!!

1 2 3 4 5 6 7 8 9

yg-mac:~ homer$ echo $TIME_STYLE      +%Y-%m-%d__%H:%M:%S yg-mac:~ homer$ yg-mac:~ homer$ ll /var/log/ | head -5 total 243688 drwxr-xr-x   3 root             wheel                 102 12 19  2017 Bluetooth -rw-r--r--@  1 root             wheel                  12  4 13  2018 CDIS.custom drwxr-xr-x   2 root             wheel                  68 10 19  2017 CoreDuet drwxrwx---  33 root             admin                1122 12  8 11:00 DiagnosticMessages

3、Macbook(Unix)借助第三方日期格式 (成功,推荐)

格式依旧不太直观, 安装 `coreutils`, 使用 `gls`

1)brew install coreutils    (注:经测试,只支持 MacOS,不支持Linux,因为MacOS下安装的 gls 本身同 Linux 下的 ls)

2)which gls

1 2

yg-mac:~ homer$ which gls /usr/local/bin/gls

3)man gls   (功能同 Linux 下的 ls 一样了,因为 MacOS默认ls与Linux并不同)

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95

yg-mac:~ homer$ gls --help Usage: gls [OPTION]... [FILE]... List information about the FILEs (the current directory by default). Sort entries alphabetically if none of -cftuvSUX nor --sort is specified.   Mandatory arguments to long options are mandatory for short options too.   -a, --all                  do not ignore entries starting with .   -A, --almost-all           do not list implied . and ..       --author               with -l, print the author of each file   -b, --escape               print C-style escapes for nongraphic characters       --block-size=SIZE      with -l, scale sizes by SIZE when printing them;                                e.g., '--block-size=M'; see SIZE format below   -B, --ignore-backups       do not list implied entries ending with ~   -c                         with -lt: sort by, and show, ctime (time of last                                modification of file status information);                                with -l: show ctime and sort by name;                                otherwise: sort by ctime, newest first   -C                         list entries by columns       --color[=WHEN]         colorize the output; WHEN can be 'always' (default                                if omitted), 'auto', or 'never'; more info below   -d, --directory            list directories themselves, not their contents   -D, --dired                generate output designed for Emacs' dired mode   -f                         do not sort, enable -aU, disable -ls --color   -F, --classify             append indicator (one of */=>@|) to entries       --file-type            likewise, except do not append '*'       --format=WORD          across -x, commas -m, horizontal -x, long -l,                                single-column -1, verbose -l, vertical -C       --full-time            like -l --time-style=full-iso   -g                         like -l, but do not list owner       --group-directories-first                              group directories before files;                                can be augmented with a --sort option, but any                                use of --sort=none (-U) disables grouping   -G, --no-group             in a long listing, don't print group names   -h, --human-readable       with -l and -s, print sizes like 1K 234M 2G etc.       --si                   likewise, but use powers of 1000 not 1024   -H, --dereference-command-line                              follow symbolic links listed on the command line       --dereference-command-line-symlink-to-dir                              follow each command line symbolic link                                that points to a directory       --hide=PATTERN         do not list implied entries matching shell PATTERN                                (overridden by -a or -A)       --hyperlink[=WHEN]     hyperlink file names; WHEN can be 'always'                                (default if omitted), 'auto', or 'never'       --indicator-style=WORD  append indicator with style WORD to entry names:                                none (default), slash (-p),                                file-type (--file-type), classify (-F)   -i, --inode                print the index number of each file   -I, --ignore=PATTERN       do not list implied entries matching shell PATTERN   -k, --kibibytes            default to 1024-byte blocks for disk usage;                                used only with -s and per directory totals   -l                         use a long listing format   -L, --dereference          when showing file information for a symbolic                                link, show information for the file the link                                references rather than for the link itself   -m                         fill width with a comma separated list of entries   -n, --numeric-uid-gid      like -l, but list numeric user and group IDs   -N, --literal              print entry names without quoting   -o                         like -l, but do not list group information   -p, --indicator-style=slash                              append / indicator to directories   -q, --hide-control-chars   print ? instead of nongraphic characters       --show-control-chars   show nongraphic characters as-is (the default,                                unless program is 'ls' and output is a terminal)   -Q, --quote-name           enclose entry names in double quotes       --quoting-style=WORD   use quoting style WORD for entry names:                                literal, locale, shell, shell-always,                                shell-escape, shell-escape-always, c, escape                                (overrides QUOTING_STYLE environment variable)   -r, --reverse              reverse order while sorting   -R, --recursive            list subdirectories recursively   -s, --size                 print the allocated size of each file, in blocks   -S                         sort by file size, largest first       --sort=WORD            sort by WORD instead of name: none (-U), size (-S),                                time (-t), version (-v), extension (-X)       --time=WORD            with -l, show time as WORD instead of default                                modification time: atime or access or use (-u);                                ctime or status (-c); also use specified time                                as sort key if --sort=time (newest first)       --time-style=TIME_STYLE  time/date format with -l; see TIME_STYLE below   -t                         sort by modification time, newest first   -T, --tabsize=COLS         assume tab stops at each COLS instead of 8   -u                         with -lt: sort by, and show, access time;                                with -l: show access time and sort by name;                                otherwise: sort by access time, newest first   -U                         do not sort; list entries in directory order   -v                         natural sort of (version) numbers within text   -w, --width=COLS           set output width to COLS.  0 means no limit   -x                         list entries by lines instead of by columns   -X                         sort alphabetically by entry extension   -Z, --context              print any security context of each file   -1                         list one file per line.  Avoid '\n' with -q or -b       --help     display this help and exit       --version  output version information and exit

上面帮助里,划重点:

      --full-time            like -l --time-style=full-iso

      --time-style=TIME_STYLE  time/date format with -l; see TIME_STYLE below

4)gls 使用示例

gls --full-time /var/log/ | head -5

1 2 3 4 5 6

$ gls --full-time /var/log/ | head -5 total 122080 drwxr-xr-x  3 root            wheel                102 2017-12-19 20:27:33.000000000 +0800 Bluetooth -rw-r--r--  1 root            wheel                 12 2018-04-13 23:05:38.000000000 +0800 CDIS.custom drwxr-xr-x  2 root            wheel                 68 2017-10-19 11:39:25.000000000 +0800 CoreDuet drwxrwx--- 33 root            admin               1122 2019-12-08 11:00:01.000000000 +0800 DiagnosticMessages

gls -l --time-style 的多种等价格式 (强烈推荐)

gls -l --time-style +'%Y-%m-%d__%H:%M:%S.%N' /var/log/ | head -5

gls -l --time-style "+%Y-%m-%d__%H:%M:%S.%N" /var/log/ | head -5

gls -l --time-style="+%Y-%m-%d__%H:%M:%S.%N" /var/log/ | head -5

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

$ gls -l --time-style +'%Y-%m-%d__%H:%M:%S.%N' /var/log/ | head -5 total 122164 drwxr-xr-x  3 root            wheel                102 2017-12-19__20:27:33.000000000 Bluetooth -rw-r--r--  1 root            wheel                 12 2018-04-13__23:05:38.000000000 CDIS.custom drwxr-xr-x  2 root            wheel                 68 2017-10-19__11:39:25.000000000 CoreDuet drwxrwx--- 33 root            admin               1122 2019-12-08__11:00:01.000000000 DiagnosticMessages $ yg-mac:~ homer$ gls -l --time-style "+%Y-%m-%d__%H:%M:%S.%N" /var/log/ | head -5     total 122168 drwxr-xr-x  3 root            wheel                102 2017-12-19__20:27:33.000000000 Bluetooth -rw-r--r--  1 root            wheel                 12 2018-04-13__23:05:38.000000000 CDIS.custom drwxr-xr-x  2 root            wheel                 68 2017-10-19__11:39:25.000000000 CoreDuet drwxrwx--- 33 root            admin               1122 2019-12-08__11:00:01.000000000 DiagnosticMessages $ $ gls -l --time-style="+%Y-%m-%d__%H:%M:%S.%N" /var/log/ | head -5 total 122172 drwxr-xr-x  3 root            wheel                102 2017-12-19__20:27:33.000000000 Bluetooth -rw-r--r--  1 root            wheel                 12 2018-04-13__23:05:38.000000000 CDIS.custom drwxr-xr-x  2 root            wheel                 68 2017-10-19__11:39:25.000000000 CoreDuet drwxrwx--- 33 root            admin               1122 2019-12-08__11:00:01.000000000 DiagnosticMessages

--time-style 的更多日期格式和参数,请见米扑博客:Linux date日期格式及加减运算

5)自定义重命名 alias (不推荐,容易混,直接使用 gls 代替 ls 即可,也很简便)

修改 .bashrc 或者 .profile 

1 2 3 4 5 6 7

vim ~/.bashrc 在最后一行添加两行,并保存 alias ls="gls -G" alias ll="ls -al"   接着,使修改的配置生效 source ~/.bashrc

使用测试结果

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17

$ ll /var/log/ | head -5 total 122256 drwxr-xr-x 69 root                2346 12  8 19:39 . drwxr-xr-x 30 root                1020  8  9  2018 .. drwxr-xr-x  3 root                 102 12 19  2017 Bluetooth -rw-r--r--  1 root                  12  4 13  2018 CDIS.custom $ $ ll --full-time /var/log/ | head -5 total 122256 drwxr-xr-x 69 root                2346 2019-12-08 19:39:56.000000000 +0800 . drwxr-xr-x 30 root                1020 2018-08-09 23:50:36.000000000 +0800 .. drwxr-xr-x  3 root                 102 2017-12-19 20:27:33.000000000 +0800 Bluetooth -rw-r--r--  1 root                  12 2018-04-13 23:05:38.000000000 +0800 CDIS.custom $ $ ls -lT gls: option requires an argument -- 'T' Try 'gls --help' for more information.

由上可见,重命名 alias 后的 ls 与 Linux 下的 ls 已经完全一致,即 MacOS下的 gls 同 Linux 下的 ls

但是,MacOS 原生的 ls -lT 已经报错了,这也是不推荐重命名alias,而推荐直接使用 gls 的核心所在

那么,知道错了,如何恢复呢,直接注释掉 alias ?错!

哈哈,其实很简单,可能您别惊吓到了,没想到如此简单:

1 2 3 4 5 6 7

vim ~/.bashrc 在最后一行添加两行,并保存 alias ls="ls" alias ll="ls -al"   接着,使修改的配置生效 source ~/.bashrc

说明:ls 仍然修改回去即可,不要重命名 alias 到 gls 了,各用各的,安得其所

alias ls="ls" alias ll="ls -al"

最后,验证是否都各司其职:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

$ ll /var/log/ | head -5            total 244688 drwxr-xr-x  69 root             wheel                2346 12  8 19:39 . drwxr-xr-x  30 root             wheel                1020  8  9  2018 .. drwxr-xr-x   3 root             wheel                 102 12 19  2017 Bluetooth -rw-r--r--@  1 root             wheel                  12  4 13  2018 CDIS.custom $ $ gls --full-time /var/log/ | head -5 total 122344 drwxr-xr-x  3 root            wheel                102 2017-12-19 20:27:33.000000000 +0800 Bluetooth -rw-r--r--  1 root            wheel                 12 2018-04-13 23:05:38.000000000 +0800 CDIS.custom drwxr-xr-x  2 root            wheel                 68 2017-10-19 11:39:25.000000000 +0800 CoreDuet drwxrwx--- 33 root            admin               1122 2019-12-08 11:00:01.000000000 +0800 DiagnosticMessages $ $ ls -lT /var/log/ | head -5 total 244696 drwxr-xr-x   3 root             wheel                 102 12 19 20:27:33 2017 Bluetooth -rw-r--r--@  1 root             wheel                  12  4 13 23:05:38 2018 CDIS.custom drwxr-xr-x   2 root             wheel                  68 10 19 11:39:25 2017 CoreDuet drwxrwx---  33 root             admin                1122 12  8 11:00:01 2019 DiagnosticMessages

说明:ls -lT /var/log/ | head -5  恢复正常,不报错,完美! 成功! 万岁!!

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-02-22 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档