来自命令 stat 的稀疏格式

来自命令 stat 的稀疏格式

我想检查我的目录的状态

    $ ls
    Foo   Bar  Zoo

检查他们的状态

    $ ls | xargs stat -x
      File: "Zoo"
      Size: 384          FileType: Directory
      Mode: (0755/drwxr-xr-x)         Uid: (  501/ ..)  Gid: (   20/   ..)
    Device: 1,4   Inode: 8603392907    Links: 12
    Access: Fri Mar 30 09:12:06 2018
    Modify: Wed Mar 28 10:07:32 2018
    Change: Wed Mar 28 10:07:32 2018
      File: "Bar"
      Size: 160          FileType: Directory
      Mode: (0755/drwxr-xr-x)         Uid: (  501/  ..)  Gid: (   20/   ..)
    Device: 1,4   Inode: 8604964551    Links: 5
    Access: Fri Mar 30 09:12:06 2018
    Modify: Thu Mar 29 20:48:46 2018
    Change: Thu Mar 29 20:48:46 2018
      File: "Zoo"
      Size: 416          FileType: Directory
      Mode: (0755/drwxr-xr-x)         Uid: (  501/  ..)  Gid: (   20/   ..)
    Device: 1,4   Inode: 8603830641    Links: 13
    Access: Fri Mar 30 09:28:54 2018
    Modify: Fri Mar 30 09:28:54 2018
    Change: Fri Mar 30 09:28:54 2018

格式很密集,我更喜欢稀疏格式来直观地区分每个文件或目录。

    $ ls | xargs stat -x
      File: "Foo"
      Size: 384          FileType: Directory
      Mode: (0755/drwxr-xr-x)         Uid: (  501/ ..)  Gid: (   20/   ..)
    Device: 1,4   Inode: 8603392907    Links: 12
    Access: Fri Mar 30 09:12:06 2018
    Modify: Wed Mar 28 10:07:32 2018
    Change: Wed Mar 28 10:07:32 2018

      File: "Bar"
      Size: 160          FileType: Directory
      Mode: (0755/drwxr-xr-x)         Uid: (  501/  ..)  Gid: (   20/   ..)
    Device: 1,4   Inode: 8604964551    Links: 5
    Access: Fri Mar 30 09:12:06 2018
    Modify: Thu Mar 29 20:48:46 2018
    Change: Thu Mar 29 20:48:46 2018

      File: "Zoo"
      Size: 416          FileType: Directory
      Mode: (0755/drwxr-xr-x)         Uid: (  501/  ..)  Gid: (   20/   ..)
    Device: 1,4   Inode: 8603830641    Links: 13
    Access: Fri Mar 30 09:28:54 2018
    Modify: Fri Mar 30 09:28:54 2018
    Change: Fri Mar 30 09:28:54 2018

如何实现?

答案1

由于您使用的是 BSD stat,因此您可以使用该-f选项显式指定格式,包括您想要的任何尾随新行。手动写出整个格式字符串-x可能有点乏味,所以这里是获得的格式字符串从源头,组合成一个字符串:

'  File: "%N"%n  Size: %-11z  FileType: %HT%n  Mode: (%OMp%03OLp/%.10Sp)         Uid: (%5u/%8Su)  Gid: (%5g/%8Sg)%nDevice: %Hd,%Ld   Inode: %i    Links: %l%nAccess: %Sa%nModify: %Sm%nChange: %Sc'

%n在末尾添加几个以获得您想要的效果:

$ stat_format='  File: "%N"%n  Size: %-11z  FileType: %HT%n  Mode: (%OMp%03OLp/%.10Sp)         Uid: (%5u/%8Su)  Gid: (%5g/%8Sg)%nDevice: %Hd,%Ld   Inode: %i    Links: %l%nAccess: %Sa%nModify: %Sm%nChange: %Sc%n%n'
$ stat -f "${stat_format}"  .zshrc .bashrc
  File: ".zshrc"
  Size: 5878         FileType: Regular File
  Mode: (0644/-rw-r--r--)         Uid: (  502/muru)  Gid: (   20/   muru)
Device: 1,5   Inode: 8596072804    Links: 1
Access: Mar 30 11:40:18 2018
Modify: Mar 26 13:34:14 2018
Change: Mar 26 13:34:14 2018

  File: ".bashrc"
  Size: 3768         FileType: Regular File
  Mode: (0644/-rw-r--r--)         Uid: (  502/muru)  Gid: (   20/   muru)
Device: 1,5   Inode: 8593701774    Links: 1
Access: Mar 30 11:40:18 2018
Modify: Jan 28 11:15:12 2018
Change: Jan 28 11:15:12 2018

答案2

有很多方法可以做你想做的事。最简单的之一如下:

$ ls | xargs stat -x | sed 'n;n;n;n;n;n;s/$/\n/' 

注意 - s 的数量n;可能会减少 1,因为我所在的系统不支持-x输出选项。

相关内容