Linux / MacOS 修改 ls 顯示年月日的時間格式

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  恢復正常,不報錯,完美! 成功! 萬歲!!

 

 

參考推薦

Linux date日期格式及加減運算

CentOS 修改date系統時間

AWS EC2修改系統時區

Mac OS 查看網絡端口

Mac 強制退出程序的六種方法

Mac Launchctl 命令行參數詳解

MacOS 終端中文亂碼的解決方法

MacOS Charles 抓包使用實例

常用抓包工具的最全彙總

發佈了748 篇原創文章 · 獲贊 3584 · 訪問量 1413萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章