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?
Using find:
find . -maxdepth 1 -type f
Using the -maxdepth 1 option ensures that you only look in the current directory (or, if you replace the . with some path, that directory). If you want a full recursive listing of all files in that and subdirectories, just remove that option.
ls -p | grep -v /
ls -p lets you show / after the folder name, which acts as a tag for you to remove.
carlpett's
find-based answer (find . -maxdepth 1 -type f) works in principle, but is not quite the same as usingls: you get a potentially unsorted list of filenames all prefixed with./, and you lose the ability to applyls's many options;
alsofindinvariably finds hidden items too, whereasls' behavior depends on the presence or absence of the-aor-Aoptions.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.
- 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
Hans Roggeman's
ls+grepanswer 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
lswhile also providing type-filtering capability, - simply by placing type-filtering characters such as
ffor files,dfor directories, andlfor symlinks before a list oflsarguments (runfls --helporfls --manto 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; trycurl -L https://git.io/n-install | bash
With Node.js installed, install as follows:
[sudo] npm install fls -g
Note:
Whether you need
sudodepends on how you installed Node.js / io.js and whether you've changed permissions later; if you get anEACCESerror, try again withsudo.The
-gensures global installation and is needed to putflsin your system's$PATH.
Manual installation
- Download this
bashscript asfls. - 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).
You can also use ls with grep or egrep and put it in your profile as an alias:
ls -l | egrep -v '^d'
ls -l | grep -v '^d'
{ find . -maxdepth 1 -type f | xargs ls -1t | less; }
added xargs to make it works, and used -1 instead of -l to show only filenames without additional ls info
With regex:
ls -al | egrep -v "^d.*"
Just adding on to carlpett's answer. For a much useful view of the files, you could pipe the output to ls.
find . -maxdepth 1 -type f|ls -lt|less
Shows the most recently modified files in a list format, quite useful when you have downloaded a lot of files, and want to see a non-cluttered version of the recent ones.
"find '-maxdepth' " does not work with my old version of bash, therefore I use:
for f in $(ls) ; do if [ -f $f ] ; then echo $f ; fi ; done
来源:https://stackoverflow.com/questions/10574794/how-to-list-only-files-and-not-directories-of-a-directory-bash