有没有办法限制 ls -Rhal 的最大深度?

有没有办法限制 ls -Rhal 的最大深度?

有没有办法限制ls -Rhal命令中的 maxdepth?我知道find . -maxdepth,但我希望能够获取附带的文件信息ls -Rhal

答案1

以下可能会有帮助:

  1. 使用,这将给出文件的大小(以 Kb 为单位),所以不完全find是:-printfls -lh

    find . -maxdepth 2 -printf '%M %n %u %g %kK %Tc %p\n'
    

    输出:

    drwxr-xr-x 4 terdon terdon 140K Tue 25 Jun 2013 04:52:17 PM CEST .
    -rw-r--r-- 1 terdon terdon 64K Mon 24 Jun 2013 03:46:05 PM CEST ./foo.pdf
    drwxr-xr-x 2 terdon terdon 4K Tue 25 Jun 2013 03:10:22 PM CEST ./hho
    -rw-r--r-- 1 terdon terdon 0K Tue 25 Jun 2013 03:10:22 PM CEST ./hho/b
    drwxr-xr-x 3 terdon terdon 4K Tue 25 Jun 2013 04:23:29 PM CEST ./haha
    drwxr-xr-x 3 terdon terdon 4K Tue 25 Jun 2013 04:24:05 PM CEST ./haha/hoho
    -rw-r--r-- 1 terdon terdon 0K Tue 25 Jun 2013 03:10:16 PM CEST ./haha/a
    

    解释:

    -printf 格式

          %M     File's permissions (in  symbolic  form,
                 as  for  ls).   This  directive is sup‐
                 ported in findutils 4.2.5 and later.
          %n     Number of hard links to file.
          %u     File's user name, or numeric user ID if
                 the user has no name.
          %g     File's  group name, or numeric group ID
                 if the group has no name.
          %k     The  amount of disk space used for this
                 file in 1K blocks. Since disk space  is
                 allocated  in multiples of the filesys‐
                 tem block size this is usually  greater
                 than   %s/1024,  but  it  can  also  be
                 smaller if the file is a sparse file.
          %Tk    File's last modification  time  in  the
                 format  specified  by  k,  which is the
                 same as for %A.
    
                 c      locale's  date and time (Sat Nov
                        04 12:02:33 EST 1989).  The for‐
                        mat  is the same as for ctime(3)
                        and so to preserve compatibility
                        with  that  format,  there is no
                        fractional part in  the  seconds
                        field.
          %p     File's name.
    
  2. 使用find-ls选项:

    find . -maxdepth 2 -ls
    

    输出:

    16728263  140 drwxr-xr-x   4 terdon terdon   143360 Jun 25 16:52 .
    16719778   64 -rw-r--r--   1 terdon   terdon      65465 Jun 24 15:46 ./foo.pdf
    16744471    4 drwxr-xr-x   2 terdon   terdon       4096 Jun 25 15:10 ./hho
    16723388    0 -rw-r--r--   1 terdon   terdon          0 Jun 25 15:10 ./hho/b
    16744470    4 drwxr-xr-x   3 terdon   terdon       4096 Jun 25 16:23 ./haha
    16744472    4 drwxr-xr-x   3 terdon   terdon       4096 Jun 25 16:24 ./haha/hoho
    16719491    0 -rw-r--r--   1 terdon   terdon          0 Jun 25 15:10 ./haha/a
    

    解释:

    -ls    True;  list current file in ls -dils format on
          standard output.  The block counts are  of  1K
          blocks,   unless   the   environment  variable
          POSIXLY_CORRECT is set, in which case 512-byte
          blocks  are  used.   See the UNUSUAL FILENAMES
          section  for  information  about  how  unusual
          characters in filenames are handled.
    
  3. 使用@mpy的建议(这可能是最好的解决办法):

    find . -maxdepth 2 -type d -print0 | xargs -0 ls -hal
    

    输出:

    .:
    total 696K
    drwxr-xr-x   4 terdon terdon 140K Jun 25 16:56 .
    drwxr-xr-x 141 terdon terdon 480K Jun 25 16:25 ..
    -rw-r--r--   1 terdon terdon    0 Jun 25 16:56 a
    -rw-r--r--   1 terdon   terdon    64K Jun 24 15:46 foo.pdf
    drwxr-xr-x   3 terdon   terdon   4.0K Jun 25 16:23 haha
    drwxr-xr-x   2 terdon   terdon   4.0K Jun 25 15:10 hho
    
    ./haha:
    total 148K
    drwxr-xr-x 3 terdon   terdon   4.0K Jun 25 16:23 .
    drwxr-xr-x 4 terdon terdon 140K Jun 25 16:56 ..
    -rw-r--r-- 1 terdon   terdon      0 Jun 25 15:10 a
    drwxr-xr-x 3 terdon   terdon   4.0K Jun 25 16:24 hoho
    
    ./haha/hoho:
    total 12K
    drwxr-xr-x 3 terdon terdon 4.0K Jun 25 16:24 .
    drwxr-xr-x 3 terdon terdon 4.0K Jun 25 16:23 ..
    -rw-r--r-- 1 terdon terdon    0 Jun 25 16:23 ha
    drwxr-xr-x 2 terdon terdon 4.0K Jun 25 16:24 hihi
    
    ./hho:
    total 144K
    drwxr-xr-x 2 terdon   terdon   4.0K Jun 25 15:10 .
    drwxr-xr-x 4 terdon terdon 140K Jun 25 16:56 ..
    -rw-r--r-- 1 terdon   terdon      0 Jun 25 15:10 b
    
  4. 使用find“-exec”选项:

    find . -maxdepth 2 -exec ls -hal '{}' \;
    

    输出:

    total 696K
    drwxr-xr-x   4 terdon terdon 140K Jun 25 16:56 .
    drwxr-xr-x 141 terdon terdon 480K Jun 25 16:25 ..
    -rw-r--r--   1 terdon terdon    0 Jun 25 16:59 a
    -rw-r--r--   1 terdon   terdon    64K Jun 24 15:46 foo.pdf
    drwxr-xr-x   3 terdon   terdon   4.0K Jun 25 16:23 haha
    drwxr-xr-x   2 terdon   terdon   4.0K Jun 25 15:10 hho
    -rw-r--r-- 1 terdon terdon 64K Jun 24 15:46 ./foo.pdf
    total 144K
    drwxr-xr-x 2 terdon   terdon   4.0K Jun 25 15:10 .
    drwxr-xr-x 4 terdon terdon 140K Jun 25 16:56 ..
    -rw-r--r-- 1 terdon   terdon      0 Jun 25 15:10 b
    -rw-r--r-- 1 terdon terdon 0 Jun 25 15:10 ./hho/b
    -rw-r--r-- 1 terdon terdon 0 Jun 25 16:59 ./a
    total 148K
    drwxr-xr-x 3 terdon   terdon   4.0K Jun 25 16:23 .
    drwxr-xr-x 4 terdon terdon 140K Jun 25 16:56 ..
    -rw-r--r-- 1 terdon   terdon      0 Jun 25 15:10 a
    drwxr-xr-x 3 terdon   terdon   4.0K Jun 25 16:24 hoho
    total 12K
    drwxr-xr-x 3 terdon terdon 4.0K Jun 25 16:24 .
    drwxr-xr-x 3 terdon terdon 4.0K Jun 25 16:23 ..
    -rw-r--r-- 1 terdon terdon    0 Jun 25 16:23 ha
    drwxr-xr-x 2 terdon terdon 4.0K Jun 25 16:24 hihi
    -rw-r--r-- 1 terdon terdon 0 Jun 25 15:10 ./haha/a
    

    解释:

    -exec command ; 
          Execute command; true if 0 status is returned.
          All following arguments to find are taken to be
          arguments to the command until an argument consisting of
          `;' is encountered.  The string `{}' is replaced by the
          current file name being processed everywhere it occurs
          in the arguments to the command, not just in arguments
          where it is alone, as in some versions of find.  Both of
          these constructions might need to be escaped (with a
          `\') or quoted to protect them from expansion by the
          shell.  See the EXAMPLES section for examples of the use
          of the -exec option.  The specified command is run once
          for each matched file.  The command is executed in the
          starting directory.  There are unavoidable security
          problems surrounding use of the -exec action; you should
          use the -execdir option instead.
    

相关内容