How can I list files by type with ls?

You can filter out everything but directories using grep this way:

ls -l | grep '^d'

the ^ indicates that the pattern is at the beginning of the line. Replace d with -, l, etc., as applicable.

You can of course use other commands to directly search for specific types (e.g. find . -maxdepth 1 -type d) or use ls -l | sort to group similar types together based on this first character, but if you want to filter you should use grep to only select the appropriate lines from the output.


If you want to display all the output but have files of similar type listed together, you can sort the output on the first character of each line:

ls -l | sort -k1,1

The command ls is dealing with file names, which are recorded in the directory data structures. So it does not really care about the file itself, including the "type" of a file.

A command that is more suited to working on actual files, not only it's names, is find. It has an option that directly answers your question on how to filter the list on file type.

This gives a listing of the current directory similar to ls -l:

find . -maxdepth 1 -ls

By default, find lists directories recursively, which is disabled by limiting the search depth to 1.
You can leave out the ., but I included it to show the directories need to be listed before the options.

With -type, you can filter by file type, which is expressed as f or d for plain files or directories:

find . -maxdepth 1 -type d -ls

There are other filter values for -type, notably l for symbolic links.
Note that there's a complication whith symlinks:
There are two types of the file in this case: l, indicating a symlink, and something like f, indicating the type of the file linked to. There are options to specify how to handle that, so you can choose.


From man find:

    -type c
           File is of type c:

           b      block (buffered) special

           c      character (unbuffered) special

           d      directory

           p      named pipe (FIFO)

           f      regular file

           l      symbolic link; this is never true if the  -L  option
                  or  the -follow option is in effect, unless the sym‐
                  bolic link is broken.  If you  want  to  search  for
                  symbolic links when -L is in effect, use -xtype.

           s      socket

           D      door (Solaris)

and relevant to the handling of symbolic links:

    -xtype c
           The  same as -type unless the file is a symbolic link.  For
           symbolic links: if the -H or -P option was specified,  true
           if the file is a link to a file of type c; if the -L option
           has been given, true if c is `l'.  In other words, for sym‐
           bolic  links, -xtype checks the type of the file that -type
           does not check.

and

    -P     Never follow symbolic links.  This is  the  default  behav‐
           iour.  When find examines or prints information a file, and
           the file is a symbolic link, the information used shall  be
           taken from the properties of the symbolic link itself.


    -L     Follow symbolic links.  When find examines or prints infor‐
           mation about files, the information  used  shall  be  taken
           from  the  properties of the file to which the link points,
           not from the link itself (unless it is  a  broken  symbolic
           link  or  find  is  unable to examine the file to which the
           link points).  Use of this option implies -noleaf.  If  you
           later  use  the -P option, -noleaf will still be in effect.
           If -L is in effect and find discovers a symbolic link to  a
           subdirectory during its search, the subdirectory pointed to
           by the symbolic link will be searched.

           When the -L option is in effect, the -type  predicate  will
           always  match  against the type of the file that a symbolic
           link points to rather than the link itself (unless the sym‐
           bolic  link  is  broken).   Using  -L causes the -lname and
           -ilname predicates always to return false.


    -H     Do not follow symbolic links, except while  processing  the
           command  line  arguments. [...]

Tags:

Ls