2

I just want to count all the files in a directory. I used two method what I found:

  1. tree /home/bkp Whit this method the output is 2177879 file.

  2. find /home/bkp -type f | wc -l And with that one is 2176704 file

What makes the difference? How I could count the all fille under all directories and subdirectories?

Thank you!

  • It is a good question here. However it sounds like one or the other has a bug. Try doing `tree -d` and `find` with `-type d` for directories, and you get 2 completely different results as well. The question would be though, which one has the bug? – Terrance Sep 15 '21 at 22:17
  • Related: [How to count all folders and subfolders...](https://askubuntu.com/a/1360535/986805) –  Sep 16 '21 at 00:40
  • Hi, I tried both, and different is still exist. with `tree -d` **306647** and with `find` **306760**. I think I don't start to count manually :-D :) – Márton Stark Sep 16 '21 at 06:32
  • Try `ls -alR | grep -c '^-'` to count files and compare the result with tree and find (if you didn't add files to the directory since). `tree -ad` (also count hidden directories) is equal to `find -type d` that could explain the difference between the two. However, I don't understand why `tree /home/bkp` output is greater than `find`. Is this an automatic backup folder or do you think file count may have change between the two commands? – Gounou Sep 16 '21 at 12:44
  • Even `tree -ad` and `find . -type d` both come up with different totals. The `find` command will usually come up with a lower count though. – Terrance Sep 16 '21 at 13:13

1 Answers1

3
tree
# Also output directories but not hidden files
.
├── Directory1
├── Directory2
├── File1
└── File2

tree -a
# Also output hidden files and hidden directories
.
├── Directory1
├── Directory2
├── File1
├── File2
├── .Hidden_Directory1
├── .Hidden_Directory2
├── .Hidden_File1
└── .Hidden_File2

find -type f
# Files and hidden files
./File1
./File2
./.Hidden_File1
./.Hidden_File2

tree -aifF | grep -v '/$'
# Output files and hidden files
.
./File1
./File2
./.Hidden_File1
./.Hidden_File2

Source : How to make tree output only files?

The -i and -f arguments cause tree to output full paths on each line, rather than indenting. The -F argument causes it to append an / to directory names, which are filtered out by the inverted grep (grep -v '/$').

man tree
-f     Prints the full path prefix for each file.
-i     Makes tree not print the indentation lines, useful when 
       used in conjunction with the -f option. Also removes as much 
       whitespace as possible when used with the -J or -x options.
Gounou
  • 591
  • 3
  • 7