Skip to main content
replaced non-working glob qualifier trickery by working glob qualifier trickery (even across directories)
Source Link
Gilles 'SO- stop being evil'
  • 865.3k
  • 205
  • 1.8k
  • 2.3k

In zsh, showLatest 10 is (om[1,10]) at the end of the pattern. These are glob qualifiers; om sorts by modification time (youngest first) and [1,10] selects the first 10 matches.

find src -type f -iname \*.py is print -lr src/**/(#i)*.py(D.) (you need to run setopt extended_glob first, put that in your ~/.zshrc).

The globbing flag . filters regular files only, D includes hidden files and files in hidden directories like the find version would. (#i) is a globbing flag that makes the remainder of the pattern case-insensitive.

print -lr prints its arguments, one per line (make that print -lr -- if the first argument may begin with a dash). Combining the two, to get the most recent files:

print -lr src/**/(#i)*.py(D.om[1,10])

which in practice you can shorten to print -lr src/**/*.py(om[1,10]).

In zsh, you probably won't need this because you can sort the list of files when you get it, but here'sat least if they're all coming from a single pattern. Here's a way to implement showLatest in zsh that works with an arbitrary list of files. Unfortunately, the om glob qualifier can only be applied to a single pattern, and a single pattern can only match files in a single directory or tree. A trick to bypass this is to use the e (or +) glob qualifier to inject an arbitrary list into a match result. Then apply an o glob qualifier to perform the sorting; built-in qualifiers don't act on the result of the e/+ rewrite, but custom ones (oe or o+) do (as of zsh 5.0.5).

#!/usr/bin/env zsh
pattern="(${(j:|:)${zmodload zsh/stat
files=(@q)$"${(f@f)$(<&0)}}})(om[1,$1])")
print -lr .(e\''reply=($files)'\'noe\''stat -A REPLY +mtime -- $~pattern$REPLY'\')

In zsh, showLatest 10 is (om[1,10]) at the end of the pattern. These are glob qualifiers; om sorts by modification time (youngest first) and [1,10] selects the first 10 matches.

find src -type f -iname \*.py is print -lr src/**/(#i)*.py(D.) (you need to run setopt extended_glob first, put that in your ~/.zshrc).

The globbing flag . filters regular files only, D includes hidden files and files in hidden directories like the find version would. (#i) is a globbing flag that makes the remainder of the pattern case-insensitive.

print -lr prints its arguments, one per line (make that print -lr -- if the first argument may begin with a dash). Combining the two, to get the most recent files:

print -lr src/**/(#i)*.py(D.om[1,10])

which in practice you can shorten to print -lr src/**/*.py(om[1,10]).

In zsh, you probably won't need this because you can sort the list of files when you get it, but here's a way to implement showLatest.

#!/usr/bin/env zsh
pattern="(${(j:|:)${(@q)${(f)$(<&0)}}})(om[1,$1])"
print -lr -- $~pattern

In zsh, showLatest 10 is (om[1,10]) at the end of the pattern. These are glob qualifiers; om sorts by modification time (youngest first) and [1,10] selects the first 10 matches.

find src -type f -iname \*.py is print -lr src/**/(#i)*.py(D.) (you need to run setopt extended_glob first, put that in your ~/.zshrc).

The globbing flag . filters regular files only, D includes hidden files and files in hidden directories like the find version would. (#i) is a globbing flag that makes the remainder of the pattern case-insensitive.

print -lr prints its arguments, one per line (make that print -lr -- if the first argument may begin with a dash). Combining the two, to get the most recent files:

print -lr src/**/(#i)*.py(D.om[1,10])

which in practice you can shorten to print -lr src/**/*.py(om[1,10]).

In zsh, you probably won't need this because you can sort the list of files when you get it, at least if they're all coming from a single pattern. Here's a way to implement showLatest in zsh that works with an arbitrary list of files. Unfortunately, the om glob qualifier can only be applied to a single pattern, and a single pattern can only match files in a single directory or tree. A trick to bypass this is to use the e (or +) glob qualifier to inject an arbitrary list into a match result. Then apply an o glob qualifier to perform the sorting; built-in qualifiers don't act on the result of the e/+ rewrite, but custom ones (oe or o+) do (as of zsh 5.0.5).

#!/usr/bin/env zsh
zmodload zsh/stat
files=("${(@f)$(<&0)}")
print -lr .(e\''reply=($files)'\'noe\''stat -A REPLY +mtime -- $REPLY'\')
added 98 characters in body
Source Link
Stéphane Chazelas
  • 584.6k
  • 96
  • 1.1k
  • 1.7k

In zsh, showLatest 10 is (om[1,10]) at the end of the pattern. These are glob qualifiers; om sorts by modification time (youngest first) and [1,10] selects the first 10 matches.

find src -type f -iname \*.py is print -lr src/**/(#i)*.py(D.) (you need to run setopt extended_glob first, put that in your ~/.zshrc). 

The globbing flag . filters regular files only, D includes hidden files and files in hidden directories like the find version would. (#i) is a globbing flag that makes the remainder of the pattern case-insensitive.   

print -lr prints its arguments, one per line (make that print -lr -- if the first argument may begin with a dash). Combining the two, to get the most recent files:

print -lr src/**/(#i)*.py(D.om[1,10])

which in practice you can shorten to print -lr src/**/*.py(om[1,10]).

In zsh, you probably won't need this because you can sort the list of files when you get it, but here's a way to implement showLatest.

#!/usr/bin/env zsh
pattern="(${(j:|:)${(@q)${(f)$(<&0)}}})(om[1,$1])"
print -lr -- $~pattern

In zsh, showLatest 10 is (om[1,10]) at the end of the pattern. These are glob qualifiers; om sorts by modification time (youngest first) and [1,10] selects the first 10 matches.

find src -type f -iname \*.py is print -lr src/**/(#i)*.py(.) (you need to run setopt extended_glob first, put that in your ~/.zshrc). The globbing flag . filters regular files only. (#i) is a globbing flag that makes the remainder of the pattern case-insensitive.  print -lr prints its arguments, one per line (make that print -lr -- if the first argument may begin with a dash). Combining the two, to get the most recent files:

print -lr src/**/(#i)*.py(.om[1,10])

which in practice you can shorten to print -lr src/**/*.py(om[1,10]).

In zsh, you probably won't need this because you can sort the list of files when you get it, but here's a way to implement showLatest.

#!/usr/bin/env zsh
pattern="(${(j:|:)${(@q)${(f)$(<&0)}}})(om[1,$1])"
print -lr -- $~pattern

In zsh, showLatest 10 is (om[1,10]) at the end of the pattern. These are glob qualifiers; om sorts by modification time (youngest first) and [1,10] selects the first 10 matches.

find src -type f -iname \*.py is print -lr src/**/(#i)*.py(D.) (you need to run setopt extended_glob first, put that in your ~/.zshrc). 

The globbing flag . filters regular files only, D includes hidden files and files in hidden directories like the find version would. (#i) is a globbing flag that makes the remainder of the pattern case-insensitive. 

print -lr prints its arguments, one per line (make that print -lr -- if the first argument may begin with a dash). Combining the two, to get the most recent files:

print -lr src/**/(#i)*.py(D.om[1,10])

which in practice you can shorten to print -lr src/**/*.py(om[1,10]).

In zsh, you probably won't need this because you can sort the list of files when you get it, but here's a way to implement showLatest.

#!/usr/bin/env zsh
pattern="(${(j:|:)${(@q)${(f)$(<&0)}}})(om[1,$1])"
print -lr -- $~pattern
Source Link
Gilles 'SO- stop being evil'
  • 865.3k
  • 205
  • 1.8k
  • 2.3k

In zsh, showLatest 10 is (om[1,10]) at the end of the pattern. These are glob qualifiers; om sorts by modification time (youngest first) and [1,10] selects the first 10 matches.

find src -type f -iname \*.py is print -lr src/**/(#i)*.py(.) (you need to run setopt extended_glob first, put that in your ~/.zshrc). The globbing flag . filters regular files only. (#i) is a globbing flag that makes the remainder of the pattern case-insensitive. print -lr prints its arguments, one per line (make that print -lr -- if the first argument may begin with a dash). Combining the two, to get the most recent files:

print -lr src/**/(#i)*.py(.om[1,10])

which in practice you can shorten to print -lr src/**/*.py(om[1,10]).

In zsh, you probably won't need this because you can sort the list of files when you get it, but here's a way to implement showLatest.

#!/usr/bin/env zsh
pattern="(${(j:|:)${(@q)${(f)$(<&0)}}})(om[1,$1])"
print -lr -- $~pattern