How to list only files and not directories of a directory Bash?

前端 未结 9 962
旧巷少年郎
旧巷少年郎 2020-12-07 21:54

How can I list all the files of one folder but not their folders or subfiles. In other words: How can I list only the files?

相关标签:
9条回答
  • 2020-12-07 22:25

    Listing content of some directory, without subdirectories

    I like using ls options, for sample:

    • -l use a long listing format
    • -t sort by modification time, newest first
    • -r reverse order while sorting
    • -F, --classify append indicator (one of */=>@|) to entries
    • -h, --human-readable with -l and -s, print sizes like 1K 234M 2G etc...

    Sometime --color and all others. (See ls --help)

    Listing everything but folders

    This will show files, symlinks, devices, pipe, sockets etc.

    so

    find /some/path -maxdepth 1 ! -type d
    

    could be sorted by date easily:

    find /some/path -maxdepth 1 ! -type d -exec ls -hltrF {} +
    

    Listing files only:

    or

    find /some/path -maxdepth 1 -type f
    

    sorted by size:

    find /some/path -maxdepth 1 -type f -exec ls -lSF --color {} +
    

    Prevent listing of hidden entries:

    To not show hidden entries, where name begin by a dot, you could add ! -name '.*':

    find /some/path -maxdepth 1 ! -type d ! -name '.*' -exec ls -hltrF {} +
    

    Then

    You could replace /some/path by . to list for current directory or .. for parent directory.

    0 讨论(0)
  • 2020-12-07 22:28
    • carlpett's find-based answer (find . -maxdepth 1 -type f) works in principle, but is not quite the same as using ls: you get a potentially unsorted list of filenames all prefixed with ./, and you lose the ability to apply ls's many options;
      also find invariably finds hidden items too, whereas ls' behavior depends on the presence or absence of the -a or -A options.

      • An improvement, suggested by Alex Hall in a comment on the question is to combine shell globbing with find:

            find * -maxdepth 0 -type f  # find -L * ... includes symlinks to files
        
        • However, while this addresses the prefix problem and gives you alphabetically sorted output, you still have neither (inline) control over inclusion of hidden items nor access to ls's many other sorting / output-format options.
    • Hans Roggeman's ls + grep answer is pragmatic, but locks you into using long (-l) output format.


    To address these limitations I wrote the fls (filtering ls) utility,

    • a utility that provides the output flexibility of ls while also providing type-filtering capability,
    • simply by placing type-filtering characters such as f for files, d for directories, and l for symlinks before a list of ls arguments (run fls --help or fls --man to learn more).

    Examples:

    fls f        # list all files in current dir.
    fls d -tA ~  #  list dirs. in home dir., including hidden ones, most recent first
    fls f^l /usr/local/bin/c* # List matches that are files, but not (^) symlinks (l)
    

    Installation

    Supported platforms

    • When installing from the npm registry: Linux and macOS
    • When installing manually: any Unix-like platform with Bash

    From the npm registry

    Note: Even if you don't use Node.js, its package manager, npm, works across platforms and is easy to install; try
    curl -L https://git.io/n-install | bash

    With Node.js installed, install as follows:

    [sudo] npm install fls -g
    

    Note:

    • Whether you need sudo depends on how you installed Node.js / io.js and whether you've changed permissions later; if you get an EACCES error, try again with sudo.

    • The -g ensures global installation and is needed to put fls in your system's $PATH.

    Manual installation

    • Download this bash script as fls.
    • Make it executable with chmod +x fls.
    • Move it or symlink it to a folder in your $PATH, such as /usr/local/bin (macOS) or /usr/bin (Linux).
    0 讨论(0)
  • 2020-12-07 22:30

    find files: ls -l /home | grep "^-" | tr -s ' ' | cut -d ' ' -f 9

    find directories: ls -l /home | grep "^d" | tr -s ' ' | cut -d ' ' -f 9

    find links: ls -l /home | grep "^l" | tr -s ' ' | cut -d ' ' -f 9

    tr -s ' ' turns the output into a space-delimited file the cut command says the delimiter is a space, and return the 9th field (always the filename/directory name/linkname).

    I use this all the time!

    0 讨论(0)
提交回复
热议问题