0

I have 10 files in a dictionary and want to list only files that have an "c" or "z" on position 3 using the ls command. How do I do this?

2 Answers2

4

Simple globbing:

ls -ld -- ??[cz]*

  • ? - matches any single character

  • [cz] - matches one character given in the brackets (character class)

Note that hidden files will not be included.

0

Use globbing, each question mark represents a character. Therefore, the answer below indicates that you want anything listed that contains c or z character as a third occurrence.

ls ??{c*,z*}

or:

ls ??{c,z}*
αғsнιη
  • 41,407
  • 2
    also note that this require a shell that support brace-expansion, see Why is brace expansion not supported? – αғsнιη Oct 29 '21 at 12:49
  • 2
    Contrary to Roman's approach, that also means expanding too independent globs so is less efficient and in zsh or bash -O failglob, that would abort the command if either of the two globs has no match. – Stéphane Chazelas Oct 29 '21 at 13:31
  • 1
    and in Bash with default settings (failglob and nullglob not set), this will give an error from ls for the nonmatching glob, e.g. if the existing files are abcde, fghij, ls will list abcde and give an error for ??z*. There's really no reason to use braces to single-character alternatives, when brackets already work (like in the other answer). Even if the alternatives did consist of more than one character, ksh-style extended globs would be better (e.g. ??@(c|foo)*). – ilkkachu Oct 29 '21 at 13:46