I want to know the number of items (files, sub-directories etc) inside a particular directory.
There seems to be a lot of questions about it but most, if not all, answers seems to rely on wc -l
. For instance:
ls -1 | wc -l
But that will give the wrong answer if one or more files have new lines in their names. Another problem here is that ls -1
might return more than just files.
A non portable answer was hidden inside the comments of one of the thousands similar questions. Works, but unfortunately it relies on -printf
which is not available in my distro.
find -maxdepth 1 -type f -printf "\n" | wc -l
(by godlygeek)
Is there a more portable solution that can correctly handle files containing any char in their names?
sh
, which would tell you all you needed to know (I think) :) And the system is Alpine Linux v3.12 – user1593842 Oct 19 '20 at 13:44