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 1.0k 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
- 544,893
RomanPerekhrest
- 30,212
-
@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нιη
- 41,407
user2062070
- 9
- 1
-
2also note that this require a shell that support brace-expansion, see Why is brace expansion not supported? – αғ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
zshorbash -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 (
failglobandnullglobnot set), this will give an error fromlsfor the nonmatching glob, e.g. if the existing files areabcde,fghij,lswill listabcdeand 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