1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
\r
2 "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
\r
3 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
\r
5 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
\r
6 <meta name="generator" content="AsciiDoc 8.2.2" />
\r
7 <style type="text/css">
\r
9 p, li, dt, dd, div, pre, h1, h2, h3, h4, h5, h6 {
\r
11 border: 1px solid red;
\r
16 margin: 1em 5% 1em 5%;
\r
21 text-decoration: underline;
\r
39 h1, h2, h3, h4, h5, h6 {
\r
41 font-family: sans-serif;
\r
43 margin-bottom: 0.5em;
\r
48 border-bottom: 2px solid silver;
\r
51 border-bottom: 2px solid silver;
\r
61 border: 1px solid silver;
\r
66 margin-bottom: 0.5em;
\r
76 font-family: sans-serif;
\r
83 font-family: sans-serif;
\r
87 font-family: sans-serif;
\r
89 border-top: 2px solid silver;
\r
95 padding-bottom: 0.5em;
\r
99 padding-bottom: 0.5em;
\r
103 div.tableblock, div.imageblock, div.exampleblock, div.verseblock,
\r
104 div.quoteblock, div.literalblock, div.listingblock, div.sidebarblock,
\r
105 div.admonitionblock {
\r
108 margin-bottom: 1.5em;
\r
110 div.admonitionblock {
\r
112 margin-bottom: 2.5em;
\r
115 div.content { /* Block element content. */
\r
119 /* Block element titles. */
\r
120 div.title, caption.title {
\r
121 font-family: sans-serif;
\r
125 margin-bottom: 0.5em;
\r
131 td div.title:first-child {
\r
134 div.content div.title:first-child {
\r
137 div.content + div.title {
\r
141 div.sidebarblock > div.content {
\r
142 background: #ffffee;
\r
143 border: 1px solid silver;
\r
150 div.listingblock > div.content {
\r
151 border: 1px solid silver;
\r
152 background: #f4f4f4;
\r
156 div.quoteblock > div.content {
\r
157 padding-left: 2.0em;
\r
163 div.verseblock + div.attribution {
\r
167 div.admonitionblock .icon {
\r
168 vertical-align: top;
\r
171 text-decoration: underline;
\r
173 padding-right: 0.5em;
\r
175 div.admonitionblock td.content {
\r
176 padding-left: 0.5em;
\r
177 border-left: 2px solid silver;
\r
180 div.exampleblock > div.content {
\r
181 border-left: 2px solid silver;
\r
185 div.verseblock div.content {
\r
189 div.imageblock div.content { padding-left: 0; }
\r
190 div.imageblock img { border: 1px solid silver; }
\r
191 span.image img { border-style: none; }
\r
195 margin-bottom: 0.8em;
\r
200 font-style: italic;
\r
202 dd > *:first-child {
\r
207 list-style-position: outside;
\r
210 list-style-type: lower-alpha;
\r
213 div.tableblock > table {
\r
214 border: 3px solid #527bbd;
\r
217 font-family: sans-serif;
\r
226 margin-bottom: 0.8em;
\r
229 padding-bottom: 5px;
\r
232 vertical-align: top;
\r
233 font-style: italic;
\r
234 padding-right: 0.8em;
\r
237 vertical-align: top;
\r
241 div#footer-badges { display: none; }
\r
246 font-family: sans-serif;
\r
250 margin-bottom: 0.1em;
\r
253 div.toclevel1, div.toclevel2, div.toclevel3, div.toclevel4 {
\r
269 /* Workarounds for IE6's broken and incomplete CSS2. */
\r
271 div.sidebar-content {
\r
272 background: #ffffee;
\r
273 border: 1px solid silver;
\r
276 div.sidebar-title, div.image-title {
\r
277 font-family: sans-serif;
\r
280 margin-bottom: 0.5em;
\r
283 div.listingblock div.content {
\r
284 border: 1px solid silver;
\r
285 background: #f4f4f4;
\r
289 div.quoteblock-content {
\r
290 padding-left: 2.0em;
\r
293 div.exampleblock-content {
\r
294 border-left: 2px solid silver;
\r
295 padding-left: 0.5em;
\r
298 /* IE6 sets dynamically generated links as visited. */
\r
299 div#toc a:visited { color: blue; }
\r
301 <title>ZSH-LOVERS(1)</title>
\r
305 <h1>ZSH-LOVERS(1)</h1>
\r
308 <div class="sectionbody">
\r
309 <p>zsh-lovers - tips, tricks and examples for the Z shell</p>
\r
312 <div class="sectionbody">
\r
313 <p>Just read it. ;-)</p>
\r
316 <div class="sectionbody">
\r
317 <p>Whenever we look at the zsh manual we wonder why there are no examples or those
\r
318 simply things in (shell) life. The zsh contains many features, but there was no
\r
319 manpage with some examples (like procmailex(5)). That's why we wrote this
\r
321 <p>Most of the tricks and oneliner come from the mailinglists zsh-users,
\r
322 zsh-workers, google, newsgroups and from ourself. See section <strong>LINKS</strong> for
\r
324 <p>Note: This manpage (zsh-lovers(1)) is <strong>not</strong> an offical part of the Z shell! It's
\r
325 just a just for fun - manpage ;)<br />
\r
326 For comments, bugreports and feedback take a quick look at the section <strong>BUGS</strong>.</p>
\r
328 <h2>SHELL-SCRIPTING</h2>
\r
329 <div class="sectionbody">
\r
330 <p>This section provides some examples for often needed shellscript-stuff. Notice
\r
331 that you should not use otherwise most examples won't work.<br />
\r
332 Parse options in shellscripts. Example taken from ZWS by Adam Chodorowski
\r
333 (<a href="http://www.chodorowski.com/projects/zws/">http://www.chodorowski.com/projects/zws/</a>):</p>
\r
334 <div class="listingblock">
\r
335 <div class="content">
\r
336 <pre><tt>parse_options()
\r
342 zparseopts -K -- p:=o_port r:=o_root l:=o_log h=o_help
\r
343 if [[ $? != 0 || "$o_help" != "" ]]; then
\r
344 echo Usage: $(basename "$0") "[-p PORT] [-r DIRECTORY]"
\r
352 if [[ $root[1] != '/' ]]; then root="$PWD/$root"; fi
\r
354 # now use the function:
\r
355 parse_options $*</tt></pre>
\r
359 <div class="sectionbody">
\r
360 <p>Available subsections are <strong>Aliases</strong>, <strong>Completion</strong>, <strong>Unsorted/Misc examples</strong>,
\r
361 <strong>(Recursive) Globbing - Examples</strong>, <strong>Modifiers usage</strong>, <strong>Redirection-Examples</strong>,
\r
362 <strong>ZMV-Examples</strong> and <strong>Module-Examples</strong>.</p>
\r
364 <p>Suffix aliases are supported in zsh since version 4.2.0. Some examples:</p>
\r
365 <div class="listingblock">
\r
366 <div class="content">
\r
367 <pre><tt>alias -s tex=vim
\r
369 alias -s org=w3m</tt></pre>
\r
371 <p>Now pressing return-key after entering <em>foobar.tex</em> starts vim with
\r
372 foobar.tex. Calling a html-file runs browser w3m. <em>www.zsh.org</em> and pressing
\r
373 enter starts w3m with argument www.zsh.org.<br />
\r
374 Global aliases can be used anywhere in the command line. Example:</p>
\r
375 <div class="listingblock">
\r
376 <div class="content">
\r
377 <pre><tt>$ alias -g C='| wc -l'
\r
378 $ grep alias ~/.zsh/* C
\r
381 <p>Some more or less useful global aliases (choose whether they are useful or not
\r
382 for you on your own):</p>
\r
383 <div class="listingblock">
\r
384 <div class="content">
\r
385 <pre><tt>alias -g ...='../..'
\r
386 alias -g ....='../../..'
\r
387 alias -g .....='../../../..'
\r
388 alias -g CA="2>&1 | cat -A"
\r
389 alias -g C='| wc -l'
\r
390 alias -g D="DISPLAY=:0.0"
\r
391 alias -g DN=/dev/null
\r
392 alias -g ED="export DISPLAY=:0.0"
\r
393 alias -g EG='|& egrep'
\r
394 alias -g EH='|& head'
\r
395 alias -g EL='|& less'
\r
396 alias -g ELS='|& less -S'
\r
397 alias -g ETL='|& tail -20'
\r
398 alias -g ET='|& tail'
\r
399 alias -g F=' | fmt -'
\r
400 alias -g G='| egrep'
\r
401 alias -g H='| head'
\r
402 alias -g HL='|& head -20'
\r
403 alias -g Sk="*~(*.bz2|*.gz|*.tgz|*.zip|*.z)"
\r
404 alias -g LL="2>&1 | less"
\r
405 alias -g L="| less"
\r
406 alias -g LS='| less -S'
\r
407 alias -g MM='| most'
\r
408 alias -g M='| more'
\r
409 alias -g NE="2> /dev/null"
\r
410 alias -g NS='| sort -n'
\r
411 alias -g NUL="> /dev/null 2>&1"
\r
413 alias -g R=' > /c/aaa/tee.txt '
\r
414 alias -g RNS='| sort -nr'
\r
415 alias -g S='| sort'
\r
416 alias -g TL='| tail -20'
\r
417 alias -g T='| tail'
\r
418 alias -g US='| sort -u'
\r
419 alias -g VM=/var/log/messages
\r
420 alias -g X0G='| xargs -0 egrep'
\r
421 alias -g X0='| xargs -0'
\r
422 alias -g XG='| xargs egrep'
\r
423 alias -g X='| xargs'</tt></pre>
\r
425 <h3>COMPLETION</h3>
\r
426 <p>See also man 1 zshcompctl zshcompsys zshcompwid. zshcompctl is the old
\r
427 style of zsh programmable completion, zshcompsys is the new completion
\r
428 system, zshcompwid are the zsh completion widgets.</p>
\r
429 <p>Some functions, like _apt and _dpkg, are very slow. You can use a cache
\r
430 in order to proxy the list of results (like the list of available
\r
431 debian packages) Use a cache:</p>
\r
432 <div class="listingblock">
\r
433 <div class="content">
\r
434 <pre><tt>zstyle ':completion:*' use-cache on
\r
435 zstyle ':completion:*' cache-path ~/.zsh/cache</tt></pre>
\r
437 <p>Prevent CVS files/directories from being completed:</p>
\r
438 <div class="listingblock">
\r
439 <div class="content">
\r
440 <pre><tt>zstyle ':completion:*:(all-|)files' ignored-patterns '(|*/)CVS'
\r
441 zstyle ':completion:*:cd:*' ignored-patterns '(*/)#CVS'</tt></pre>
\r
443 <p>Fuzzy matching of completions for when you mistype them:</p>
\r
444 <div class="listingblock">
\r
445 <div class="content">
\r
446 <pre><tt>zstyle ':completion:*' completer _complete _match _approximate
\r
447 zstyle ':completion:*:match:*' original only
\r
448 zstyle ':completion:*:approximate:*' max-errors 1 numeric</tt></pre>
\r
450 <p>And if you want the number of errors allowed by _approximate to
\r
451 increase with the length of what you have typed so far:</p>
\r
452 <div class="listingblock">
\r
453 <div class="content">
\r
454 <pre><tt>zstyle -e ':completion:*:approximate:*' \
\r
455 max-errors 'reply=($((($#PREFIX+$#SUFFIX)/3))numeric)'</tt></pre>
\r
457 <p>Ignore completion functions for commands you don't have:</p>
\r
458 <div class="listingblock">
\r
459 <div class="content">
\r
460 <pre><tt>zstyle ':completion:*:functions' ignored-patterns '_*'</tt></pre>
\r
462 <p>With helper functions like:</p>
\r
463 <div class="listingblock">
\r
464 <div class="content">
\r
465 <pre><tt>xdvi() { command xdvi ${*:-*.dvi(om[1])} }</tt></pre>
\r
467 <p>you can avoid having to complete at all in many cases, but if you do,
\r
468 you might want to fall into menu selection immediately and to have the
\r
469 words sorted by time:</p>
\r
470 <div class="listingblock">
\r
471 <div class="content">
\r
472 <pre><tt>zstyle ':completion:*:*:xdvi:*' menu yes select
\r
473 zstyle ':completion:*:*:xdvi:*' file-sort time</tt></pre>
\r
475 <p>Completing process IDs with menu selection:</p>
\r
476 <div class="listingblock">
\r
477 <div class="content">
\r
478 <pre><tt>zstyle ':completion:*:*:kill:*' menu yes select
\r
479 zstyle ':completion:*:kill:*' force-list always</tt></pre>
\r
481 <p>If you end up using a directory as argument, this will remove the
\r
482 trailing slash (usefull in ln)</p>
\r
483 <div class="listingblock">
\r
484 <div class="content">
\r
485 <pre><tt>zstyle ':completion:*' squeeze-slashes true</tt></pre>
\r
487 <p>cd will never select the parent directory (e.g.: cd ../<TAB>):</p>
\r
488 <div class="listingblock">
\r
489 <div class="content">
\r
490 <pre><tt>zstyle ':completion:*:cd:*' ignore-parents parent pwd</tt></pre>
\r
492 <p>Another method for <em>quick change directories</em>. Add this to your ~/.zshrc, then just enter
\r
493 “cd …./dir”</p>
\r
494 <div class="listingblock">
\r
495 <div class="content">
\r
496 <pre><tt>rationalise-dot() {
\r
497 if [[ $LBUFFER = *.. ]]; then
\r
503 zle -N rationalise-dot
\r
504 bindkey . rationalise-dot</tt></pre>
\r
506 <h3>UNSORTED/MISC examples</h3>
\r
507 <p>Hint: A list of valid glob Qualifiers can be found in zshexpn(1).
\r
508 See “man 1 zshexpn | less -p” Qualifiers for details.</p>
\r
509 <div class="listingblock">
\r
510 <div class="content">
\r
511 <pre><tt># random numbers
\r
512 $ echo $[${RANDOM}%1000] # random between 0-999
\r
513 $ echo $[${RANDOM}%11+10] # random between 10-20
\r
514 $ echo ${(l:3::0:)${RANDOM}} # N digits long (3 digits)
\r
517 $ echo "${(j::)${(@Oa)${(s::):-hello}}}"
\r
519 # Show newest directory
\r
522 # random array element
\r
523 $ FILES=( .../files/* )
\r
524 $ feh $FILES[$RANDOM%$#FILES+1]
\r
526 # cat first line in all files in this dir
\r
527 $ for file (*(ND-.)) IFS= read -re < $file
\r
529 # test if a parameter is numeric
\r
530 $ if [[ $1 == <-> ]] ; then
\r
536 # Show me all the .c files for which there doesn't exist a .o file.
\r
537 $ print *.c(e_'[[ ! -e $REPLY:r.o ]]'_)
\r
539 # All files in /var/ that are not owned by root
\r
540 $ ls -ld /var/*(^u:root)
\r
542 # All files for which the owner hat read and execute permissions
\r
545 # The same, but also others dont have execute permissions
\r
546 $ echo *(f:u+rx,o-x:)
\r
548 # brace expansion - example
\r
551 $ print -r -- $^X.$^Y
\r
552 A.+ A.- B.+ B.- C.+ C.-
\r
554 # Fetch the newest file containing the string 'fgractg*.log' in the
\r
555 # filename and contains the string 'ORA-' in it
\r
556 $ file=(fgractg*.log(Nm0om[1]))
\r
557 $ (($#file)) && grep -l ORA- $file
\r
559 $ files=$( find . -name . -o -prune -name 'fgractg*>log' -mtime 0 -print )
\r
560 > if [ -n "$files" ]; then
\r
564 > file=$(ls -td $files | head -1)
\r
565 > grep -l ORA- "$file"
\r
568 # keep specified number of child processes running until entire task finished
\r
569 $ zsh -c 'sleep 1 & sleep 3 & sleep 2& print -rl -- $jobtexts'
\r
571 # Remove zero length and .bak files in a directory
\r
572 $ rm -i *(.L0) *.bak(.)
\r
574 # print out files that dont have extensions
\r
575 $ printf '%s\n' ^?*.*
\r
576 $ printf '%s\n' ^?*.[^.]*(D)
\r
577 $ ls -d -- ^?*.*(D)
\r
579 # Finding files which does not contain a specific string
\r
580 $ print -rl file* | comm -2 -3 - <(grep -l string file*)'
\r
581 $ for f (file*(N)) grep -q string $f || print -r $f'
\r
583 # Show/Check whether a option is set or not. It works both with $options as
\r
585 $ echo $options[correct]
\r
590 # Count the number of directories on the stack
\r
591 $ print $((${${(z)${(f)"$(dirs -v)"}[-1]}[1]} + 1)) # or
\r
592 $ dirs -v | awk '{n=$1}END{print n+1}'
\r
594 # Matching all files which do not have a dot in filename
\r
597 # Show only the ip-address from ``ifconfig device''
\r
598 # ifconfig from net-tools (Linux)
\r
599 $ print ${${$(LC_ALL=C /sbin/ifconfig eth0)[7]}:gs/addr://}
\r
600 # ifconfig from 4.2BSD {Free,Net,Open}BSD
\r
601 $ print ${$(/sbin/ifconfig tun0)[6]}
\r
603 # Ping all the IP addresses in a couple of class C's or all hosts
\r
605 $ for i in {1..254}; do ping -c 1 192.168.13.$i; done
\r
608 $ while ( [[ $I -le 255 ]] ) ; do ping -1 2 150.150.150.$I; let I++; done
\r
610 $ for i in $(sed 's/#.*//' > /etc/hosts | awk '{print $2}')
\r
612 : echo "Trying $i ... "
\r
614 : echo '============================='
\r
617 # load all available modules at startup
\r
620 $ for md ($module_path) m=($m $md/**/*(*e:'REPLY=${REPLY#$md/}'::r))
\r
623 # Rename all files within a directory such that their names get a numeral
\r
624 # prefix in the default sort order.
\r
625 $ i=1; for j in *; do mv $j $i.$j; ((i++)); done
\r
626 $ i=1; for f in *; do mv $f $(echo $i | \
\r
627 awk '{ printf("%03d", $0)}').$f; ((i++)); done
\r
628 $ integer i=0; for f in *; do mv $f $[i+=1].$f; done
\r
630 # Find (and print) all symbolic links without a target within the current
\r
632 $ $ file **/*(D@) | fgrep broken
\r
633 $ for i in **/*(D@); [[ -f $i || -d $i ]] || echo $i
\r
634 $ echo **/*(@-^./=%p)
\r
635 $ print -l **/*(-@)
\r
637 # List all plain files that do not have extensions listed in `fignore'
\r
638 $ ls **/*~*(${~${(j/|/)fignore}})(.)
\r
639 # see above, but now omit executables
\r
640 $ ls **/*~*(${~${(j/|/)fignore}})(.^*)
\r
642 # Print out files that dont have extensions (require *setopt extendedglob*
\r
643 # and *setopt dotglob*)
\r
644 $ printf '%s\n' ^?*.*
\r
646 # List files in reverse order sorted by name
\r
647 $ print -rl -- *(On)
\r
649 $ print -rl -- *(^on)
\r
651 # Synonymic to ``ps ax | awk '{print $1}'''
\r
652 $ print -l /proc/*/cwd(:h:t:s/self//)
\r
654 # Get the PID of a process (without ``ps'', ``sed'', ``pgrep'', ..
\r
658 > for i in /proc/<->/stat
\r
660 > [[ "$(< $i)" = *\((${(j:|:)~@})\)* ]] && echo $i:h:t
\r
664 # for X in 'n' 'o' 'p' 'q' 'r' 's' 't' 'u' 'v' 'w' 'x' 'y'; do ...
\r
665 $ for (( i = 36#n; i <= 36#y; i++ )); do
\r
666 > print ${$(([##36]i)):l}
\r
668 # or in combination with ``dc''
\r
669 $ print {$((##n))..$((##y))}P\ 10P | dc
\r
671 $ eval print '${$(([##36]'{$((36#n))..$((36#y))}')):l}'
\r
673 # foreach in one line of shell
\r
674 $ for f (*) print -r -- $f
\r
676 # copy a directory recursively without data/files
\r
679 $ mkdir -p -- $dirs
\r
681 $ find . -type d -exec env d="$dest_root" \
\r
682 sh -c ' exec mkdir -p -- "$d/$1"' '{}' '{}' \;
\r
684 # If `foo=23'', then print with 10 digit with leading '0'.
\r
686 $ print ${(r:10::0:)foo}
\r
688 # find the name of all the files in their home directory that have
\r
689 # more than 20 characters in their file names
\r
690 print -rl $HOME/${(l:20::?:)~:-}*
\r
693 $ print -r -- ${(qq)m} > $nameoffile # save it
\r
694 $ eval "m=($(cat -- $nameoffile)" # or use
\r
695 $ m=("${(@Q)${(z)"$(cat -- $nameoffile)"}}") # to restore it
\r
697 # get a "ls -l" on all the files in the tree that are younger than a
\r
698 # specified age (e.g "ls -l" all the files in the tree that where
\r
699 # modified in the last 2 days)
\r
700 $ ls -tld **/*(m-2)
\r
701 # This will give you a listing 1 file perl line (not à la ls -R).
\r
702 # Think of an easy way to have a "ls -R" style output with
\r
703 # only files newer than 2 day old.
\r
704 $ for d (. ./**/*(/)) {
\r
705 > print -r -- $'\n'${d}:
\r
706 > cd $d && {
\r
708 > (($#l)) && ls -ltd -- $l
\r
712 # If you also want directories to be included even if their mtime
\r
713 # is more than 2 days old:
\r
714 $ for d (. ./**/*(/)) {
\r
715 > print -r -- $'\n'${d}:
\r
716 > cd $d && {
\r
718 > (($#l)) && ls -ltd -- $l
\r
722 # And if you want only the directories with mtime < 2 days to be listed:
\r
723 $ for d (. ./**/*(N/m-2)) {
\r
724 > print -r -- $'\n'${d}:
\r
725 > cd $d && {
\r
727 > (($#l)) && ls -ltd -- $l
\r
733 $ echo ${(l:42::-:)}
\r
734 # or use ``$COLUMS''
\r
735 $ echo ${(l:$COLUMNS::-:)}
\r
736 # and now with colors (require autoload colors ;colors)
\r
737 $ echo "$bg[red]$fg[black]${(l:42::-:)}"
\r
739 # Redirect STDERR to a command like xless without redirecting STDOUT as well.
\r
740 $ foo 2>>(xless)
\r
741 # but this executes the command asynchronously. To do it synchronously:
\r
742 $ { { foo 1>&3 } 2>&1 | xless } 3>&1
\r
744 # Rename all MP3-Files from name with spaces.mp3 to Name With Spaces.mp3
\r
745 $ for i in *.mp3; do
\r
746 > mv $i ${${(C)i}:s/Mp3/mp3/}
\r
749 # Match file names containing only digits and ending with .xml (require
\r
750 # *setopt kshglob*)
\r
751 $ ls -l [0-9]##.xml
\r
752 $ ls -l <0->.xml
\r
754 # Remove all "non txt" files
\r
757 # Move 200 files from a directory into another
\r
758 $ mv -- *([1,200]) /another/Dir
\r
760 # Convert images (foo.gif => foo.png):
\r
761 $ for i in **/*.gif; convert $i $i:r.png
\r
763 # convert a collection of mp3 files to wave or cdr,
\r
764 # e.g. file.wav -> file.mp3)
\r
765 $ for i (./*.mp3){mpg321 --w - $i > ${i:r}.wav}
\r
767 # Download with LaTeX2HTML created Files (for example the ZSH-Guide):
\r
768 $ for f in http://zsh.sunsite.dk/Guide/zshguide{,{01..08}}.html; do
\r
769 > lynx -source $f >${f:t}
\r
772 # Move all files in dir1 and dir2 that have line counts greater than 10 to
\r
773 # another directory say "/more10"
\r
774 $ mv dir[12]/**/*.cr(-.e{'((`wc -l < $REPLY` > 10))'}) /more10
\r
776 # Make with dpkg a master-list of everyfile that it has installed
\r
777 $ diff <(find / | sort) <(cat /var/lib/dpkg/info/*.list | sort)
\r
779 # Replace this fucking Escape-Sequences:
\r
780 $ autoload colors ; colors
\r
781 $ print "$bg[cyan]$fg[blue]You are a idiot" >> /dev/pts/3
\r
783 # Get ASCII value of a character
\r
784 $ char=N ; print $((#char))
\r
786 # Filename "Erweiterung"
\r
787 # Note: The (N) says to use the nullglob option for this particular
\r
789 $ for i in *.o(N); do
\r
793 # Rename files; i. e. FOO to foo and bar to BAR
\r
794 $ for i in *(.); mv $i ${i:l} # `FOO' to `foo'
\r
795 $ for i in *(.); mv $i ${i:u} # `bar to `BAR'
\r
797 # Show all suid-files in $PATH
\r
798 $ ls -latg ${(s.:.)PATH} | grep '^...s'
\r
799 # or more complex ;)
\r
800 $ print -l ${^path}/*(Ns,S)
\r
801 # or show only executables with a user given pattern
\r
802 $ print -l ${^path}/*vim*(*N)
\r
804 # gzip files when containing a certain string
\r
805 $ gzip ${(ps:\0:)"$(grep -lZ foobar ./*.txt(.))"}
\r
807 # A small one-liner, that reads from stdin and prints to stdout the first
\r
808 # unique line i. e. does not print lines that have been printed before
\r
809 # (this is similar to the unique command, but unique can only handle
\r
811 $ IFS=$'\n\n'; print -rl -- ${(Oau)${(Oa)$(cat file;echo .)[1,-2]}}
\r
813 # Lists every executable in PATH
\r
814 $ print -l ${^path}/*(-*N)
\r
816 # Match all .c files in all subdirectories, _except_ any SCCS subdirectories?
\r
817 $ ls **/*.c~(*/)#SCCS/*
\r
819 # List all `README' - files case-insensitive with max. one typo
\r
820 $ ls **/*(#ia2)readme
\r
822 # case insensitive checking for variables
\r
823 $ if [[ $OSTYPE == (#i)LINUX*(#I) ]]; then
\r
824 > echo "Penguin on board."
\r
826 > echo "Not a Linux."
\r
829 <h3>(Recursive) Globbing - Examples</h3>
\r
830 <p>A list of valid glob Qualifiers can be found in zshexpn(1). <strong>Note:</strong>
\r
831 **/ is equivalent to (*/)#! For example:</p>
\r
832 <div class="listingblock">
\r
833 <div class="content">
\r
834 <pre><tt>$ print (*/)#zsh_us.ps
\r
835 zsh-4.2.3/Doc/zsh_us.ps
\r
836 $ print **/zsh_us.ps
\r
837 zsh-4.2.3/Doc/zsh_us.ps</tt></pre>
\r
839 <div class="listingblock">
\r
840 <div class="content">
\r
841 <pre><tt># Search for `README' in all Subdirectories
\r
844 # find directories that contain both "index.php" and "index.html", or in
\r
845 # general, directories that contain more than one file matching "index.*"
\r
846 $ ls **/*(D/e:'[[ -e $REPLY/index.php && -e $REPLY/index.html ]]':)
\r
848 $ ls **/*(D/e:'l=($REPLY/index.*(N)); (( $#l >= 2 ))':)
\r
850 # Find command to search for directory name instead of basename
\r
851 $ print -rl /**/*~^*/path(|/*)
\r
853 $ find / | grep -e /path/ -e '/path$'
\r
855 # Print he path of the directories holding the ten biggest C regular files
\r
856 # in the current directory and subdirectories.
\r
857 $ print -rl -- **/*.c(D.OL[1,10]:h) | sort -u
\r
859 # Find files with size == 0 and send a mail
\r
860 $ files=(**/*(ND.L0m+0m-2))
\r
861 > (( $#files > 0 )) && print -rl -- $files | \
\r
862 mailx -s "empty files" foo@bar.tdl
\r
865 $ chmod 700 **/(.) # Only files
\r
866 $ chmod 700 **/(/) # Only directories
\r
868 # print out all of the files in that directory in 2 columns
\r
869 $ print -rC2 -- ${1:[...]}/*(D:t)
\r
870 # ^- number ob columns
\r
871 # or - if you feel concerned about special characters - use
\r
872 $ list=(${1:[...]}/*(ND:t))
\r
873 $ (($#list)) && print -rC2 -- ${(V)list}
\r
875 # Search all files in /home/*/*-mail/ with a setting ``chmod -s'' flag
\r
876 # (recursive, include dotfiles) remove the setgid/setuid flag and print
\r
878 $ chmod -s /home/*/*-mail(DNs,S) /home/*/*-mail/**/*(DNs,S))
\r
879 # or with a small script
\r
880 $ for file (/home/*/*-mail(DNs,S) /home/*/*-mail/**/*(DNs,S)) {
\r
881 > print -r -- $file
\r
882 > chmod -s $file && print -r fixed $file
\r
884 # or use ``zargs'' (require autoload zargs) prevent the arg list too
\r
886 $ zargs /home/*/*-mail(DNs,S) /home/*/*-mail/**/*(DNs,S)) -- chmod -s
\r
888 # List files beginning at `foo23' upwards (foo23, foo24, foo25, ..)
\r
889 $ ls -l foo<23->
\r
891 # get all files that begin with the date strings from June 4 through
\r
893 $ ls -l 200406{04..10}*(N)
\r
894 # or if they are of the form 200406XX (require ``setopt extended_glob''
\r
895 $ ls -l 200306<4-10>.*
\r
897 # remove spaces from filenames
\r
898 $ for a in ./**/*\ *(Dod); do mv $a ${a:h}/${a:t:gs/ /_}; done
\r
900 # Show only all *.c and *.h - Files
\r
903 # Show only all *.c - files and ignore `foo.c'
\r
906 # show data to *really* binary format
\r
907 $ zsh -ec 'while {} {printf %.8x $n;repeat 8 \
\r
908 > {read -ku0 a printf \ %.8d $(([##2]#a))};print;((n+=8))}' < binary
\r
910 # Show only world-readable files
\r
913 # List files in the current directory are not writable by the owner
\r
914 $ print -l ~/*(ND.^w)
\r
916 # find and delete the files which are older than a given parameter
\r
917 # (seconds/minutes/hours)
\r
918 # deletes all regular file in /Dir that are older than 3 hours
\r
919 $ rm -f /Dir/**/*(.mh+3)
\r
920 # deletes all symlinks in /Dir that are older than 3 minutes
\r
921 $ rm -f /Dir/**/*(@mm+3)
\r
922 # deletes all non dirs in /Dir that are older than 30 seconds
\r
923 $ rm -f /Dir/**/*(ms+30^/)
\r
924 # deletes all folders, sub-folders and files older than one hour
\r
925 $ rm ./**/*(.Dmh+1,.DL0)
\r
926 # deletes all files more than 6 hours old
\r
928 # removes all files but the ten newer ones (delete all but last 10
\r
929 # files in a directory)
\r
930 $ rm ./*(Om[1,-11])
\r
931 Note: If you get a arg list too long, you use the builtin rm. For
\r
933 $ zmodload zsh/files ; rm -f **/*(mh+6)
\r
934 or use the zargs function:
\r
935 $ autoload zargs ; zargs **/*(mh+6) -- rm -f
\r
937 # A User's Guide to the Z-Shell /5.9: Filename Generation and Pattern
\r
938 # Matching find all files in all subdirectories, searching recursively,
\r
939 # which have a given name, case insensitive, are at least 50 KB large,
\r
940 # no more than a week old and owned by the root user, and allowing up
\r
941 # to a single error in the spelling of the name. In fact, the required
\r
942 # expression looks like this:
\r
943 $ ls **/(#ia1)name(LK+50mw-1u0)
\r
945 # Change the UID from 102 to 666
\r
946 $ chown 666 **/*(u102)
\r
948 # List all files which have not been updated since last 10 hours
\r
949 $ print -rl -- *(Dmh+10^/)
\r
951 # delete only the oldest file in a directory
\r
952 $ rm ./*filename*(Om[1])
\r
954 # Sort the output from `ls -l' by file size
\r
957 # find most recent file in a directory
\r
958 $ setopt dotglob ; print directory/**/*(om[1])
\r
960 # Show only empty files which nor `group' or `world writable'
\r
963 # find - and list - the ten newest files in directories and subdirs
\r
965 $ print -rl -- **/*(Dom[1,10])
\r
967 # Print only 5 lines by "ls" command (like ``ls -laS | head -n 5'')
\r
968 $ ls -fl *(DOL[1,5])
\r
970 # display the 5-10 last modified files
\r
971 $ print -rl -- /path/to/dir/**/*(D.om[5,10])
\r
973 # find all files without a valid owner
\r
974 $ chmod someuser /**/*(D^u:${(j.:u:.)${(f)"$(</etc/passwd)"}%%:*}:)
\r
976 # find all the empty directories in a tree
\r
977 $ for f in ***/*(/l2); do foo=($f/*(N)); [[ -z $foo ]] && print $f; done
\r
978 # Note:Since Zsh 4.2.1 the glob qualifier F indicates a non-empty directory.
\r
979 # Hence *(F) indicates all subdirectories with entries, *(/^F) means all
\r
980 # subdirectories with no entries.
\r
983 # remove empty directories afterwards
\r
984 $ rmdir ./**/*(/od) 2> /dev/null
\r
986 # Show only files are owned from group `users'
\r
987 $ ls -l *(G[users])
\r
990 <h3>Modifiers usage</h3>
\r
991 <p>Modifiers are a powerful mechanism that let you modify the results
\r
992 returned by parameter, filename and history expansion. See zshexpn(1)
\r
994 <div class="listingblock">
\r
995 <div class="content">
\r
996 <pre><tt><<<<<<< /home/dope/download/Source/HG-Repos/zsh-lovers/zsh-lovers.1.txt.orig.432616858
\r
997 # NOTE: Zsh 4.3.4 needed!
\r
999 # files modified today
\r
1000 $ print *(e:age today now:)
\r
1001 # files modified since 5 pm
\r
1002 $ print *(e-age 17:00 now-)
\r
1003 # ... since 5 o'clock yesterda
\r
1004 $ print *(e-age yesterday,17:00 now-)
\r
1005 # ... from last Christmas before today
\r
1006 $ print *(e-age 2006/12/25 today-)
\r
1007 # ... before yesterday
\r
1008 $ print *(e-age 1970/01/01 yesterday-)
\r
1009 # all files modified between the start of those dates
\r
1010 $ print *(e:age 2006/10/04 2006/10/09:)
\r
1011 # all files modified on that date
\r
1012 $ print *(e:age 2006/10/04:)
\r
1014 $ print *(e-age 2006/10/04:10:15 2006/10/04:10:45-)
\r
1016 # Remove a trailing pathname component, leaving the head. This works like
\r
1017 ||||||| /tmp/zsh-lovers.1.txt~base.AvW_ZW
\r
1018 # Remove a trailing pathname component, leaving the head. This works like
\r
1020 # Remove a trailing pathname component, leaving the head. This works like
\r
1021 >>>>>>> /tmp/zsh-lovers.1.txt~other.vDaddL
\r
1026 # Remove all leading pathname components, leaving the tail. This works
\r
1027 # like `basename'.
\r
1031 # Remove the suffix from each file (*.sh in this example)
\r
1032 $f:e is $f file extension
\r
1033 :h --> head (dirname)
\r
1034 :t --> tail (basename)
\r
1035 :r --> rest (extension removed)
\r
1036 $ for f (*.sh) mv $f $f:r
\r
1038 # Remove a filename extension of the form `.xxx', leaving the root name.
\r
1044 # Remove all but the extension.
\r
1051 # Print the new command but do not execute it. Only works with history
\r
1058 # Quote the substituted words, escaping further substitutions.
\r
1065 # Convert the words to all lowercase.
\r
1072 # Convert the words to all uppercase.
\r
1079 # convert 1st char of a word to uppercase
\r
1080 $ foo="one two three four"
\r
1081 $ print -r -- "${(C)foo}"
\r
1082 One Two Three Four</tt></pre>
\r
1084 <h3>Redirection-Examples</h3>
\r
1085 <p>See zshmisc(1) for more informations (or less ${^fpath}/zmv(N))</p>
\r
1086 <div class="listingblock">
\r
1087 <div class="content">
\r
1088 <pre><tt># Append `exit 1' at the end of all *.sh - files
\r
1089 $ echo "exit 1" >> *.sh
\r
1091 # adding files to foobar.tar.gz
\r
1092 $ eval set =(gunzip < foobar.tar.gz) '
\r
1093 tar rf $1 additional.txt &&gzip < $1 > foobar.tar.gz'
\r
1095 # Redirect output to a file AND display on screen
\r
1096 $ foobar >&1 > file1 > file2 > ..
\r
1098 # pipe single output to multiple inputs
\r
1099 $ zcat foobar.Z >> (gzip -9 > file1.gz) \
\r
1100 >> (bzip2 -9 > file1.bz2) \
\r
1101 >> (acb --best > file1.acb)
\r
1103 # Append /etc/services at the end of file `foo' and `bar'
\r
1104 $ cat /etc/services >> foo >> bar
\r
1107 $ echo An error >&2 2>&1 | sed -e 's/A/I/'
\r
1109 # send standard output of one process to standard input of several processes
\r
1112 $ process1 > >(process1) > >(process2)
\r
1114 # initializing a variable and simultaneously keeping terminal output
\r
1116 $ { a=$(command >&1 >& 3 3 > &- 2>&1);} 3>&1
\r
1118 # redirect stderr two times
\r
1119 $ setopt multios ; program 2> file2 > file1 2>&1
\r
1121 # Duplicating stdout and stderr to a logfile
\r
1122 $ exec 3>&1 > logfile 2>&2 2>&1 >&3 3>&-
\r
1124 # redirect stderr (only) to a file and to orig. stderr:
\r
1125 $ command 2>&2 2>stderr
\r
1126 # redirect stderr and stdout to separate files and both to orig. stdout:
\r
1127 $ command 2>&1 1>&1 2>stderr 1>stdout
\r
1128 # redirect stderr and stdout to separate files and stdout to orig. stdout
\r
1129 # AND stderr to orig. stderr:
\r
1130 $ command 2>&2 1>&1 2>stderr 1>stdout
\r
1132 # More fun with STDERR ;)
\r
1133 $ ./my-script.sh 2> >(grep -v moron >error.log)|process-output >output.log
\r
1134 $ echo "Thats STDOUT" >>(sed 's/stdout/another example/' > foobar)</tt></pre>
\r
1136 <h3>ZMV-Examples (require autoload zmv)</h3>
\r
1137 <p><strong>Note:</strong> <em>-n</em> means no execution (just print what would happen). At</p>
\r
1138 <div class="listingblock">
\r
1139 <div class="content">
\r
1140 <pre><tt># remove round bracket within filenames
\r
1141 # i. e. foo-(bar).avi -> foo-bar.avi
\r
1142 $ zmv '*' '${f//[()]/}'
\r
1144 # serially all files (foo.foo > 1.foo, fnord.foo > 2.foo, ..)
\r
1147 1.c asd.foo bla.foo fnord.foo foo.fnord foo.foo
\r
1148 $ c=1 zmv '*.foo' '$((c++)).foo'
\r
1150 1.c 1.foo 2.foo 3.foo 4.foo foo.fnord
\r
1152 # Rename "file.with.many.dots.txt" by substituting dots (exept for the last
\r
1153 # one!) with a space
\r
1154 $ touch {1..20}-file.with.many.dots.txt
\r
1155 $ zmv '(*.*)(.*)' '${1//./ }$2'
\r
1157 # Remove the first 4 chars from a filename
\r
1158 $ zmv -n '*' '$f[5,-1]' # NOTE: The "5" is NOT a mistake in writing!
\r
1160 # Rename names of all files under the current Dir to lower case, but keep
\r
1162 $ zmv -Qv '(**/)(*)(.D)' '$1${(L)2}'
\r
1164 # replace all 4th character, which is "1", with "2" and so on
\r
1166 $ zmv '(???)1(???[1-4].txt)' '${1}2${2}'
\r
1168 # Remove the first 15 characters from a string
\r
1169 $ touch 111111111111111{a-z}
\r
1171 $ zmv '*' '$f[16,-1]'
\r
1173 # Replace spaces (any number of them) with a single dash in file names
\r
1175 $ zmv -n '(**/)(* *)' '$1${2//( #-## #| ##)/-}'
\r
1177 $ find . -depth -name '* *' -exec bash -c '
\r
1178 > shopt -s extglob
\r
1180 > dir=${file%/*}
\r
1181 > name=${file##*/}
\r
1182 > newname=${name//*([ -]) *([ -])/-}
\r
1183 > mv -i -- "$file" "$Dir/$newname"' {} {} \;
\r
1185 # Clean up file names and remove special characters
\r
1187 $ zmv -n '(**/)(*)' '$1${2//[^A-Za-z0-9._]/_}'
\r
1189 # Add *.py to a bunch of python scripts in a directory (some of them end
\r
1190 # in *.py and give them all a proper extension
\r
1192 $ zmv -n '(**/)(con*)(#qe,file $REPLY | grep "python script",)' '$1$2.py'
\r
1194 # lowercase all extensions (i. e. *.JPG) incl. subfolders
\r
1196 $ zmv '(**/)(*).(#i)jpg' '$1$2.jpg'
\r
1197 # Or - without Zsh
\r
1198 $ find Dir -name '*.[jJ][pP][gG]' -print | while read f
\r
1202 > *) mv "$f" "${f%.*}.jpg" ;
\r
1206 # remove leading zeros from file extension
\r
1209 filename.001 filename.003 filename.005 filename.007 filename.009
\r
1210 filename.002 filename.004 filename.006 filename.008 filename.010
\r
1211 $ zmv '(filename.)0##(?*)' '$1$2'
\r
1213 filename.1 filename.10 filename.2 filename.3 filename.4 filename.5 ..
\r
1218 foo_10.jpg foo_2.jpg foo_3.jpg foo_4.jpg foo_5.jpg foo_6.jpg ..
\r
1219 $ zmv -fQ 'foo_(<0->).jpg(.nOn)' 'foo_$(($1 + 1)).jpg'
\r
1221 foo_10.jpg foo_11.jpg foo_3.jpg foo_4.jpg foo_5.jpg ...
\r
1223 # adding leading zeros to a filename (1.jpg -> 001.jpg, ..
\r
1225 $ zmv '(<1->).jpg' '${(l:3::0:)1}.jpg'
\r
1227 # See above, but now only files with a filename >= 30 chars
\r
1229 $ c=1 zmv "${(l:30-4::?:)}*.foo" '$((c++)).foo'
\r
1231 # Replace spaces in filenames with a underline
\r
1233 $ zmv '* *' '$f:gs/ /_'
\r
1235 # Change the suffix from *.sh to *.pl
\r
1237 $ zmv -W '*.sh' '*.pl'
\r
1239 # Add a "".txt" extension to all the files within ${HOME}
\r
1240 # ``-.'' is to only rename regular files or symlinks to regular files,
\r
1241 # ``D'' is to also rename hidden files (dotfiles))
\r
1243 $ zmv -Q '/home/**/*(D-.)' '$f.txt'
\r
1244 # Or to only rename files that don't have an extension:
\r
1245 $ zmv -Q '/home/**/^?*.*(D-.)' '$f.txt'
\r
1247 # Recursively change filenames with characters ? [ ] / = + < > ; : " , - *
\r
1249 $ chars='[][?=+<>;",*-]'
\r
1250 $ zmv '(**/)(*)' '$1${2//$~chars/%}'
\r
1252 # Removing single quote from filenames (recursively)
\r
1254 $ zmv -Q "(**/)(*'*)(D)" "\$1\${2//'/}"
\r
1256 # When a new file arrives (named file.txt) rename all files in order to
\r
1257 # get (e. g. file119.txt becomes file120.txt, file118.txt becomes
\r
1258 # file119.txt and so on ending with file.txt becoming file1.txt
\r
1260 $ zmv -fQ 'file([0-9]##).txt(On)' 'file$(($1 + 1)).txt'
\r
1262 # lowercase/uppercase all files/directories
\r
1264 $ zmv '(*)' '${(L)1}' # lowercase
\r
1265 $ zmv '(*)' '${(U)1}' # uppercase
\r
1267 # Remove the suffix *.c from all C-Files
\r
1269 $ zmv '(*).c' '$1'
\r
1271 # Uppercase only the first letter of all *.mp3 - files
\r
1273 $ zmv '([a-z])(*).mp3' '${(C)1}$2.mp3'
\r
1275 # Copy the target `README' in same directory as each `Makefile'
\r
1277 $ zmv -C '(**/)Makefile' '${1}README'
\r
1279 # Removing single quote from filenames (recursively)
\r
1281 $ zmv -Q "(**/)(*'*)(D)" "\$1\${2//'/}"
\r
1283 # Rename pic1.jpg, pic2.jpg, .. to pic0001.jpg, pic0002.jpg, ..
\r
1285 $ zmv 'pic(*).jpg' 'pic${(l:4::0:)1}.jpg'
\r
1286 $ zmv '(**/)pic(*).jpg' '$1/pic${(l:4::0:)2}.jpg' # recursively</tt></pre>
\r
1288 <h3>Module-Examples</h3>
\r
1289 <p>Please read zshmodules(1) first!</p>
\r
1290 <h4>zsh/pcre (require zmodload zsh/pcre)</h4>
\r
1291 <div class="listingblock">
\r
1292 <div class="content">
\r
1293 <pre><tt># Copy files of a certain period (date indicated in the filenames)
\r
1294 $ zmodload zsh/pcre
\r
1295 $ ls -d -- *(e:'[[ $REPLY -pcre-match pcre-regexp ]]':)
\r
1297 $ m() { [[ $1 -pcre-match pcre-regexp ]] }
\r
1298 $ ls -d -- *(+m)</tt></pre>
\r
1300 <h4>zsh/clone (require zmodload zsh/clone)</h4>
\r
1301 <div class="listingblock">
\r
1302 <div class="content">
\r
1303 <pre><tt># Creates a forked instance of the current shell ($! is set to zero) and
\r
1304 # execute ``command'' on /dev/tty8 (for this example).
\r
1305 $ zmodload zsh/clone
\r
1306 $ clone /dev/tty8 && (($! == 0)) && exec command</tt></pre>
\r
1308 <h4>zsh/datetime (require zmodload zsh/datetime)</h4>
\r
1309 <div class="listingblock">
\r
1310 <div class="content">
\r
1311 <pre><tt> $ zmodload zsh/datetime
\r
1312 $ alias datereplacement='strftime "%Y-%m-%d" $EPOCHSECONDS'
\r
1313 $ export DATE=`datereplacement`
\r
1316 # strip date from filename
\r
1317 $ $ zmodload zsh/datetime
\r
1318 $ setopt extendedglob
\r
1319 $ touch aaa_bbb_20041212_c.dat eee_fff_20051019_g.dat
\r
1320 $ strftime -s pattern \
\r
1321 '???_???_<0-%Y%m%d>_?.dat' $((EPOCHSECONDS - 365 * 24 * 60 * 60 / 2))
\r
1322 $ print -rl -- $~pattern
\r
1323 aaa_bbb_20041212_c.dat
\r
1324 $ print -rl -- $pattern
\r
1325 ???_???_<0-20050815>_?.dat
\r
1327 # Search files size == 0, to be based on the file name containing a date
\r
1328 # rather than the "last modified" date of the file
\r
1329 $ zmodload -i zsh/datetime
\r
1330 $ strftime -s file "abc_de_%m%d%Y.dat" $((EPOCHSECONDS - 24 * 60 * 60 ))
\r
1331 $ files=(**/$file(N.L0))
\r
1332 $ (( $#files > 0 )) && print -rl -- $files | \
\r
1333 mailx -s "empty files" foo@bar.tdl</tt></pre>
\r
1335 <h4>zsh/stat (require zmodload zsh/stat)</h4>
\r
1336 <div class="listingblock">
\r
1337 <div class="content">
\r
1338 <pre><tt># test if a symbolic link links to a certain file
\r
1339 $ zmodload -i zsh/stat
\r
1340 $ ! stat -LH s foo.ln || [[ $s[link] != "foo.exe" ]] || ln -sf foo.exe foo.ln
\r
1342 # comparing file dates
\r
1343 $ zmodload zsh/stat
\r
1346 $ touch bar & sleep 5 & touch foo
\r
1347 $ echo $file1 is $(($(stat +mtime $file2) - \
\r
1348 $(stat +mtime $file1))) seconds older than $file2.
\r
1349 bar is 5 seconds older than foo
\r
1351 # list the files of a disk smaller than some other file
\r
1352 $ zmodload zsh/stat
\r
1353 $ stat -A max +size some-other-file
\r
1354 $ print -rl ./**/*(D.L-$max)
\r
1356 # List the top 100 biggest files in a disk
\r
1357 $ zmodload zsh/stat
\r
1358 $ ls -fld ./**/*(d`stat +device .`OL[1,100])
\r
1360 # Get only the user name and the file names from (like
\r
1361 # ls -l * | awk '{print $3" " $8}')
\r
1362 $ zmodload zsh/stat
\r
1364 > stat -sA user +uid -- "$file" &&
\r
1365 > print -r -- "$user" "$file"
\r
1368 # get the difference between actual bytes of file and allocated bytes of file
\r
1369 $ zmodload zsh/stat
\r
1370 $ print $(($(stat +block -- file) * 512 - $(stat +size -- file)))
\r
1372 # Find largest file
\r
1373 # ``D'' : to include dot files (d lowercase is for device)
\r
1374 # ``O'' : reverse Ordered (o lowercase for non-reverse order)
\r
1375 # ``L'' : by file Length (l is for number of links)
\r
1376 # ``[1]'': return only first one
\r
1377 $ zmodload zsh/stat
\r
1378 $ stat +size ./*(DOL[1])
\r
1380 # file size in bytes
\r
1381 $ zmodload zsh/stat
\r
1382 $ stat -L +size ~/.zshrc
\r
1385 # Delete files in a directory that hasn't been accessed in the last ten days
\r
1386 # and send ONE mail to the owner of the files informing him/her of the files'
\r
1388 $ zmodload zsh/stat zsh/files
\r
1389 $ typeset -A f; f=()
\r
1390 $ rm -f /path/**/*(.a+10e{'stat -sA u +uidr $REPLY; f[$u]="$f[$u]$REPLY"'})
\r
1391 $ for user (${(k)f}) {print -rn $f[$user]|mailx -s "..." $user}
\r
1393 # Get a "ls -l" on all the files in the tree that are younger than a
\r
1395 $ zmodload zsh/stat
\r
1396 $ for d (. ./**/*(N/m-2))
\r
1397 > print -r -- $'\n'$d: && cd $d && {
\r
1398 > for f (*(Nm-2om))
\r
1399 > stat -F '%b %d %H:%M' -LsAs -- $f &&
\r
1400 > print -r -- $s[3] ${(l:4:)s[4]} ${(l:8:)s[5]} \
\r
1401 > ${(l:8:)s[6]} ${(l:8:)s[8]} $s[10] $f ${s[14]:+-> $s[14]}
\r
1405 # get file creation date
\r
1406 $ zmodload zsh/stat
\r
1407 $ stat -F '%d %m %Y' +mtime ~/.zshrc
\r
1409 $ stat -F '%D' +mtime ~/.zshrc
\r
1410 06/30/04</tt></pre>
\r
1412 <h4>zsh/files (require zmodload zsh/files)</h4>
\r
1413 <div class="listingblock">
\r
1414 <div class="content">
\r
1415 <pre><tt># search a directory for files containing a certain string then copy those
\r
1416 # files to another directory.
\r
1417 $ zmodload zsh/files
\r
1419 $ cp $(grep -lZr foobar .) otherdirectory</tt></pre>
\r
1421 <h4>zsh/mapfile (require zmodload zsh/mapfile)</h4>
\r
1422 <div class="listingblock">
\r
1423 <div class="content">
\r
1424 <pre><tt># grepping for two patterns
\r
1425 $ zmodload zsh/mapfile
\r
1427 $ pattern2="bar foo"
\r
1428 $ print -l ./**/*(DN.e{'z=$mapfile[$REPLY] && [[ $z = *$pattern1* && \
\r
1429 $z = *$pattern2* ]]'})
\r
1430 # or a solution in combination with zsh/pcre
\r
1431 $ zmodload -i zsh/mapfile zsh/pcre
\r
1433 $ pattern2="bar foo"
\r
1434 $ pcre_compile "(?s)(?=.*?$pattern1).*?$pattern2"
\r
1436 $ print -l ./**/*(DN.e{'pcre_match $mapfile[$REPLY]'})
\r
1438 # equivalent for ``less /etc/passwd | grep -v root''
\r
1439 $ zmodload zsh/mapfile
\r
1441 $ print -rl -- ${${=mapfile[/etc/passwd]}:#*root*}
\r
1442 # or - for case insensitive
\r
1443 $ setopt extendedglob
\r
1444 $ print -rl -- ${${=mapfile[/etc/passwd]}:#*(#i)root*}
\r
1446 # If a XML-file contains stuff like ``<TAGA/>'' and ``<TAGB/>'', number
\r
1447 # this empty tags (ones ending in '/>') so if encountered in the same
\r
1448 # order, the preceeding tags would become ``<TAGA/>1</TAGA>'' and
\r
1449 # ``<TAGB/>2</TAGB>''
\r
1450 $ zmodload zsh/mapfile
\r
1452 $ apfile[data.xml.new]=${(S)mapfile[data.xml]//\
\r
1453 > (#im)<TAGA>*<\/TAGA>/<TAGA>$((++cnt))<\/TAGA>}
\r
1455 # removing all files in users Maildir/new that contain ``filename="gone.src''
\r
1456 $ zmodload zsh/{files,mapfile}
\r
1457 $ rm -f /u1/??/*/Maildir/new/100*(.e{'[[ $mapfile[$REPLY] == \
\r
1458 *filename=\"gone.scr\"* ]]'})
\r
1460 # Grep out the Title from a postscript file and append that value to the
\r
1461 # end of the filename
\r
1463 $ zmodload zsh/mapfile
\r
1464 $ zmv '(*).ps' '$1-${${${mapfile[$f]##*%%Title: }%% *}//[^a-zA-Z0-9_]/}.ps'</tt></pre>
\r
1466 <h4>zsh/mathfunc (require zmodload zsh/mathfunc)</h4>
\r
1467 <div class="listingblock">
\r
1468 <div class="content">
\r
1469 <pre><tt>$ zmodload zsh/mathfunc
\r
1470 $ echo $(( sin(1/4.0)**2 + cos(1/4.0)**2 - 1 ))
\r
1471 -1.1102230246251565e-16
\r
1472 $ echo $(( pi = 4.0 * atan(1.0) ))
\r
1473 3.1415926535897931
\r
1474 $ echo $(( f = sin(0.3) ))
\r
1475 0.29552020666133955
\r
1476 $ print $((1e12 * rand48()))
\r
1477 847909677310.23413
\r
1478 $ print $(( rand48(seed) ))
\r
1479 0.01043488334700271</tt></pre>
\r
1481 <h4>zsh/termcap (require zmodload zsh/termcap)</h4>
\r
1482 <div class="listingblock">
\r
1483 <div class="content">
\r
1484 <pre><tt> $ zmodload -ab zsh/termcap echotc
\r
1485 $ GREEN=`echotc AF 2`
\r
1486 $ YELLOW=`echotc AF 3`
\r
1487 $ RED=`echotc AF 1`
\r
1488 $ BRIGHTRED=`echotc md ; echotc AF 1`
\r
1489 $ print -l ${GREEN}green ${YELLOW}yellow ${RED}red ${BRIGHTRED}brightred</tt></pre>
\r
1491 <h4>zsh/zpty (require zmodload zsh/zpty)</h4>
\r
1492 <div class="listingblock">
\r
1493 <div class="content">
\r
1494 <pre><tt> $ zmodload zsh/zpty
\r
1495 $ zpty PW passwd $1
\r
1496 $ zpty PW passwd $1
\r
1497 # ``-r'': read the output of the command name.
\r
1498 # ``z'' : Parameter
\r
1499 $ zpty -r PW z '*password:'
\r
1500 # send the to command name the given strings as input
\r
1502 $ zpty -r PW z '*password:'
\r
1504 # The second form, with the -d option, is used to delete commands
\r
1505 # previously started, by supplying a list of their names. If no names
\r
1506 # are given, all commands are deleted. Deleting a command causes the HUP
\r
1507 # signal to be sent to the corresponding process.
\r
1508 $ zpty -d PW</tt></pre>
\r
1510 <h4>zsh/net/socket (require zmodload zsh/net/socket)</h4>
\r
1511 <div class="listingblock">
\r
1512 <div class="content">
\r
1513 <pre><tt># ``-l'': open a socket listening on filename
\r
1514 # ``-d'': argument will be taken as the target file descriptor for the
\r
1516 # ``3'' : file descriptor. See ``A User's Guide to the Z-Shell''
\r
1517 # (3.7.2: File descriptors)
\r
1518 $ zmodload zsh/net/socket
\r
1520 # ``-a'': accept an incoming connection to the socket
\r
1521 $ zsocket -a -d 4 3
\r
1522 $ zsocket -a -d 5 3 # accept a connection
\r
1523 $ echo foobar >&4
\r
1524 $ echo barfoo >&5
\r
1525 $ 4>&- 5>&- 3>&</tt></pre>
\r
1527 <h4>zsh/zftp (require zmodload zsh/zftp)</h4>
\r
1528 <div class="listingblock">
\r
1529 <div class="content">
\r
1530 <pre><tt> $ autoload -U zfinit
\r
1532 $ zfparams www.example.invalid myuserid mypassword
\r
1535 $ zfls -l zshtips.html
\r
1536 $ zfput zshtips.html
\r
1537 $ zfls -l zshtips.html
\r
1539 # Automatically transfer files using FTP with error checking
\r
1540 $ autoload -U zfinit ; zfinit
\r
1541 $ zftp open host.name.invalid user passwd || exit
\r
1542 $ zftp get /remote/file > /local/file; r=$?
\r
1543 $ zftp close && exit r
\r
1545 # compress and ftp on the fly
\r
1546 $ autoload -U zfinit ; zfinit
\r
1547 $ zftp open host.name.invalid user password
\r
1548 $ zftp get $file | bzip2 > ${file}.bz2
\r
1551 # Recursice ``get''
\r
1552 $ autoload -U zfinit ; zfinit
\r
1554 $ zfcd daemontools
\r
1555 $ for file in `zfls` ; do
\r
1560 # Upload all regular files in $HOME/foobar (recursive) that are newer than
\r
1561 # two hours to ftp.foobar.invalid/path/to/upload
\r
1562 $ autoload -U zfinit ; zfinit
\r
1563 $ zfopen ftp.foobar.invalid/path/to/upload
\r
1565 $ zfput -r **/*(.mh-2)
\r
1568 # long list of files on a ftp
\r
1569 $ autoload -U zfinit ; zfinit
\r
1570 $ zfopen some-host
\r
1571 $ zfcd /some/remote/Dir
\r
1572 $ cd /some/local/Dir
\r
1573 # If the list.txt is located on the remote host, change to
\r
1574 # zfget ${(f)"$(zftp get /path/to/remote/list.txt)"}
\r
1575 $ zfget ${(f)"$(cat list.txt)"}
\r
1576 $ zfclose</tt></pre>
\r
1578 <h4>zsh/zselect (require zmodload zsh/zselect)</h4>
\r
1579 <div class="listingblock">
\r
1580 <div class="content">
\r
1581 <pre><tt># It's similar to
\r
1584 | $ stty -icanon min 0 time 50
\r
1587 | $ case "$yesno" in
\r
1588 | > yes) command1;;
\r
1589 | > *) command2;;
\r
1592 $ zmodload zsh/zselect
\r
1593 $ if zselect -t 500 -r 0 && read yesno && [ yes = "$yesno" ]; then
\r
1597 > fi</tt></pre>
\r
1601 <div class="sectionbody">
\r
1602 <h3>Navigation options</h3>
\r
1603 <p><strong>auto_cd</strong> (allow one to change to a directory by entering it as a
\r
1604 command). <strong>auto_pushd</strong> (automatically append dirs to the push/pop list)
\r
1605 pushd_ignore_dups (and don't duplicate them).</p>
\r
1607 <p><strong>no_hup</strong> (don't send HUP signal to background jobs when exiting ZSH).
\r
1608 <strong>print_exit_value</strong> (show a message with the exit code when a command
\r
1609 returns with a non-zero exit code)</p>
\r
1610 <h4>History options</h4>
\r
1611 <p><strong>hist_verify</strong> (let the user edit the command line after history
\r
1612 expansion (e.g. !ls) instead of immediately running it) <tt>
\r
1613 Use the same history file for all sessions : </tt>
\r
1614 <strong>setopt SHARE_HISTORY</strong></p>
\r
1615 <h4>Privacy / Security</h4>
\r
1616 <p><strong>no_clobber</strong> (or set -C; prevent <em>></em> redirection from truncating
\r
1617 the given file if it already exists)</p>
\r
1618 <h4>Spelling correction</h4>
\r
1619 <p><strong>correct</strong> (automatically correct the spelling of commands).
\r
1620 <strong>correct_all</strong> (automatically correct the spelling of each word on the
\r
1621 command line) <strong>dvorak</strong> (dvorak layout)</p>
\r
1623 <h2>UNSORTED/MISC</h2>
\r
1624 <div class="sectionbody">
\r
1625 <p>Mailpath: simple multiple mailpath:</p>
\r
1626 <div class="listingblock">
\r
1627 <div class="content">
\r
1628 <pre><tt>mailpath=($HOME/Mail/mbox'?new mail in mbox'
\r
1629 $HOME/Mail/tux.u-strasbg'?new mail in tux'
\r
1630 $HOME/Mail/lilo'?new mail in lilo'
\r
1631 $HOME/Mail/ldap-fr'?new mail in ldap-fr')</tt></pre>
\r
1633 <p>Mailpath: dynamic mailpath:</p>
\r
1634 <div class="listingblock">
\r
1635 <div class="content">
\r
1636 <pre><tt>typeset -a mailpath
\r
1637 for i in ~/Mail/Lists/*(.); do
\r
1638 mailpath[$#mailpath+1]="${i}?You have new mail in ${i:t}."
\r
1641 <p>Avoid globbing on special commands:</p>
\r
1642 <div class="listingblock">
\r
1643 <div class="content">
\r
1644 <pre><tt>for com in alias expr find mattrib mcopy mdir mdel which;
\r
1645 alias $com="noglob $com"</tt></pre>
\r
1647 <p>For migrating your bashprompt to zsh use the script bash2zshprompt located in
\r
1648 the zsh source distribution under <em>Misc</em>.</p>
\r
1649 <p>For migration from (t)csh to zsh use the c2z tool that converts csh
\r
1650 aliases and environment and shell variables to zsh. It does this by running
\r
1651 csh, and having csh report on aliases and variables. The script then converts
\r
1652 these to zsh startup files. It has some issues and usage information that are
\r
1653 documented at the top of this script.</p>
\r
1654 <p>Here are functions to set the title and hardstatus of an <strong>XTerm</strong> or of <strong>GNU
\r
1655 Screen</strong> to <em>zsh</em> and the current directory, respectively, when the prompt is
\r
1656 displayed, and to the command name and rest of the command line, respectively,
\r
1657 when a command is executed:</p>
\r
1658 <div class="listingblock">
\r
1659 <div class="content">
\r
1660 <pre><tt>function title {
\r
1661 if [[ $TERM == "screen" ]]; then
\r
1662 # Use these two for GNU Screen:
\r
1663 print -nR $' 33k'$1$' 33'\
\r
1664 print -nR $' 33]0;'$2$''
\r
1665 elif [[ $TERM == "xterm" || $TERM == "rxvt" ]]; then
\r
1666 # Use this one instead for XTerms:
\r
1667 print -nR $' 33]0;'$*$''
\r
1670 function precmd { title zsh "$PWD" }
\r
1671 function preexec {
\r
1673 local -a cmd; cmd=(${(z)1})
\r
1674 title $cmd[1]:t "$cmd[2,-1]"
\r
1677 <p>Put the following line into your ~/.screenrc to see this fancy hardstatus:</p>
\r
1678 <div class="listingblock">
\r
1679 <div class="content">
\r
1680 <pre><tt>caption always "%3n %t%? (%u)%?%?: %h%?"</tt></pre>
\r
1682 <p>Special variables which are assigned:</p>
\r
1683 <div class="listingblock">
\r
1684 <div class="content">
\r
1685 <pre><tt>$LINENO $RANDOM $SECONDS $COLUMNS $HISTCHARS $UID
\r
1686 $EUID $GID $EGID $USERNAME $fignore $mailpath $cdpath</tt></pre>
\r
1690 <div class="sectionbody">
\r
1697 <strong><a href="http://www.zsh.org/">http://www.zsh.org/</a></strong>
\r
1705 <strong><a href="http://sourceforge.net/projects/zsh/">http://sourceforge.net/projects/zsh/</a></strong>
\r
1709 Z shell page at sunsite.dk
\r
1713 <strong><a href="http://zsh.sunsite.dk/">http://zsh.sunsite.dk/</a></strong>
\r
1717 From Bash to Z Shell: Conquering the Command Line - the book
\r
1721 <strong><a href="http://www.bash2zsh.com/">http://www.bash2zsh.com/</a></strong>
\r
1725 Mailinglistarchive
\r
1729 <strong><a href="http://www.zsh.org/mla/">http://www.zsh.org/mla/</a></strong>
\r
1737 <strong><a href="http://www.zsh.org/FAQ/">http://www.zsh.org/FAQ/</a></strong>
\r
1745 <strong><a href="http://zsh.sunsite.dk/Guide/">http://zsh.sunsite.dk/Guide/</a></strong>
\r
1753 <strong><a href="http://www.zshwiki.org/">http://www.zshwiki.org/</a></strong>
\r
1757 Die Zsh als interaktive Shell
\r
1761 <strong><a href="http://cssun.rrze.uni-erlangen.de/~sipakale/zshreferat.html">http://cssun.rrze.uni-erlangen.de/~sipakale/zshreferat.html</a></strong>
\r
1765 A short introduction from BYU
\r
1769 <strong><a href="http://docs.cs.byu.edu/docs/zsh/index.php">http://docs.cs.byu.edu/docs/zsh/index.php</a></strong>
\r
1777 <strong><a href="http://stchaz.free.fr/mouse.zsh">http://stchaz.free.fr/mouse.zsh</a></strong>
\r
1781 Shell Corner: Zsh Suite of "keeper" Functions
\r
1785 <strong><a href="http://www.unixreview.com/documents/s=9513/ur0501a/ur0501a.htm">http://www.unixreview.com/documents/s=9513/ur0501a/ur0501a.htm</a></strong>
\r
1789 The Z Shell (A Fan Page)
\r
1793 <strong><a href="http://www.princeton.edu/~kmccarty/zsh.html">http://www.princeton.edu/~kmccarty/zsh.html</a></strong>
\r
1797 Making the Transition to Zsh
\r
1801 <strong><a href="http://www.linux-mag.com/cgi-bin/printer.pl?issue=2002-05&article=power">http://www.linux-mag.com/cgi-bin/printer.pl?issue=2002-05&article=power</a></strong>
\r
1805 Curtains up: introducing the Z shell
\r
1809 <strong><a href="http://www-128.ibm.com/developerworks/linux/library/l-z.html?dwzone=linux">http://www-128.ibm.com/developerworks/linux/library/l-z.html?dwzone=linux</a></strong>
\r
1813 ZSH-Liebhaberseite
\r
1817 <strong><a href="http://michael-prokop.at/computer/tools_zsh_liebhaber.html">http://michael-prokop.at/computer/tools_zsh_liebhaber.html</a></strong>
\r
1821 ZSH-Seite von Michael Prokop
\r
1825 <strong><a href="http://www.michael-prokop.at/computer/tools_zsh.html">http://www.michael-prokop.at/computer/tools_zsh.html</a></strong>
\r
1829 A Case for the Z Shell on <strong><a href="http://www.daemonnews.org/">http://www.daemonnews.org/</a></strong>
\r
1833 <strong><a href="http://ezine.daemonnews.org/199910/zsh.html">http://ezine.daemonnews.org/199910/zsh.html</a></strong>
\r
1837 ZSH-Section from Dotfiles.com
\r
1841 <strong><a href="http://www.dotfiles.com/index.php?app_id=4">http://www.dotfiles.com/index.php?app_id=4</a></strong>
\r
1845 Writing Zsh Completion Functions
\r
1849 <strong><a href="http://www.linux-mag.com/2002-07/power_01.html">http://www.linux-mag.com/2002-07/power_01.html</a></strong>
\r
1853 ZSH Prompt introduction
\r
1857 <strong><a href="http://aperiodic.net/phil/prompt/">http://aperiodic.net/phil/prompt/</a></strong>
\r
1865 <strong><a href="http://www.adamspiers.org/computing/zsh/">http://www.adamspiers.org/computing/zsh/</a></strong>
\r
1869 Zzappers Best of ZSH Tips
\r
1873 <strong><a href="http://www.rayninfo.co.uk/tips/zshtips.html">http://www.rayninfo.co.uk/tips/zshtips.html</a></strong>
\r
1877 Zsh Webpage by Christian Schneider
\r
1881 <strong><a href="http://strcat.de/wiki/zsh/">http://strcat.de/wiki/zsh/</a></strong>
\r
1882 <strong><a href="http://strcat.de/wiki/zsh-german">http://strcat.de/wiki/zsh-german</a></strong> (German translation. Help needed!)
\r
1886 The zsh-lovers webpage
\r
1890 <strong><a href="http://grml.org/zsh/">http://grml.org/zsh/</a></strong>
\r
1898 <strong>#zsh at irc.libera.chat</strong>
\r
1902 The Z shell reference-card (included in the zsh-lovers debian-package)
\r
1906 <strong><a href="http://www.bash2zsh.com/zsh_refcard/refcard.pdf">http://www.bash2zsh.com/zsh_refcard/refcard.pdf</a></strong>
\r
1912 <div class="sectionbody">
\r
1913 <p>This manpage was written by Michael Prokop, Christian <em>strcat</em>
\r
1914 Schneider and Matthias Kopfermann. But many ideas have been taken from
\r
1915 zsh-geeks e.g. from the zsh-mailinglists (zsh-users and zsh-workers),
\r
1916 google, newsgroups and the zsh-Wiki.<br />
\r
1917 Thanks for your cool and incredible tips. We learned much from you!</p>
\r
1918 <p>In alphabetic order:</p>
\r
1919 <div class="listingblock">
\r
1920 <div class="content">
\r
1921 <pre><tt>Andrew 'zefram' Main - http://www.fysh.org/~zefram/
\r
1922 Barton E. Schaefer - http://www.well.com/user/barts/
\r
1923 Matthias Kopfermann - http://www.infodrom.north.de/~matthi/
\r
1924 Oliver Kiddle - http://people.freenet.de/opk/
\r
1925 Paul Falstad - http://www.falstad.com/
\r
1926 Peter Stephenson - http://homepage.ntlworld.com/p.w.stephenson/
\r
1928 Stephane Chazelas - http://stephane.chazelas.free.fr/
\r
1929 Sven Guckes - http://www.guckes.net/
\r
1930 Sven Wischnowsky - http://w9y.de/zsh/zshrc</tt></pre>
\r
1934 <div class="sectionbody">
\r
1935 <p>Manpages of zsh:</p>
\r
1936 <div class="listingblock">
\r
1937 <div class="content">
\r
1938 <pre><tt> zsh Zsh overview
\r
1939 zshall Tthe Z shell meta-man page
\r
1940 zshbuiltins Zsh built-in commands
\r
1941 zshcalsys zsh calendar system
\r
1942 zshcompctl zsh programmable completion
\r
1943 zshcompsys Zsh completion system
\r
1944 zshcompwid Zsh completion widgets
\r
1945 zshcontrib User contributions to zsh
\r
1946 zshexpn Zsh expansion and substitution
\r
1947 zshmisc Anything not fitting into the other sections
\r
1948 zshmodules Zsh loadable modules
\r
1949 zshoptions Zsh options
\r
1950 zshparam Zsh parameters
\r
1951 zshroadmap Informal introduction to the zsh manual
\r
1952 zshtcpsys Zsh tcp system
\r
1953 zshzle Zsh command line editing
\r
1954 zshzftpsys Zsh built-in FTP client
\r
1955 zshall Meta-man page containing all of the above</tt></pre>
\r
1957 <p>Note: especially <em>man zshcontrib</em> covers very useful topics! <tt>
\r
1958 Book: <strong>From Bash to Z Shell</strong> by Oliver Kiddle, Jerry Peck and Peter
\r
1959 Stephenson. <strong>ISBN: 1590593766</strong>. - <strong><a href="http://www.bash2zsh.com/">bash2zsh.com</a></strong> </tt>
\r
1960 Also take a look at the section <strong>LINKS</strong> in this manpage.</p>
\r
1963 <div class="sectionbody">
\r
1964 <p>Probably. This manpage might be never complete. So please report bugs,
\r
1965 feedback and suggestions to <zsh-lovers@michael-prokop.at>. Thank
\r
1968 <h2>COPYRIGHT</h2>
\r
1969 <div class="sectionbody">
\r
1970 <p>Copyright (C) Michael Prokop, Christian Schneider and Matthias
\r
1974 <div id="footer-text">
\r
1975 Last updated 01-Nov-2009 04:31:39 CEST
\r