Post History
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...
Answer
#3: Post edited
- 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
- 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
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).