1
1
12rbah2019-11-15 06:56:40
linux
12rbah, 2019-11-15 06:56:40

How to determine the number of objects in a directory (files and folders)?

It is assumed that you will have to scan quite large directories, and I would like to know if there are built-in tools in Linux, Windows in order to quickly find out the number of objects in a directory, or does it make sense to write a function to count files and directories yourself?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
H
hint000, 2019-11-15
@hint000

are there built-in tools in linux

If you need the number of objects directly in the specified directory, then
If you need the number of objects together with those recursively nested in subdirectories, then
In both cases, the name of the target directory is taken as an example /home
Well, the result is plus or minus one or two, :) I was too lazy to take into account the headers. If the actual quantity is accurate to one, then correct it.

A
Alexander Pavlyuk, 2019-11-15
@pav5000

It has a handy filepath.Walk function that traverses a folder tree recursively. It is enough to count calls to WalkFunc or check inside this function os.FileInfo for whether it is a file or a folder and only then count.
https://golang.org/pkg/path/filepath/#Walk

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question