List only regular files (but not directories) in current directory
ls -p | grep -v /
This command lists all non-hidden files that aren't directories (regular files, links, device files, etc.). To also include hidden files, add the -A
option to ls
It assumes none of the files have newline characters in their name. Adding a -q
option to ls
would transform all non-printable characters including newline to ?
, guaranteeing they're on one line and so suitable for feeding to a line-based utility like grep
and for printing on a terminal.
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-.)
.
To list regular files only:
ls -al | grep '^-'
With symbolic links (to any type of file) included:
ls -al | grep '^[-l]'
Where the first character of the list describes the type of file, so -
means that it's a regular file, for symbolic link is l
.
Debian/Ubuntu
Print the names of the all matching files (including links):
run-parts --list --regex . .
With absolute paths:
run-parts --list --regex . "$PWD"
Print the names of all files in /etc
that start with p
and end with d
:
run-parts --list --regex '^p.*d$' /etc