Score:2

why is output different of counting files?

in flag

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!

Terrance avatar
id flag
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?
bac0n avatar
cn flag
Related: [How to count all folders and subfolders...](https://askubuntu.com/a/1360535/986805)
Márton Stark avatar
in flag
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 :)
Gounou avatar
us flag
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?
Terrance avatar
id flag
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.
Score:3
us flag
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.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.