迷途XO's Blog

confused coding

开始用zsh(1):globbing

迷途XO posted @ 2010年6月15日 07:10 in 未分类 with tags zsh shell linux unix script globbing , 5787 阅读

一直不清楚blog的定位是什么,姑且先作为知识的整理和分享好了。

zsh的交互式实在太诱人,反正还没有熟悉bash,还是直接上手zsh好了。切换shell可以直接用chsh -s /bin/zsh

 

globbing

也就是利用*之类的字符生成多个文件名,除了bsah的模式之外还有一个扩充模式。然后python中也有对应的模块,不知道有什么不同的地方。

基本的模式都很熟悉了:

*.c   *.[co]   *.[^ch]   foo[1-9][0-9]??   mysrc.{c,h}
mv {old,new}.c

在默认设置下,zsh可以在在globbing后面加入qualifiers,比如说:

ls -d *(.) 

表示只生成plain文件的文件名,其中最后的()里面的是zsh的qualifiers,如果是(@)的话就表示只生成软链文件。下面列举出一些qualifiers,更具体的qualifiers可以参见"man zshexpn"。

  1. /:目录
  2. @:软链
  3. .:plain文件
  4. %:设备文件,其中%b是指定block设备,%c是char设备
  5. *:可执行文件(对本用户)
  6. 权限(就是mod)的过滤,具体有:sStxwrAIEXWR,f???
  7. ....

如果在qualifiers前面加上^就是相反的意思。

感觉就是,熟悉了qualifiers后find好像可以退休了,不过很多qualifiers都用不着就是了,需要的时候再查查zshexpn的manpage就可以了。

此外zsh还有一个扩展模式,则需要设置EXTENDEDGLOB变量:

setopt extendedglob

下面列出扩展模式的特性:

1. nagative pattern: "^" 和 "~"

在pattern前加上^,就^表示只生成pattern中^后面不符合的文件名。如:

ls -d *.^c

表示只生成后缀不为c的文件名。

在第一个pattern后面加上~和另外一个pattern,表示生成符合第一个pattern不符合第二个pattern的文件名:

ls pattern1~pattern2

2.数字范围

看看example就好:

% ls run<200-300>
run234  run240
% ls run<300-400>
run303
% ls run<-200>
run123  run2
% ls run<300->
run303
% ls run<>
run123  run2    run234  run240  run303

3.分组:

% ls (foo|bar).*
bar.o  foo.c  foo.o
% ls *.(c|o|pro)
bar.o     file.pro  foo.c     foo.o     main.o    q.c

4.递归搜索子文件夹 **/

ls /path/to/somewhere/**/*.h

几个问题:

1. 扩充失败时会见到:

zsh: no match found: pattern

也就是说找不到适合pattern的文件了。

2. 如果要匹配文件名中的特殊字符,记录要quote,一般用双引号包着就可以。

 

Avatar_small
依云 说:
2011年6月29日 20:31

第一个问题 unsetopt nomatch 即可。

我也没怎么用过 bash。最初看了个 PDF 用了段时间的 fish,后来就转 zsh 了~

Avatar_small
HP Board Model Paper 说:
2022年9月02日 15:12

HP Board Model Paper 2023 Class 4 Pdf Download with Answers for English Medium, Hindi Medium, Urdu Medium & Students for Small Answers, Long Answer, Very Long Answer Questions, and Essay Type Questions to Term1 & Term2 Exams at official website. HP Board Model Paper Class 4 New Exam Scheme or Question Pattern for Sammittive Assignment Exams (SA1 & SA2): Very Long Answer (VLA), Long Answer (LA), Small Answer (SA), Very Small Answer (VSA), Single Answer, Multiple Choice and etc.

Avatar_small
seo service london 说:
2023年11月01日 19:00

This is a great feature for sharing this informative message. I am impressed by the knowledge you have on this blog. It helps me in many ways. Thanks for posting this again

Avatar_small
civaget 说:
2023年12月07日 04:50

Crafting exceptional content is the cornerstone of 구글 상위노출 success. It's a combination of art and science that requires dedication and precision.

Avatar_small
civaget 说:
2023年12月10日 18:37

Anonymity allows me to ask candid questions on 달리머넷. It's a supportive community that encourages curiosity.

Avatar_small
civaget 说:
2023年12月10日 19:08

청주공항렌트카 - the best way to explore Jeju Island.

Avatar_small
civaget 说:
2023年12月10日 22:17

티비몬 offers a great variety of shows and movies, making it a go-to platform for entertainment enthusiasts.

Avatar_small
civaget 说:
2023年12月11日 21:08

오피가이드 makes OP exploration exciting and informative. It's a must-visit for anyone curious about OP services.

Avatar_small
civaget 说:
2023年12月13日 17:52

제주퍼블릭's transparency in pricing is commendable. No hidden fees, just a fantastic night of fun and luxury.

Avatar_small
civaget 说:
2023年12月14日 18:41

오피 offers discreet relaxation services, perfect for unwinding after a long day's work. Highly recommended!

Avatar_small
SEO 说:
2023年12月14日 22:22

안전놀이터 is your gateway to a world of secure gaming excitement.

Avatar_small
civaget 说:
2023年12月15日 22:13

As someone who used to be skeptical, I can confidently say that 힐링 is the real deal. Give it a chance, and you'll see the difference.

Avatar_small
civaget 说:
2023年12月16日 13:24

This internet site is really a walk-through its the knowledge you desired about it and didn’t know who need to. Glimpse here, and you’ll absolutely discover it. NBA중계

Avatar_small
civaget 说:
2023年12月16日 21:47

Choose Tipping if you're serious about quality 설문조사 사이트 추천.

Avatar_small
civaget 说:
2023年12月24日 03:44

A visit to 수원휴게텔 is a journey to relaxation. The skilled therapists and tranquil setting create an ideal haven for unwinding.

Avatar_small
civaget 说:
2023年12月26日 00:43

epl무료중계 is my go-to for fantasy football insights, enhancing my fantasy league experience.

Avatar_small
boardmodelpaper.com 说:
2024年1月19日 01:12

Board Model Papers 2024 provide all states of 6th to 10th text books 2024 Candidates who are Searching for 6th to 10th and 11th to 12th text books and syllabus, sample questions, exam pattern, and Co-Curricular Subject textbooks can refer to this entire article. boardmodelpaper.com and question papers for following the website and Arts, Science, Commerce Stream Subject Wise Solved Question Bank for Hindi & English Medium Students with Exam Pattern & Blueprint and subject Wise with 11th & 12th Question Bank 2024 for General & Vocational Course. Here, we have gathered all subjects of Board textbooks for all Class along with the direct download links.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter