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

71%
+3 −0
Q&A How to get number of files in directory

A solution I often use (and which is ultimately a variation of the find-based approach in the answer by Canina) also uses find, but only prints a single . per file: find . -maxdepth 1 -type f -pri...

posted 11mo ago by AdminBee‭  ·  edited 11mo ago by AdminBee‭

Answer
#3: Post edited by user avatar AdminBee‭ · 2023-06-13T12:48:28Z (11 months ago)
  • A solution I often use (and which is ultimately a variation of the `find`-based approach in the [answer by Canina](https://linux.codidact.com/posts/286769/286772#answer-286772)) also uses `find`, but only prints a single `.` per file:
  • ```lang-bash
  • find . -maxdepth 1 -type f -printf '.' | wc -m
  • ```
  • It then uses the `-m` flag of `wc` to print the number of characters, rather than lines (for obvious reasons).
  • This approach also avoids problems that may occur if the actual filenames contain "exotic" characters (such as the newline); it may in addition have a slight speed advantage over printing the entire filename for cases where there are many files and, possibly, long filenames (but YMMV and I haven't conducted studies).
  • *Note* that, as with any `find`-based approach, this will also count hidden files (those whose name begin with `.`), so the result can differ from what you might expect based on the output of `ls`.
  • A solution I often use (and which is ultimately a variation of the `find`-based approach in the [answer by Canina](https://linux.codidact.com/posts/286769/286772#answer-286772)) also uses `find`, but only prints a single `.` per file:
  • ```lang-bash
  • find . -maxdepth 1 -type f -printf '.' | wc -m
  • ```
  • It then uses the `-m` flag of `wc` to print the number of characters, rather than lines (for obvious reasons).
  • This approach also avoids problems that may occur if the actual filenames contain "exotic" characters (such as the newline); it may in addition have a slight speed advantage over printing the entire filename for cases where there are many files and, possibly, long filenames (but YMMV and I haven't conducted studies).
  • *Note* that, as with any `find`-based approach, this will also count hidden files (those whose name begin with `.`) unless you filter them out, so the result can differ from what you might expect based on the output of `ls`.
#2: Post edited by user avatar AdminBee‭ · 2023-06-13T12:47:53Z (11 months ago)
Add clarification
  • A solution I often use (and which is ultimately a variation of the `find`-based approach in the [answer by Canina](https://linux.codidact.com/posts/286769/286772#answer-286772)) also uses `find`, but only prints a single `.` per file:
  • ```lang-bash
  • find . -maxdepth 1 -type f -printf '.' | wc -m
  • ```
  • It then uses the `-m` flag of `wc` to print the number of characters, rather than lines (for obvious reasons).
  • This approach also avoids problems that may occur if the actual filenames contain "exotic" characters (such as the newline); it may in addition have a slight speed advantage over printing the entire filename for cases where there are many files and, possibly, long filenames (but YMMV and I haven't conducted studies).
  • A solution I often use (and which is ultimately a variation of the `find`-based approach in the [answer by Canina](https://linux.codidact.com/posts/286769/286772#answer-286772)) also uses `find`, but only prints a single `.` per file:
  • ```lang-bash
  • find . -maxdepth 1 -type f -printf '.' | wc -m
  • ```
  • It then uses the `-m` flag of `wc` to print the number of characters, rather than lines (for obvious reasons).
  • This approach also avoids problems that may occur if the actual filenames contain "exotic" characters (such as the newline); it may in addition have a slight speed advantage over printing the entire filename for cases where there are many files and, possibly, long filenames (but YMMV and I haven't conducted studies).
  • *Note* that, as with any `find`-based approach, this will also count hidden files (those whose name begin with `.`), so the result can differ from what you might expect based on the output of `ls`.
#1: Initial revision by user avatar AdminBee‭ · 2023-06-13T12:46:06Z (11 months ago)
A solution I often use (and which is ultimately a variation of the `find`-based approach in the [answer by Canina](https://linux.codidact.com/posts/286769/286772#answer-286772)) also uses `find`, but only prints a single `.` per file:
```lang-bash
find . -maxdepth 1 -type f -printf '.' | wc -m
```
It then uses the `-m` flag of `wc` to print the number of characters, rather than lines (for obvious reasons).

This approach also avoids problems that may occur if the actual filenames contain "exotic" characters (such as the newline); it may in addition have a slight speed advantage over printing the entire filename for cases where there are many files and, possibly, long filenames (but YMMV and I haven't conducted studies).