缩短/替换ls的用户和组输出

时间:2017-05-05 06:35:44

标签: string shell ls

在我的工作笔记本电脑上输入ls -l输出:

drwx------+  3 abcd123e  VERYLONGDOMAINNAME\Domain Users   102 May  4 09:35 Movies
drwx------+  4 abcd123e  VERYLONGDOMAINNAME\Domain Users   136 May  5 00:25 Music
drwx------+  3 abcd123e  VERYLONGDOMAINNAME\Domain Users   102 May  4 09:35 Pictures

是否可以用ls的输出替换长而丑陋的组名(可能还有用户),而不是修改任何系统设置,类似于:

drwx------+  3 fb  dusers   102 May  4 09:35 Movies
drwx------+  4 fb  dusers   136 May  5 00:25 Music
drwx------+  3 fb  dusers   102 May  4 09:35 Pictures

1 个答案:

答案 0 :(得分:1)

<击> 我最近能做的就是

alias ll="ls -l | sed -- 's/USER GROUPNAME/replacement/'"

示例

$ ls -l
drwxr-xr-x+   3 Theofanis  staff      102  4 May 13:01 Desktop
drwx------+  26 Theofanis  staff      884 20 Mar 13:59 Documents
drwx------+  22 Theofanis  staff      748  5 May 12:38 Downloads

$ alias ll="ls -l | sed -- 's/Theofanis  staff/somebody/'"

$ ll
drwxr-xr-x+   3 somebody      102  4 May 13:01 Desktop
drwx------+  26 somebody      884 20 Mar 13:59 Documents
drwx------+  22 somebody      748  5 May 12:38 Downloads

<击>

更新以符合新要求

  • 添加了$@,以便您可以将参数传递给 ls
  • 添加--color=always以不断输出颜色。
  • 还添加了一个示例来替换用户/组的多个组合。


function ll() {
    /bin/ls -l --color=always "$@" | 
    sed -- 's/USER1 GROUPNAME1/replacement1/' | 
    sed -- 's/USER2 GROUPNAME2/replacement2/'
}

实施例

$ ls -l
  drwxr-xr-x+   3 Theofanis  staff      102  4 May 13:01 Desktop
  drwx------+  26 Theofanis  staff      884 20 Mar 13:59 Documents
  drwx------+  22 Theofanis  staff      748  5 May 12:38 Downloads
  drwx------+  22 Theofanis  admin      748  5 May 12:38 Public

$ function ll() {
      ls -l --color=always "$@" |
      sed -- 's/Theofanis  staff/somebody/' | 
      sed -- 's/Theofanis  admin/someone/'
  }

$ ll
  drwxr-xr-x+   3 somebody      102  4 May 13:01 Desktop
  drwx------+  26 somebody      884 20 Mar 13:59 Documents
  drwx------+  22 somebody      748  5 May 12:38 Downloads
  drwx------+  22 someone      748  5 May 12:38 Public

$ ll -F # appends '/' on directories
  drwxr-xr-x+   3 somebody      102  4 May 13:01 Desktop/
  drwx------+  26 somebody      884 20 Mar 13:59 Documents/
  drwx------+  22 somebody      748  5 May 12:38 Downloads/
  drwx------+  22 someone      748  5 May 12:38 Public/