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?
Asked
Active
Viewed 9,818 times
0
-
s/dictionary/directory/ ? – steve Oct 12 '17 at 21:08
-
2Welcome to StackExchange. What have you tried so far to answer your coursework ? – steve Oct 12 '17 at 21:08
2 Answers
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.
Stéphane Chazelas
- 522,931
- 91
- 1,010
- 1,501
RomanPerekhrest
- 29,703
- 3
- 43
- 67
-
@Tigger, that should be fixed by the recent edit (which also fixes the problems with filenames starting with `-`) – Stéphane Chazelas Oct 29 '21 at 13:28
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нιη
- 40,939
- 15
- 71
- 114
user2062070
- 9
- 1
-
2also note that this require a shell that support brace-expansion, see [Why is brace expansion not supported?](https://unix.stackexchange.com/q/92819/72456) – αғsнιη Oct 29 '21 at 12:49
-
2Contrary 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
-
1and 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