Recursively count all the files in a directory
--------------------------------------------------
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------
Music by Eric Matyas
https://www.soundimage.org
Track title: Magical Minnie Puzzles
--
Chapters
00:00 Recursively Count All The Files In A Directory
00:52 Accepted Answer Score 401
01:21 Answer 2 Score 25
01:34 Answer 3 Score 8
01:50 Answer 4 Score 8
01:58 Thank you
--
Full question
https://superuser.com/questions/198817/r...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#linux #commandline
#avk47
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------
Music by Eric Matyas
https://www.soundimage.org
Track title: Magical Minnie Puzzles
--
Chapters
00:00 Recursively Count All The Files In A Directory
00:52 Accepted Answer Score 401
01:21 Answer 2 Score 25
01:34 Answer 3 Score 8
01:50 Answer 4 Score 8
01:58 Thank you
--
Full question
https://superuser.com/questions/198817/r...
--
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