How do I show all the files in a directory with a name contain a number? I tried

ls [0-9] textfiles

(textfiles) is my directory.

This is how my Linux pocket guide shows me. I am just trying to display all the files that contain a number in the name. I have actually tried about 20+ variations that I found in my book and online.

You probably want something like

ls textfiles/*[0-9]*

or (if "textfiles is my directory" means you're inside the directory):

ls *[0-9]*

Note these commands don't restrict themselves to regular files. Directories, symlinks, named pipes and other entries may match. In broad Unix context all these are "files" and such matching is done with respect to their names only. To tell regular files apart, you need another tool like find.

I'm surprised any guide advised you ls [0-9] textfiles as it makes little sense in the context of your question.

To decode *[0-9]* refer to man 7 glob or e.g. this article, Standard Wildcards (globbing patterns) section. It's worth noticing in the above cases it's the shell (not ls!) that expands the given pattern.

  • Thank you. This worked great. – StPatrick Oct 22 at 20:44
  • @StPatrick Please take our quick tour to learn what you can do with an answer that solves your problem. – Kamil Maciorowski Oct 22 at 20:50

find also works, and can display only files, for example in the current dir (.):

find . -type f -name "*[0-9]*"

It's recursive by default, but use -maxdepth 1 to avoid that.

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.