Using OR patterns in shell wildcards
Solution 1
You don't even need extended globbing enabled to do what you want. This will work in bash:
ls {day*,night*}
Solution 2
There is no option in ls
to filter on filename but in most of the shells there are globbing extension man bash
/Pattern Matching
ksh
ls -lrtd -- *@(day|night)*
zsh
setopt extendedglob
ls -lrtd -- *(day|night)*
or:
setopt kshglob
ls -lrtd -- *@(day|night)*
bash
shopt -s extglob
ls -lrtd -- *@(day|night)*
In any of these three shells you can do this, but note that if one of the cases doesn't match any file, that pattern will be left unexpanded (e.g. *day* night1.txt othernight.txt
if there is no file name containing day
; see man bash
/EXPANSION
or /Brace Expansion
specifically):
ls -lrtd -- *{day,night}*
In any shells you can do:
ls -lrtd -- *day* *night*
In zsh, if there's either no day
or night
file, the last two commands will fail; set the nonomatch
or csh_null_glob
option, or add (N)
after each pattern to avoid this.
Solution 3
Shells do not uses regular expressions for argument expansion.
You can enable the extended pattern matching by
$ shopt -s extglob
and then
$ ls @(day|night).txt
day.txt night.txt
See for example the Bash Reference Manual (Pattern Matching)
Related videos on Youtube
Jethro
Updated on September 18, 2022Comments
-
Jethro over 1 year
I have the following seperation of logic in my application, as you can see I have a class called SimpleController which is what I use to Import and Find SimpleEntities.
I read all the SimpleEntities into memory List cause I search these Entities very often and its a lot faster then reading from the database everytime I want to search for an Entity.
Would it be better to move the logic where I read and store the SimpleEntities into memory into the SimpleLogic class instead of the SimpleController class?
public class SimpleEntity { public int SimpleId { get; set; } public string SimpleName { get; set; } } public class SimpleDAL { public ICollection<SimpleEntity> GetAllSimpleEntities() { //Retrieve SimpleEntities from Database } public void InsertSimpleEntity(SimpleEntity simpleEntity) { //Insert simple Entity into Database } } public class SimpleLogic { private readonly SimpleDAL simpleDAL = new SimpleDAL(); public ICollection<SimpleEntity> GetAllSimpleEntities() { return simpleDAL.GetAllSimpleEntities(); } public void InsertSimpleEntity(SimpleEntity simpleEntity) { //Validate simpleEntity before adding to database if (simpleEntity.SimpleId <= 0) throw new Exception("Invalid SimpleEntity Id: " + simpleEntity.SimpleId); if (String.IsNullOrEmpty(simpleEntity.SimpleName)) throw new Exception("SimpleEntity Name cannot be empty or null"); simpleDAL.InsertSimpleEntity(simpleEntity); } } public class SimpleController { private readonly SimpleLogic simpleLogic = new SimpleLogic(); private List<SimpleEntity> simpleEntities; public SimpleController() { simpleEntities = simpleLogic.GetAllSimpleEntities().ToList(); } public int FindSimpleIndex(int simpleId) { return simpleEntities.FindIndex(p=> p.SimpleId == simpleId); } public void ImportOtherSimpleEntity(OtherSimpleEntity otherSimpleEntity) { if (otherSimpleEntity.Operation == "Update") { int index = FindSimpleIndex(otherSimpleEntity.OtherSimpleId); //If entity was found update SimpleEntity if (index > -1) { //Call SimpleLogic.UpdateSimpleEntity(Pass in SimpleEntity); } } } }
-
Jethro over 13 yearsOk, I agree with the SimpleEntityManager aspect. I also though about moving the Entity Validation Logic into the Entity it'self but then the Logic class doesn't seem to have a real use, it just calls the DAL and seems to be a bit of a waste.
-
Random832 over 11 yearsWhat about
*{day,night}*
. I'd edit it, but I don't know which shells support it? -
itsbruce over 11 yearsI was adapting the original poster's text; I thought that was clear. sigh. Text updated. Surely you can see that the mechanism works?
-
Izkata over 11 years@Random832 Works in
bash
,ksh
, andzsh
(none need extended globbing), but notsh
- however, Nahuel's last "all shells" example does work insh
. -
Admin about 2 yearsUnfortunately, that would also pick up on file entries where e.g. the owning user or group contains
day
ornight
, not only the filename.