Skip to content

Instantly share code, notes, and snippets.

@flavius
Created April 27, 2013 13:10
Show Gist options
  • Save flavius/5473073 to your computer and use it in GitHub Desktop.
Save flavius/5473073 to your computer and use it in GitHub Desktop.
145750 flavius │ Hi. How to count the number of files in a directory (for a conditional "if there are any files in the directory, do │ Chiku|dc
│ this"), without spawning a subprocess like ls? │ chris2
145938 ccxCZ │ > files=( *(N) ); echo $#files │ Chryyz
145939 SweetieBot │ ccxCZ: 0 │ chutzpah
150011 ccxCZ │ though that doesn't get .files │ cinch
150111 flavius │ ccxCZ: and where do I put the path to the directory? │ claytron
150114 Mikachu │ (DN) │ cod3r
150123 Mikachu │ /the/directory/*(DN) │ Codynyx
150216 flavius │ so in the end: files=( ~/test/*(DN) ); if [[ $#files ]] │ Corey
150239 m0viefreak │ that would be true even when there are 0 files │ crazedpsyc
150251 flavius │ -gt 0 maybe │ crees
150252 m0viefreak │ files=( ~/test/*(DN) ); if (( files )) │ cschneid
150338 flavius │ then why not if (( ( ~/test/*(DN) ) )) ? │ cxreg
150338 m0viefreak │ always use an arithmetic context (( )) when doing match or comparing numbers │ danielsh
150410 m0viefreak │ oh │ dario
150415 m0viefreak │ files=( ~/test/*(DN) ); if (( $#files )) │ DarthGandalf
150510 flavius │ is it possible to squeeze the two statements into one, count via $# and also do the glob search? │ dbr
150556 Mikachu │ if you want nonempty, just use /path/to/dir(F) │ der-onkel
150606 Mikachu │ FN of course │ derf0 ▼
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment