简体   繁体   中英

How to find ls only files with uppercases in name?

I want to filter only files and folders with name start with 2[AZ] (uppercase only):

ls -recurse | Where-Object {$_.name -match '2[A-Z]'}

But it still returns lowercase files. Reading the -match parameter of Where-Object I don't see a way to fix this.

PowerShell's comparison operators are case- insensitive by default, but they have c -prefixed variants that are case- sensitive , namely -cmatch in the case of the -match operator :

gci -recurse | Where-Object { $_.Name -cmatch '^2[A-Z]' }
  • gci is used as the PowerShell-idiomatic alternative to the Windows-only ls alias for Get-ChildItem .

    • See the bottom section of this answer for more information on why aliases named for commands or utilities of a different shell or platform are best avoided.
  • Regex assertion ^ is used to ensure that the pattern only matches at the start of file names.


Alternatively, use simplified syntax :

gci -recurse | Where-Object Name -cmatch '^2[A-Z]'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM