Ls

來自命令 stat 的稀疏格式

  • March 30, 2018

我想檢查我的目錄的狀態

   $ 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

如何實現?

由於您使用的是 BSD stat,因此您可以使用該-f選項明確指定格式,包括您想要的任何尾隨新行。手動寫出整個格式字元串-x可能有點乏味,所以這裡是從 source獲得的格式字元串,組合成一個字元串:

'  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

引用自:https://unix.stackexchange.com/questions/434423