ITPub博客

首页 > Linux操作系统 > Linux操作系统 > [20130226]ls -l显示的时间问题.txt

[20130226]ls -l显示的时间问题.txt

原创 Linux操作系统 作者:lfree 时间:2013-02-26 11:04:13 0 删除 编辑
[20130226]ls -l显示的时间问题.txt

# touch --date='2013-01-01 07:01:00' aa.txt
# touch --date='2012-09-01 07:01:00' bb.txt
# touch a.txt b.txt c.txt

#  ls -l
-rw-r--r-- 1 root root    0 Jan  1 07:01 aa.txt
-rw-r--r-- 1 root root    0 Feb 26 09:20 a.txt
-rw-r--r-- 1 root root    0 Sep  1 07:01 bb.txt
-rw-r--r-- 1 root root    0 Feb 26 09:20 b.txt
-rw-r--r-- 1 root root    0 Feb 26 09:20 c.txt


个人非常不习惯这种显示格式(显示的好像是修改时间),没有年的显示,非常容易混淆,我希望看到年月日,实际上
而且不是英文的日期.今天仔细看了ls的man文档,发现定义--time-style参数,就可以很好的满足自己的条件.

--补充如果修改日期在当前日期减去半年,才会出现年的显示.
#  date
Tue Feb 26 11:09:19 CST 2013
#  touch --date='2012-08-27 08:01:00' bb.txt
#  /bin/ls -l
total 4
-rw-r--r-- 1 root root    0 Feb 26 09:42 aa.txt
-rw-r--r-- 1 root root    0 Feb 26 09:20 a.txt
-rw-r--r-- 1 root root    0 Aug 27  2012 bb.txt
-rw-r--r-- 1 root root    0 Feb 26 09:20 b.txt
-rw-r--r-- 1 root root    0 Feb 26 09:20 c.txt

#  touch --date='2012-08-28 08:01:00' bb.txt
#  /bin/ls -l
total 4
-rw-r--r-- 1 root root    0 Feb 26 09:42 aa.txt
-rw-r--r-- 1 root root    0 Feb 26 09:20 a.txt
-rw-r--r-- 1 root root    0 Aug 28 08:01 bb.txt
-rw-r--r-- 1 root root    0 Feb 26 09:20 b.txt
-rw-r--r-- 1 root root    0 Feb 26 09:20 c.txt
---

# ls -l --time-style=+'%Y-%m-%d %H:%M:%S'
-rw-r--r-- 1 root root    0 2013-01-01 07:01:00 aa.txt
-rw-r--r-- 1 root root    0 2013-02-26 09:20:05 a.txt
-rw-r--r-- 1 root root    0 2012-09-01 07:01:00 bb.txt
-rw-r--r-- 1 root root    0 2013-02-26 09:20:05 b.txt
-rw-r--r-- 1 root root    0 2013-02-26 09:20:05 c.txt

这样显示清晰多了.

修改/etc/profile.d/colorls.sh文件如下[注修改前最好做好文件的备份,避免改错回不来!,我的使用环境是centos6.2]:
 cat /etc/profile.d/colorls.sh
# color-ls initialization

#when USER_LS_COLORS defined do not override user LS_COLORS, but use them.
if [ -z "$USER_LS_COLORS" ]; then

  #ts="--time-style=+"%Y-%m-%d %H:%M:%S""
  alias ll='ls -l --time-style=+"%Y-%m-%d %H:%M:%S"' 2>/dev/null
  alias l.='ls -d .* --time-style=+"%Y-%m-%d %H:%M:%S"' 2>/dev/null


  # Skip the rest for noninteractive shells.
  [ -z "$PS1" ] && return

  COLORS=

  for colors in "$HOME/.dir_colors.$TERM" "$HOME/.dircolors.$TERM" \
      "$HOME/.dir_colors" "$HOME/.dircolors"; do
    [ -e "$colors" ] && COLORS="$colors" && break
  done

  [ -z "$COLORS" ] && [ -e "/etc/DIR_COLORS.256color" ] && \
      [ "x`tty -s && tput colors 2>/dev/null`" = "x256" ] && \
      COLORS="/etc/DIR_COLORS.256color"

  if [ -z "$COLORS" ]; then
    for colors in "/etc/DIR_COLORS.$TERM" "/etc/DIR_COLORS" ; do
      [ -e "$colors" ] && COLORS="$colors" && break
    done
  fi

  # Existence of $COLORS already checked above.
  [ -n "$COLORS" ] || return

  eval "`dircolors --sh "$COLORS" 2>/dev/null`"
  [ -z "$LS_COLORS" ] && return
  grep -qi "^COLOR.*none" $COLORS >/dev/null 2>/dev/null && return
fi

alias ll='ls -l --color=auto --time-style=+"%Y-%m-%d %H:%M:%S"' 2>/dev/null
alias l.='ls -d .* --color=auto --time-style=+"%Y-%m-%d %H:%M:%S"' 2>/dev/null
alias ls='ls --color=auto --time-style=+"%Y-%m-%d %H:%M:%S"' 2>/dev/null



来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/267265/viewspace-754738/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论
熟悉oracle相关技术,擅长sql优化,rman备份与恢复,熟悉linux shell编程。

注册时间:2008-01-03

  • 博文量
    4006
  • 访问量
    7970878