Existe uma maneira de limitar o maxdepth para ls -Rhal?

2

Existe uma maneira de limitar o maxdepth em um comando ls -Rhal ? Estou ciente do find . -maxdepth , mas quero poder obter as informações do arquivo que vem com ls -Rhal .

    
por Aaron 24.06.2013 / 22:09

1 resposta

2

Isso pode ajudar:

  1. Use find do -printf , isso dará o tamanho dos arquivos em Kb, então não é bem assim ls -lh :

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

    Saída:

    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
    

    Explicação:

    -printf format

          %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. Use find da opção -ls :

    find . -maxdepth 2 -ls
    

    Saída:

    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
    

    Explicação:

    -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. Use a sugestão de @mpy (esta é provavelmente a melhor solução):

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

    Saída:

    .:
    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. Use a opção '-exec' de find :

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

    Saída:

    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
    

    Explicação:

    -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.
    
por 25.06.2013 / 17:01

Tags