Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Post History

77%
+5 −0
Q&A How to get number of files in directory

Populate an array of the file names and then print how many entries are in the array: $ ( shopt -s nullglob; files=( .* * ); echo "${#files[@]}" ) 124 That will work correctly even if your fi...

posted 2y ago by Ed Morton‭  ·  edited 3mo ago by Ed Morton‭

Answer
#3: Post edited by user avatar Ed Morton‭ · 2024-10-06T13:22:22Z (3 months ago)
  • Populate an array of the file names and then print how many entries are in the array:
  • $ ( shopt -s nullglob; files=( .* * ); echo "${#files[@]}" )
  • 124
  • That will work correctly even if your file names contain newlines, unlike anything piped to `wc -l`. I'm using a subshell to restrict the environment that `shopt -s nullglob` applies to - if you don't care about that then you don't need a subshell.
  • Populate an array of the file names and then print how many entries are in the array:
  • $ ( shopt -s nullglob; files=( .* * ); echo "${#files[@]}" )
  • 124
  • That will work correctly even if your file names contain newlines, unlike anything piped to `wc -l`. I'm using a subshell to restrict the environment that `shopt -s nullglob` applies to - if you don't care about that then you don't need a subshell.
  • Note that the above includes both files and directories (which are, of course, a type of file) - if you want to exclude directories then you need a different solution.
#2: Post edited by user avatar Ed Morton‭ · 2024-10-06T13:18:16Z (3 months ago)
  • Populate an array of the file names and then print how many entries are in the array:
  • $ files=( * )
  • $ echo "${#files[@]}"
  • 124
  • That will work correctly even if your file names contain newlines, unlike anything piped to `wc -l`.
  • Populate an array of the file names and then print how many entries are in the array:
  • $ ( shopt -s nullglob; files=( .* * ); echo "${#files[@]}" )
  • 124
  • That will work correctly even if your file names contain newlines, unlike anything piped to `wc -l`. I'm using a subshell to restrict the environment that `shopt -s nullglob` applies to - if you don't care about that then you don't need a subshell.
#1: Initial revision by user avatar Ed Morton‭ · 2023-03-31T00:15:07Z (almost 2 years ago)
Populate an array of the file names and then print how many entries are in the array:

    $ files=( * )
    $ echo "${#files[@]}"
    124

That will work correctly even if your file names contain newlines, unlike anything piped to `wc -l`.