Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Is there a way to limit maxdepth in a ls -Rhal command? I am aware of find . -maxdepth, but I want to be able to get the file information that comes with ls -Rhal.

share|improve this question
2  
Will find . -maxdepth 2 -type d -print0 | xargs -0 ls -hal help you? –  mpy Jun 24 '13 at 20:33
    
Yes thank you. Looks like I need to dive deeper into the man pages next time. –  Aaron Jun 25 '13 at 11:59

1 Answer 1

These might help:

  1. Use find's -printf, this will give the size of files in Kb, so not quite ls -lh:

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

    Output:

    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
    

    Explanation:

    -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's -ls option:

    find . -maxdepth 2 -ls
    

    Output:

    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
    

    Explanation:

    -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 @mpy's suggestion (this is probably the best solution):

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

    Output:

    .:
    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 find's ' -exec` option:

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

    Output:

    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
    

    Explanation:

    -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.
    
share|improve this answer
    
Thank you for the expanded selections and explanations. Can you explain from an under the hood perspective what the difference is between using the exec and the xargs options? –  Aaron Jun 25 '13 at 16:32
    
@Aaron, not much really. Only that xargs will optimize the commands so as to launch as few ls instances as possible, it won't really make a difference for what you are trying to do. –  terdon Jun 25 '13 at 17:17

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.