Ubuntu – Do I see a difference in file count between Nautilus and find

filesfindnautilus

For a university project, I have to count all the files in a folder. I have used the command:

find ./dirName | wc -l

Although when I compare this to the file count provided by Nautilus it is considerably more. See the screenshot below:

enter image description here

./dirName is actually a directory of files from a repository (SVN/GIT) and I need to find out how many files make up the system.

Could anyone explain why these differences occur and maybe tell me which one is more reliable?

Best Answer

Nautilus doesn't count hidden files.

Files and directories starting with a dot (.) are hidden in Linux.

Steps to reproduce:

mkdir somedir && cd somedir
touch .hidden .hidden2 regular regular2      # 4 files, 2 hidden
find . | wc -l                               # outputs 5 (4 files + dir itself)

Nautilus reports: Contents: 2 items, totalling 0 bytes

Using Git

Here's a quick demonstration on the amount of files for the metadata used in Git, all in the .git directory.

git init myrepo                              # Initialized [...] in myrepo/.git/
cd myrepo/
find . | wc -l                               # outputs 23! for an empty repository
tree -a                                      # outputs 10 directories, 12 files

echo "have to add something for git ls-tree" > somefile
git add somefile && git commit -m "Initial commit"
find . | wc -l                               # outputs 38 (!)
git ls-tree -r HEAD | wc -l                  # outputs 1

And also Nautilus reports 1 there.

My suggestion: use tree

As Gilles pointed out in his answer, using find and piping it to wc isn't overly reliable if the file names contain special characters.

It seems that tree is capable of doing this right:

tree -a
.
├── dir
│   └── regular3
├── dir2
├── .hidden
├── .hidden2
├── regular
└── regular2

2 directories, 5 files