101 Powerful & Practical ZSH GLOBS

v0.9a 13 May 2021 Probably contains errors please email any comments to zzapper@gmail.com zzapper's ZSH home

Glob by File Size (L)

L bytes (default) ,Lk kilo,Lm megabytes

size bytes

 ls *(.L0)         # list pesky empty files (yes that is a zero) 
 ls *(.DL0)         # list empty files include DotFiles (D)
 ls **(.L0)         # recursivively list empty files anywhere in hierarchy
 ls -l *(.L-20)    # list file size less than 20 bytes 
 ls -l *(.L20)    # list file size exactly 20 bytes 
 ls -l *(.L+20)    # list file size more than 20 bytes 

'k' size KB

 ls -l *(Lk+100)   # list file size more than 100kb 
 ls -l *(Lk-100)   # list file size less 100kb 

'm' size MB

 ls -l *(Lm+2)     # list file size larger/greater than 2 mbs 
 ls -l *(Lm-2)     # list file size less than 2 mbs 

Useful Size GLOBS

 ls **(.Lm+10)   # list files larger than 10MB anywhere in hierarchy 
 ls -hlS **(.Lm+2)  | less  # list largest files  largest first  

Glob by File Size ORDER (OL)

Size GLOBS By Order (O/o)

 ls -lhS *(.oL[1]) # smallest file in directory
 ls -lhS *(.OL[1]) # largest file in directory
 ls -lhS /**(.OL[1]) # largest file on system (slow)
 ls -lhS /**~(/proc/*)(.OL[1]) # but exclude O/S (kcore)
 ls -hlS /**~(/proc/*)(.OL[1,10]) # find the 10 biggest files on your system 
 ls -lS **.(php|inc)~(libs|locallibs)/*(.OL[1,5]) #  find largest files excluding certain directories

Glob by Modification Time (m)

m=minutes,d=days(default),w=week,M=Month,+n older than,-n younger than,just n equal to

m minutes

 ls *(.mm0)  # modified in last minute
 ls *(.mm+1)  # older than an minute
 ls *(.^mm0)  # older than an minute
 ls *(.mm3)  # modified 3 minutes ago
 ls *(.mm-3) # less than 3 minutes
 ls *(.mm+3) # more than 3 minutes

h hours

 ls *(.mh0)  # modified in last hour
 ls *(.mh3)  # modified 3 hours ago
 ls *(.mh-3) # less than 3 hours
 ls *(.mh+3) # more than 3 hours
 ls *(.^mh3) # all files not 3 hours  old

days (default)

 ls *(.m0)   # modified today (last 24 hours)
 ls *(.m-1)  # modified today (last 24 hours)
 ls *(.^m0)  # not modified today
 ls *(.m3)   # modified 3 days ago

d days which is same as above

 ls *(.md0)   # modified today (last 24 hours)
 ls *(.md-1)  # modified today (last 24 hours)
 ls *(.^md0)  # not modified today
 ls *(.md3)   # modified 3 days ago

w weeks

 ls *(.mw0)  # this week
 ls *(.mw+2) # older than 2 weeks 
 ls *(.mw2) # 2 weeks old 
 ls *(.mw-2) # less than 2 weeks 

M months

 ls *(.mM0)  # this month
 ls *(.^mM0) # older tham a month
 ls *(.mM+6) # older than 6 months 

M*12 Year No modifier for years so use multiples of months

 ls *(.mM-12) # this year
 ls *(.^mM+12) # older than a year

GLOB By Modification Time Order

 ls *(.[1,10]) # 10 files
 ls -lt *(.Om[1]) # oldest file
 ls -lt *(.om[1]) # youngest file
 ls -lt *(.om[1,10]) # 10 youngest files in date ascending order
 ls -lt *(.Om[1,10]) # 10 oldest files in date descending order
 ls -lt *(.m0om[1,10]) # youngest 10 files modified in last 24 hrs 
 ls -lt *(.m0Om[1,10]) # oldest 10 files modified in last 24 hrs 
 ls -lt *(.om[3,$]) # All but 3 newest files

Complex GLOB by Modified Time (m)

d days (default)

 ls *(.m0)   # modified today (last 24 hours)
 ls *.(php|js)(.m0)   # php,js files modified today 
 ls *.(php|js)~(test.*)(.m0)   # php,js files modified today exlude test.*
 ls **.(php|js)~(test.*|test/*)(.m0)  # recursive exclude test files and directory 

GLOB By Very Precise Date & Time (age)

 autoload -U age
 ls -tl *(.e#age yesterday #)
 ls -tl *(.e-age 08:00 09:05 -)
 ls -lt *(.e:age 2021/04/01 +5d:)
 touch -d '11:50 04 October 2006'  {apache,mysql}.log
 grep -l 'server crash' *.log(.e#age 2006/10/04:10:15 2006/10/04:12:45#)

GLOB by Ownership/Permissions

 ls *(.f644)   # files with permissions 644
 ls *(.g:root:)  # files belonging to group root
 ls *(.u:apache:)  # files belonging to user apache
 ls -l *(.rwg:nobody:u:root:) # user has read/write permissions

Excluding files a-m but only if owned by apache

-rwxr-xr-x. 1 nobody (owner) apache (Group) 0 Feb 24 10:23 x.x

 ls -l *.*~[a-m]*(u:nobody:g:apache:.xX)

find all files owned by root (u0), world-writable (W), more than 10k in size (Lk+10) and modified during the last hour (m0)

 ls **/*(u0WLk+10m0)

find all files that don't have the write permission to group in current directory and all subdirectories

 ls **(.:g-w:)

GLOBS for grep excluding certain directories

 grep -i somestr **/*.(js|php|css) | grep -Ev 'libs/|temp/|test/' 
 grep -i somestr **/*.(js|php|css)~libs/*~temp/*~test/* 
 grep -i somestr **/*.(js|php|css)~(libs|temp|test)/* 
 grep -iw '$direct' report/**/*.{inc,php}  # searching for a php variable
 grep -i "$1" **/*.{js,php,css}~(libs|temp|tmp|test)/* # exclude directories from grep *N* EXTENDED_GLOB required

GLOBS use xargs or zargs for grepping huge hierarchies

 autoload zargs;
 zargs ./**/*.{php,inc,js} -- grep -i 'cons. unit'   
 zargs **/*.(js|php|css)~(libs|locallibs|test|dompdf)/* -- grep console.log 
 zargs ./**/*.(php|inc|js) -- tar rvf dev2$(date '+%d-%m-%Y').tar *N*
 zargs ./**/*.{inc,php} -- grep -i 'if *( *$[a-z0-9_]*=[0-9"]'   ## detect if ($fred=2) type php errors (single equals) 

Selectively tar a web root

 zargs ./{html,live}/**/*.(php|inc|js)~(**/wiki|**/dompdf)/* -- tar rvf /tmp/web2$(date "+%d-%m-%Y").tar
 zargs **/*.(php|inc) -- sed -i 's#ereg_replace("\([^"]*\)"#preg_replace("/\1/"#g'    ## global sed substitute using zargs 
 zargs  /tmp/*(.m+30) -- rm # purge files older than 30 days

GLOBS to purge a huge directory

 mv *.jpg(.m+2) old/ # older than 2 days
 mv *.log(.mw+3) old/ # older than 3 weeks
 mv *(.mM+6) old/ # older than 6 months
 mv *(.[1,100]) old/  # 100 files
 mv *(.Om[1,100]) old/  # oldest 100 files

Miscellaneous GLOBS

 a=(**/*(.D));echo $#a  # count files in a (huge) hierarchy
 ls ^x*           # list all but x*

list all files without an extension ( no dot)

 ls *~*.*(.) # list all files with no dot
 ls -ld **/*(/^F) # recursively list any empty sub-directories