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?
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
)
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 {} +
or
find /some/path -maxdepth 1 -type f
sorted by size:
find /some/path -maxdepth 1 -type f -exec ls -lSF --color {} +
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.
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
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,
ls
while also providing type-filtering capability,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)
Supported platforms
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
.
fls
.chmod +x fls
.$PATH
, such as /usr/local/bin
(macOS) or /usr/bin
(Linux).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!