linux - How to list only ordinary files on UNIX

07
2014-07
  • lzap

    I'd like to list only ordinary files (not directories or symlinks) using ls from GNU tools. So far I have only some hackish greps. Is there an option or better way of doing that?

    Edit: I need more coffee: find -type d

    Thanks

  • Answers
  • shekhar

    Have you tried with find type -f. I mean

    find /path/of/yourlocation type -f
    
  • mpy

    As you didn't specify your shell, here is a solution using the Z shell. Using zsh it is very easy to restrict any command to only plain files:

    zsh% ls *(.)
    zsh% tail *(.)
    

    This uses the so called Glob Qualifier (.) to restricts the expansion of * to only plain files. See man zshexpn for much more qualifiers and explanations. Using Glob Qualifier I nearly never need monstrous find calls anymore.


  • Related Question

    linux - Unix - List all directories and subdirectories, excluding directories without files
  • ftiaronsem

    I would like to list all the directories and sub directories in and below the current path. Since I only wanted to display directories I came up with the follwing command:

    find -type d -exec ls -d1 {} \; | cut -c 3-
    

    This prints out for example

    webphone
    music
    finance
    finance/banking
    finance/realestate
    finance/trading
    finance/other
    finance/moneylending
    finance/insurance
    webradio
    webtv
    

    The problem I have right now is, that the directory finance is listed. finance contains no files yust the sub directories you see above. What I want to achieve is the following output:

    webphone
    music
    finance/banking
    finance/realestate
    finance/trading
    finance/other
    finance/moneylending
    finance/insurance
    webradio
    webtv
    

    In this list the directory finance is not listed. Therefore I need your adive of how to filter directories which contain no files (only subdirectories).

    Thanks in advance

    ftiaronsem


  • Related Answers
  • Gilles

    Here's one way: list all regular files, strip away the file basenames, and remove duplicates.

    find . -type f | sed 's!/[^/]*$!!' | sort -u
    

    If you want to strip the leading ./:

    find . -type f | sed -e 's!/[^/]*$!!' -e 's!^\./!!' | sort -u
    
  • udo

    I consider installing tree:

    • sudo apt-get install tree

    and then run

    • tree -d /path/to/start/dir

    to display directories only.

    Example:

    root@X100e:~# tree -d /var/cache/
    /var/cache/
    ├── apache2
    │   └── mod_disk_cache
    ├── apt
    │   └── archives
    │       └── partial
    ├── binfmts
    ├── cups
    │   └── rss
    ├── debconf
    ├── dictionaries-common
    ├── flashplugin-installer
    ...