Recursively count all the files in a directory
Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn
--
Music by Eric Matyas
https://www.soundimage.org
Track title: Sunrise at the Stream
--
Chapters
00:00 Question
01:13 Accepted answer (Score 398)
01:48 Answer 2 (Score 50)
02:42 Answer 3 (Score 26)
03:00 Answer 4 (Score 8)
03:16 Thank you
--
Full question
https://superuser.com/questions/198817/r...
Question links:
[How can I count the number of folders in a drive using Linux?]: https://superuser.com/questions/129088/h...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#linux #commandline
#avk47
--
Music by Eric Matyas
https://www.soundimage.org
Track title: Sunrise at the Stream
--
Chapters
00:00 Question
01:13 Accepted answer (Score 398)
01:48 Answer 2 (Score 50)
02:42 Answer 3 (Score 26)
03:00 Answer 4 (Score 8)
03:16 Thank you
--
Full question
https://superuser.com/questions/198817/r...
Question links:
[How can I count the number of folders in a drive using Linux?]: https://superuser.com/questions/129088/h...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#linux #commandline
#avk47
ACCEPTED ANSWER
Score 401
find . -type f | wc -l
Explanation:
find . -type f
finds all files ( -type f ) in this ( . ) directory and in all sub directories, the filenames are then printed to standard out one per line.
This is then piped | into wc
(word count) the -l
option tells wc to only count lines of its input.
Together they count all your files.
ANSWER 2
Score 25
For files:
find -type f | wc -l
For directories:
find -mindepth 1 -type d | wc -l
They both work in the current working directory.
ANSWER 3
Score 8
With bash 4+
shopt -s globstar
for file in **/*
do
if [ -d "$file" ];then
((d++))
elif [ -f "$file" ];then
((f++))
fi
done
echo "number of files: $f"
echo "number of dirs: $d"
No need to call find twice if you want to search for files and directories
ANSWER 4
Score 8
Slight update to accepted answer, if you want a count of dirs and such
find $DIR -exec stat -c '%F' {} \; | sort | uniq -c | sort -rn