Filter Files Directly in Zsh with Extended Glob Qualifiers: A Practical Guide
Learn how to use Zsh’s extended glob qualifiers to filter files by size, time, permissions, and more—saving time and avoiding external commands.
28 Oct 2025, 08:56 UTC

Concrete Problem
When working in a directory with thousands of files, I often need to process only a subset—say, hidden files, recent modifications, or large binaries—without spawning external utilities.
Thesis
Zsh’s extended glob qualifiers let you express these filters directly in the shell’s pattern syntax, keeping scripts fast and concise.
Understanding Extended Glob Qualifiers
Qualifiers follow a pattern like *(+h) and can be combined:
+h– hidden files (dot‑files)/– directories%10– modified in the last 10 days+s– size qualifiers (e.g.,+s1kfor >1 kB)- Logical operators:
|(OR),!(NOT)
Syntax Overview
*(+h) # all hidden files
*(/ | +h) # directories or hidden files
*(%10) # modified within last 10 days
*(+s1k) # larger than 1 kB
*(+s1k|%10) # large files or recently modified
*(+s1k&!+h) # large files that are not hidden
Practical Example
Suppose you want to copy all non‑hidden files larger than 1 MB that were modified in the last 7 days to a backup directory:
# run in an interactive Zsh session
mkdir -p /tmp/backup
cp *(+s1M%7) /tmp/backup/
Explanation:
*– match all entries in the current directory+s1M– size > 1 MiB%7– modified within the last 7 days- Both qualifiers are applied simultaneously; only files satisfying both appear.
Check the result with:
ls -lh /tmp/backup/
Verify that the copied files match the criteria by comparing with stat or ls -l on the source.
Trade‑offs & Limitations
- Portability: Qualifiers are Zsh‑specific; scripts will fail in Bash, Fish, or POSIX shells.
- Version support: Qualifiers such as
+Mand+srequire Zsh 4.3+. On older systems, the pattern may expand to nothing. - Readability: Complex chains like
*(+s1M%7|!+h)can be hard to read; consider helper functions or comments. - Filesystem metadata: On networked or virtual filesystems, modification times or sizes may be unreliable.
Actionable Next Steps
- Check your Zsh version:
zsh --version - Test a simple qualifier in a fresh directory:
echo *(+h) - Replace a slow
findpipeline in your script with an equivalent glob qualifier, e.g.,ls -l *(*[1-9]k) - Document the qualifier logic in comments so future maintainers understand the intent.
By integrating extended glob qualifiers, you can write lean, high‑performance shell scripts that stay entirely within the shell’s built‑in capabilities.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.