Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

How to list the properties of folders in a directory. Like, I want to 'find ./ -type d' and at the same time 'll' them.

share|improve this question

3 Answers 3

up vote 1 down vote accepted

You can use find directly to print any details you want to see:

For the information similar to ll - you can show the ls -dils information with the -ls option:

find . -type d -ls

For example:

$ find /boot -type d -ls
3407873    4 drwxr-xr-x   3 root     root         4096 May  8 09:58 /boot
3407874    4 drwxr-xr-x   5 root     root         4096 Jul 31 03:32 /boot/grub
3408122    4 drwxr-xr-x   2 root     root         4096 Nov 27  2012 /boot/grub/fonts
3407884   12 drwxr-xr-x   2 root     root        12288 Apr 18 05:57 /boot/grub/i386-pc
3408117    4 drwxr-xr-x   2 root     root         4096 Apr 18 05:57 /boot/grub/locale

If you want to show more details: the option -printf takes a format string that allows you to show any propperties of the directory.

For example, to show the inode number and plain file name:

find . -type d -printf '%i %f\n'

The format string is described in man find:

    -printf format
           True;  print  format on the standard output, interpreting `\' escapes and `%' directives.  Field widths
           and precisions can be specified as with the `printf' C function.  Please note that many of  the  fields
           are  printed  as  %s rather than %d, and this may mean that flags don't work as you might expect.  This
           also means that the `-' flag does work (it forces fields to be left-aligned).  Unlike  -print,  -printf
           does not add a newline at the end of the string.  The escapes and directives are:

[... literal chars ...]

           A  `\'  character followed by any other character is treated as an ordinary character, so they both are
           printed.

           %%     A literal percent sign.

           %a     File's last access time in the format returned by the C `ctime' function.

           %Ak    File's last access time in the format specified by k, which is either `@' or a directive for the
                  C  `strftime'  function.   The possible values for k are listed below; some of them might not be
                  available on all systems, due to differences in `strftime' between systems.

[... many more time formats ...]

           %b     The amount of disk space used for this file in 512-byte blocks. Since disk space is allocated in
                  multiples  of  the filesystem block size this is usually greater than %s/512, but it can also be
                  smaller if the file is a sparse file.

           %c     File's last status change time in the format returned by the C `ctime' function.

           %Ck    File's last status change time in the format specified by k, which is the same as for %A.

           %d     File's depth in the directory tree; 0 means the file is a command line argument.

           %D     The device number on which the file exists (the st_dev field of struct stat), in decimal.

           %f     File's name with any leading directories removed (only the last element).

           %F     Type of the filesystem the file is on; this value can be used for -fstype.

           %g     File's group name, or numeric group ID if the group has no name.

           %G     File's numeric group ID.

           %h     Leading directories of file's name (all but the last element).  If the  file  name  contains  no
                  slashes (since it is in the current directory) the %h specifier expands to ".".

           %H     Command line argument under which file was found.

           %i     File's inode number (in decimal).

           %k     The  amount of disk space used for this file in 1K blocks. Since disk space is allocated in mul‐
                  tiples of the filesystem block size this is usually greater than %s/1024, but  it  can  also  be
                  smaller if the file is a sparse file.

           %l     Object of symbolic link (empty string if file is not a symbolic link).

           %m     File's  permission  bits (in octal).  This option uses the `traditional' numbers which most Unix
                  implementations use, but if your particular implementation uses an  unusual  ordering  of  octal
                  permissions  bits, you will see a difference between the actual value of the file's mode and the
                  output of %m.   Normally you will want to have a leading zero on this number, and  to  do  this,
                  you should use the # flag (as in, for example, `%#m').

           %M     File's  permissions  (in  symbolic  form,  as for ls).  This directive is supported in findutils
                  4.2.5 and later.

           %n     Number of hard links to file.

           %p     File's name.

           %P     File's name with the name of the command line argument under which it was found removed.

           %s     File's size in bytes.

           %S     File's sparseness.  This is calculated as (BLOCKSIZE*st_blocks / st_size).  The exact value  you
                  will get for an ordinary file of a certain length is system-dependent.  However, normally sparse
                  files will have values less than 1.0, and files which use indirect blocks may have a value which
                  is  greater  than  1.0.    The  value used for BLOCKSIZE is system-dependent, but is usually 512
                  bytes.   If the file size is zero, the value printed is undefined.  On systems which  lack  sup‐
                  port for st_blocks, a file's sparseness is assumed to be 1.0.

           %t     File's last modification time in the format returned by the C `ctime' function.

           %Tk    File's last modification time in the format specified by k, which is the same as for %A.

           %u     File's user name, or numeric user ID if the user has no name.

           %U     File's numeric user ID.

           %y     File's type (like in ls -l), U=unknown type (shouldn't happen)

           %Y     File's type (like %y), plus follow symlinks: L=loop, N=nonexistent

           A `%' character followed by any other character is discarded, but the other character is printed (don't
           rely on this, as further format characters may be introduced).  A `%' at the end of the format argument
           causes  undefined  behaviour  since there is no following character.  In some locales, it may hide your
           door keys, while in others it may remove the final page from the novel you are reading.

           The %m and %d directives support the # , 0 and + flags, but the other directives do not, even  if  they
           print  numbers.   Numeric  directives that do not support these flags include G, U, b, D, k and n.  The
           `-' format flag is supported and changes the alignment of a field from right-justified  (which  is  the
           default) to left-justified.

           See  the  UNUSUAL  FILENAMES section for information about how unusual characters in filenames are han‐
           dled.


In case you need some other variant of a find command, you can do a whole lot with find - the man page is pretty long.

For example, if you would want to leave out hiden directories - those with names starting with ., and show only the first level of directories, you can use this:

find . -maxdepth 1 -type d -name '[^.]*' -printf '%i %p\n'

$ find /boot -maxdepth 1 -type d -name '[^.]*' -printf '%i %p\n' 
3407873 /boot
3407874 /boot/grub

(Take a look at the two new parts in the command - both are frequently useful.)

share|improve this answer
    
But when I use 'find . -type d -ls', it lists all folders including the hidden folder (.blahblah/). Can I exclude those hidden folders? And Can I NOT find it recursively? I only want the first level of folder. Thanks. –  winston Aug 4 at 18:06
    
@winston There's a new example. –  Volker Siegel Aug 4 at 18:52

This displayed all the properties of folder included inside the root directory ls -all / . Hope this is what you wanted

share|improve this answer

If your shell is zsh and you have extended globbling (setopt extended_glob in you .zshrc) you can use

ls -ld *(/)

The extended glob patter means: list all entries which are directories. If you need that to be recursive like in find, you can use

ls -ld **/*(/) 

I am sure there is the equivalent for bash too...

share|improve this answer
    
I'm not so sure, regarding the equivalent patterns in bash . –  Volker Siegel Aug 4 at 18:55

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.