TL;DR
To find files matching a regular expression, use find with the -regex
option:
find [startingPath] -type [fileType] -regex "[regularExpression]"
In your case, if you want to search for files (file type f
) ending in .png
, starting from the current directory:
find . -type f -regex ".*\.pdf"
If you want to have an ls
-like output, use the -ls
action:
find . -type f -regex ".*\.pdf" -ls
(the output has the same format as ls -dils
).
If you want to execute a command for each file, use the -exec
action, e.g.:
find . -type f -regex ".*\.pdf" -exec file {} \;
... will print file type information for each matching file.
There are a lot more things you can do with find, just read the manual.
As @steeldriver said in the comment, there is no regular expression in your command. *.png
is a shell glob and is expanded before ls
is run. Imagine there are two files in the current directory:
picture1.png
picture2.png
...then ls -R *.png
will be expanded to:
ls -R picture1.png picture2.png
In this case, the -R
option is not particularly useful because there are no directories specified that ls
could recurse into.
If the shell doesn't find any matching name, it passes the argument literally (depends on the shell, but bash does):
ls -R *.png
... and ls
complains because there is no file called *.png
.
.png
in the current directory and all its subdirectories, usefind . -name '*.png'
rather than trying to filter the output ofls -R
– steeldriver Jan 18 at 13:25