From the Linux CLI, using the bash shell, how to list files (using the ls command) in a directory where the filenames desired all begin with a character P but the rest of the filename is then followed by 8 lower-case characters? Tried the following but it does not work.
ls -las P[a-z]{8}
An example directory would have files like so:
Pasdfghjk
Pqwertyui
Pabcd
Pads
where only the first two files are desired to be listed/returned to STDOUT. If the ls command cannot do this then what command or command sequence can?
Bash globs don't have a finite repetition operator ({8}
), even with extglob
enabled.
So you can pipe ls
through grep
:
ls -ls | grep -E 'P[a-z]{8}$'
(That's not perfect because it will match filenames which end with the pattern. But that might not be important in your case.)
Or you could expand the glob manually:
ls -ls P[a-z][a-z][a-z][a-z][a-z][a-z][a-z][a-z]