Find number of files in folder and sub folders?
I want to find the total count of the number of files under a folder and all its sub folders.
May be something like
find . -type f | wc -l
would do the trick. Try the command from the parent folder.
find . -name <pattern> -type f
finds all files in.
and subfolders. The result (a list of files found) is passed (|
) towc -l
which counts the number of lines.-name <pattern>
only looks for certain files.The solution will fail on files which names contain a newline.
@user unknown: `find . -type f -ls | wc -l`
even faster: `find . -type f -print0 | tr -d -c '\0' | wc -c`
@arrange: even faster: `find . -type f -printf . | wc -c` - I adopt the print for my solution instead of my -exec echo .
Be aware that this also counts hidden files starting with a dot. I consider this a feature rather than a bug, but it is good to know.
this command worked like a charm in my case, looks like black magic
For future references, can you elaborate a bit more on what the command is doing? Thanks
Use the
tree
command. You might need to install thetree
package.It will list all the files and folders under the given folder and list a summary at the end.
To count files (even files without an extension) at the root of the current directory, use:
ls -l | grep ^- | wc -l
To count files (even files without an extension) recursively from the root of the current directory, use:
ls -lR | grep ^- | wc -l
Those will not count hidden files.
True. I'm more inclined to accept and use your answer as the solution.
Actually, not counting hidden files / files in hidden directories is an useful _feature_ while working inside a subversion or git repository!
The fastest and easiest way, is to use
tree
. Its speed is limited by your output terminal, so if you pipe the result totail -1
, you'll get immediate result. You can also control to what directory level you like the results, using the-L
option. For colorized output, use-C
. For example:$ tree share/some/directory/ | tail -1 558 directories, 853 files $ tree -L 2 share/some/directory/ | tail -1 120 directories, 3 files
If it's not already there, you can get it here.
find -type f -printf . | wc -c
Don't count the output lines of find, because filenames, containing 99 newlines, will count as 100 files.
Filenames containing new lines is an incredibly rare edge case.
@DisgruntledGoat: So an error will be extremely hard to find.
I like a good edge case, especially many years later.
Use this command for each folder in the path
for D in *; do echo $D; find $D -type f| wc -l; done
You can use
find . | wc -l
find .
will list all files and folders and theire contents starting in your current folder.
wc -l
counts the results of findThis solution counts also the folders, I gave the mark cause it matched my occasion that I didnt want to count them in :)
The solution will fail on files which names contain a newline.
find seems to be quicker than tree so I used below to count files in each directory of the current working directory (ignoring files in CWD) with allowing directories to have spaces:
ls -d */ | while read dir_line do echo -n "$dir_line :" find "$dir_line" -type f | wc -l done
Parsing output of `ls` is very bad idea.
Great code, how can I arrange the output lines say in an increasing or decreasing count of files
I'd go with this option myself:
ls -alR | grep -c ^-
Please add some details ...
License under CC-BY-SA with attribution
Content dated before 6/26/2020 9:53 AM
user unknown 9 years ago
The solution will fail on files which names contain a newline.