Yeah, I too know it's half-a-year old now, but...
I have tried ls /etc *[0-9]* but that doesn't work.
Well, no wonder. Putting a space
between the patterns makes the shell treat it as two separate arguments, so ls
will list the entire contents of /etc
, plus any files in the current directory with digits in their names. What you need to use is a single
glob pattern that targets the directory you want.
This works just fine for me:
to only display the current directory, preventing recursive listing.
But since this is really a shell expansion function rather than a feature of ls
, you could even just use echo
, if you want them on individual lines).
printf "%s\n" /etc/*[0-9]*
In a script you can similarly use the globs to set an array or run in a for
loop or whatever.
As for the second question, I don't know why anyone ever
want to try to use grep
when the appropriate answer is just to use find
, which was expressly made for just such a purpose.
Any other technique would require first using ls
or another tool that prints timestamps in its output, then parsing that output for the timestamp and filename. Then, since grep can't do direct comparisons, use some kind of function (shell or awk, perhaps) to compare it to a reference timestamp and print out the filenames corresponding to the appropriate stamps. Why bother?