List only regular files (but not directories) in current directory

  • I can use ls -ld */ to list all the directory entries in the current directory. Is there a similarly easy way to just list all the regular files in the current directory? I know I can use find

    find . -maxdepth 1 -type f
    

    or stat

    stat -c "%F %n" * | grep "regular file" | cut -d' ' -f 3-
    

    but these do not strike me as being overly elegant. Is there a nice short way to list only the regular files (I don't care about devices, pipes, etc.) but not the sub-directories of the current directory? Listing symbolic links as well would be a plus, but is not a necessity.

    What do you mean by "overly elegant"? As far as I know, the `find` command is the best way to do what you want. For some reliable other options, you should look into shell specific commands (and those are anything but portable)!

    @rahmu I was for something similar to `ls -d */`, which is short, easy to type, and easy to understand. So I'm pretty happy with Ulrich Dangel's answer, even though I'm not using zsh.

  • With zsh and Glob Qualifiers you can easily express it directly, e.g:

    echo *(.)
    

    will either only return the list of regular files or an error depending on your configuration.

    For the non-directories:

    echo *(^/)
    

    (will include symlinks (including to directories), named pipes, devices, sockets, doors...)

    echo *(-.)
    

    for regular files and symlinks to regular files.

    echo *(-^/)
    

    for non-directories and no symlinks to directories either.

    Also, see the D globbing qualifier if you want to include Dot files (hidden files), like *(D-.).

License under CC-BY-SA with attribution


Content dated before 6/26/2020 9:53 AM